2012-12-14 69 views
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. 

任何人都可以告訴我如何總結每列,並顯示總和以及兩個表中每列的記錄數同時顯示計數在我的查詢?使用sql和case語句中的總和和計數

我的結果應該是這種

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語句來完成此操作。我想在一個報告中顯示每個列的總和和兩個表的每列的計數。我擁有的查詢屬於以下類型。

+0

嘗試使用子查詢。 – Kermit

+0

顯示你想要的輸出是非常有用的,但是如果我們也有一個你想從中獲得輸出的輸入數據*的樣本,那將會非常有用。 –

回答

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