在sql中的case语句中使用sum和count

select req.code ,res.code,
case
( 
when (req.code==res.code) then 'pass'
when (req.code<>res.code) then 'fail'
/*....2 more case 'when' 'then' statements here*/
end ) result,

req.country ,res.country,

case (when then staments as above)result,
/*.......case stmts upto 70 statemnts*/
from requesttable req full outer join responsetable res on
req.id=res.id 
and ....some conditions.

任何人都可以告诉我如何对每列进行求和并同时显示两个表的每列中的总和以及记录数,并在查询中显示计数?

我的结果应该是这样的

code  code1  result  sum     sum1    equivalence country country1 result1  sum    sum1
100   100    pass    200000  25000   fail        ind     aus      fail     800000 800000

equivalence
pass

我正在准备一份加入两张桌子的报告.我使用多个case语句来完成此任务.我想在一个报告中一起显示每个列的总和和两个表的每列的数量.我拥有的查询属于以下类型.

最佳答案 我想这就是你要找的东西.对于显示在该行上的代码和国家/地区值,它将为您显示所显示组合的通过和失败帐户,并且您将在定义聚合的列上具有唯一性.

Select  req.code, 
        res.code,
        Sum(Case When req.code = res.code) Then 1 Else 0 End) As [Pass],
        Sum(Case When req.code <> res.code) Then 1 Else 0 End) As [Fail],
        req.country, 
        res.country,
        Sum(Case When req.country = res.country) Then 1 Else 0 End) As [Pass],
        Sum(Case When req.country <> res.country) Then 1 Else 0 End) As [Fail]
From    requesttable req
Full    Outer Join responsetable res
        On  req.id = res.id
Where   ...
Group   By  req.code,
            res.code,
            req.country,
            res.country
点赞