首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SQL Select语句-使用等于两个变量的一列进行选择

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

Stack Overflow用户
提问于 2018-10-09 16:04:47
回答 1查看 91关注 0票数 0

在下面的select语句中,当@PAPCOD = 'SIN‘=值是'DD’和'SD‘以及@PAPCOD = 'ENG’=值是'DI‘和'SI’时,我想放入两个值

我该怎么做呢。只有一个值可以

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

发布于 2018-10-09 16:26:39

只需添加一个OR,复制相同的情况,并将值更改为SD和SI。

代码语言:javascript
运行
复制
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 
    ) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52716131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档