SQL Select语句-使用等于两个变量的一列进行选择

时间:2018-10-09 08:04:47

标签: sql sql-server tsql

在下面的select语句中,当 @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 :(得分:0)

只需添加一个OR,即可复制相同的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 
    )