2015-01-10 131 views
0

我需要一個查詢,顯示工作人員尚未付款的JobID,但公司已付款。以下是表列和樣本數據:通過聚合過濾SQL查詢結果

tblInvoices columns: 
------------------- 
JobID 
InvoiceID 
WorkerPaidAmountTotal 
CompanyPaidAmountTotal 


Sample data 
----------- 

JobID | InvoiceID | WorkerPaidAmountTotal | CompanyPaidAmountTotal 

1   30   100     150 
1   31   0     100 
2   32   0     75 
3   33   25     50 
3   34   10     30 
4   35   0     0 

我知道如何獲得支付給工人或公司的金額的總和。結果是這樣的:


JobID Worker Company 
1  100  250 
2  0  75 
3  35  80 
4  0  0 

但我需要的是當操作員已得到0只是JobIDs的結果和公司> 0。我想結果應該是這樣的,但我想不通的查詢可以這樣做:


JobID Worker Company 
2  0  75 

回答

1

使用HAVING子句可以過濾組。試試這個:

SELECT jobid, 
     Worker=Sum(WorkerPaidAmountTotal), 
     Company=Sum(CompanyPaidAmountTotal) 
FROM tablename 
GROUP BY jobid 
HAVING Sum(WorkerPaidAmountTotal) = 0 
     AND Sum(CompanyPaidAmountTotal) > 0 
+0

是的,完美的作品。很簡單。 – TZERO

0
select jobid, worker, company where WorkerPaidAmountTotal = 0 and CompanyPaidAmountTotal 

似乎平淡做...可能是我did'nt明白的問題