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 |
|
sankararam
Starting Member
1 Post |
Posted - 2010-08-11 : 21:36:49
|
| Hello Friends - I need to write a query using GROUP BY and I need your helpMy table is as follows: I have three tables A,B and C as followsTable AVendor id Vendor NameA1 AAAB1 BEST BUYC1 CIRCUIT CITYTable BVendor ID and Address (address1, address2, city,contry, zip,telephone)ID ADDRESS CITY CONTRY ZIP A1 ABC STRETT, NEW YORK USA 00101B1 EAST STREET,NEW YORK USA 01730C1 CIRCUIT CITY ATLANTA USA 33923Table CVendor iD, basic amout, disc amt, tax amt, check numberVENDOR BAS AMT DISC TAX CHEQUE NOA1 100 5 5 400023B1 250 50 50 209438A1 200 10 20 440480B1 200 0 10 308089B1 450 100 50 0808800C 1000 300 150 0000898I NEED TO DISPLAY MY RESULT AS FOLLOWS: Vendor id, vendor name, address, total checks against the vendor and total amount (group) so that one single amount for the vendor for all the transactionsRESULTA1 AAA ADDRESS 2 (COUNT FOR CHECIK NO) 310BA BEST BUY 3 850C1 CIRCUIT CIT 1 750(SUM THE AMOUNT AGAINST EACH VENDOR AND NO OF RECORDS WITH ADDRESS)I need a query to get the above result. Please help meMy advance thanks |
|
|
sneupane
Starting Member
28 Posts |
Posted - 2010-08-11 : 22:14:34
|
| I tried to do what you asked but might not be the exact solution coz' the question is not that clear.SelectVendorID,VendorName ,Address ,,count(Check Number) as TotalChecks ,sum(basic amount) as TotalAmountfrom Table TA-----------------Join Table TB------------------left join(select VendorID,Address from TB )TBon TA.VendorID = TB.VendorID-----------------Join Table TC------------------left join(select VendorID,Basic amount,Check Number, from TC )TCon TA.VendorID = TC.VendorIDGroup By VendorID,VendorName ,AddressSar |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-08-12 : 04:38:00
|
How you are calculating amout is it basic-disc+tax ?If yes then might be this one - SELECT C.VendorID, VendorName, Address, COUNT(CheckNumber), SUM(basic-disc+tax) FROM C INNER JOIN B ON C.VendorID = B.VendorID INNER JOIN A ON B.VendorID = A.VendorID GROUP BY C.VendorID, VendorName, Address Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-12 : 13:23:31
|
| but sample output certainly doesnt fit that formula as i cant understand how OP got 750 for C------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-08-13 : 02:48:10
|
Yes Visakh,Thats why I was asking to him that how you are calculating but for first row 310 is coming with this formula. so i thought there might me typo error.quote: Originally posted by visakh16 but sample output certainly doesnt fit that formula as i cant understand how OP got 750 for C------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
|
|
|
|
|