Оператор выбора SQL - выбор с использованием одного столбца, равного переменным буксировки - PullRequest
0 голосов
/ 09 октября 2018

в следующем операторе выбора я хочу поставить два значения, когда @PAPCOD = 'SIN' = значения 'DD' и 'SD' и @PAPCOD = 'ENG' = значения 'DI' и 'SI'

как я могу это сделать.Только я могу поставить одно значение

SELECT ISNULL((SUM(Con_Amount)),0) +
       (SELECT DISTINCT ISNULL(SUM(Correspondent_Other_Payments.Oth_Pmt_Amount),0)
               FROM Correspondent_Other_Payments
                    WHERE Correspondent_Other_Payments.Oth_Cnt_Code = Contributions.Con_Cnt_Code and
                          Correspondent_Other_Payments.Oth_Prv_Code = Contributions.Con_Prv_Code and
                          Correspondent_Other_Payments.Oth_Dst_Code = Contributions.Con_Dst_Code and
                          Correspondent_Other_Payments.Oth_Cor_Code = Contributions.Con_Cor_Code and
                          Correspondent_Other_Payments.Oth_Pmt_Date = CONVERT(DATE, @PubDatE, 111) and
                          Correspondent_Other_Payments.Oth_AuditChk = 'Y')
       FROM Contributions
            INNER JOIN Correspondent_Master
                       ON Contributions.Con_Cnt_Code = Correspondent_Master.Cor_Country_Code and
                          Contributions.Con_Prv_Code = Correspondent_Master.Cor_Province_Code and
                          Contributions.Con_Dst_Code = Correspondent_Master.Cor_District_Code and
                          Contributions.Con_Cor_Code = Correspondent_Master.Cor_Code
                          WHERE Con_paper LIKE
                                               CASE
                                                    WHEN @PapCod = 'SIN' THEN
                                                         'DD'
                                                    WHEN @PapCod = 'ENG' THEN
                                                         'DI'
                                                    ELSE
                                                         @PapCod
                                               END and
                               (Con_PubDate BETWEEN CONVERT(DATE, @PubDatB, 111) and
                                                    CONVERT(DATE, @PubDatE, 111)) and
                                Contributions.Audit_Chk = 'Y' /* Audited */
                                GROUP BY Contributions.Con_Cnt_Code,
                                         Contributions.Con_Prv_Code,
                                         Contributions.Con_Dst_Code,
                                         Contributions.Con_Cor_Code,
                                         Contributions.Con_Paper
                                         ORDER BY Contributions.Con_Cnt_Code,
                                                  Contributions.Con_Prv_Code,
                                                  Contributions.Con_Dst_Code,
                                                  Contributions.Con_Cor_Code

Sampal Data & Result what I Want

1 Ответ

0 голосов
/ 09 октября 2018

Просто добавьте ИЛИ, скопируйте тот же CASE и измените значения на SD и SI.

DECLARE
    @t TABLE(Con_Paper VARCHAR(50), Con_Amount INT)

    INSERT INTO @t VALUES 
    ('DD', 100)
    ,('SD', 250)
    ,('BN',450)
    ,('DD',50)
    ,('DI',350)
    ,('NL',65)


DECLARE @PapCod VARCHAR(50) = 'ENG'

SELECT SUM(Con_Amount)
FROM @t
WHERE 
    (Con_paper LIKE
                   CASE
                        WHEN @PapCod = 'SIN' THEN 'DD'
                        WHEN @PapCod = 'ENG' THEN 'DI'
                        ELSE @PapCod
                   END 
    OR 
    Con_paper LIKE
                   CASE
                        WHEN @PapCod = 'SIN' THEN 'SD'
                        WHEN @PapCod = 'ENG' THEN 'SI'
                        ELSE @PapCod
                   END 
    ) 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...