Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Tim_Blanch
Starting Member
1 Post |
Posted - 2012-08-20 : 16:41:28
|
| I have 3 tablesBillHeader with SupplierId, CustomerIdBillProperty with SupplierIdBillChg with CustomerId, CommFlgwith the query:SELECT DISTINCT CustomerId, SUM(CASE WHEN CommFlg = 1 THEN 1 ELSE 0 END) AS ComFlag FROM BillChg GROUP BY CustomerIdI get my CustomerId, ComFlag where ComFlag is the sum of all bool fields that are associated with CustomerId.Now I would like(that I am having trouble with) to add my SupplierId that is associated with the CustomerId.I have tried:SELECT DISTINCT a.SupplierId, b.CustomerId, SUM(CASE WHEN c.CommFlg = 1 THEN 1 ELSE 0 END) AS ComFlagFROM BillProperty aJOIN BillHdr b ON a.SupplierId = b.SupplierIdJOIN BillChg c ON b.CustomerId = c.CustomerIdWHERE a.SupplierId = 745079813 AND c.CustomerId = 2277565GROUP BY a.SupplierId, b.CustomerId, c.CommFlgORDER BY a.SupplierIdBut no luckI would like an output like:SupplierId, CustomerId, Sum of CommFlgCan anyone help me a little here?Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-20 : 16:51:33
|
| [code]SELECT a.SupplierId, b.CustomerId,c.ComFlagFROM BillProperty aJOIN BillHdr b ON a.SupplierId = b.SupplierIdJOIN(SELECT CustomerId, SUM(CASE WHEN CommFlg = 1 THEN 1 ELSE 0 END) AS ComFlag FROM BillChg GROUP BY CustomerId)cON b.CustomerId = c.CustomerIdWHERE a.SupplierId = 745079813 AND c.CustomerId = 2277565ORDER BY a.SupplierId[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|