3
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

我正在尝试准备一份加入两个表格的报告。我正在使用多个案例语句来完成此操作。我想在一个报告中同时显示两个表的每列的总和和每列的计数。我的查询属于以下类型。

4

1 回答 1

1

我认为这就是你正在寻找的东西。对于显示在该行上的代码和国家/地区值,它将为您提供所显示组合的通过和失败帐户,并且您将在定义聚合的列上具有唯一性。

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
于 2012-12-20T03:08:27.497 回答