tags:

views:

46

answers:

4

Im having two tables

TableA

Custid prodid Amt1
  1      10   200
  2      20   300
  3      10   400
  4      30   300
  5      20   400
  6      30   200

TableB

prodid  Amt2
    10   100
    20   200
    30   300

and i want sum of all amount group by prodid. can any one give me both ansi and Tsql quries.

A: 

What about that? :-)

SELECT SUM(a.Amt1),SUM(b.Amt2)
FROM (TableA AS a INNER JOIN TableB AS b ON a.prodid = b.prodid)
GROUP BY a.prodid

Peter Wippermann
A: 

I believe this will work anywhere:

SELECT a.Custid,a.prodid,sum(a.Amt1 + b.Amt2) as AmtSum FROM TableA a JOIN TableB b ON (a.prodid = b.prodid) GROUP BY a.Custid,a.prodid;
kicsit
+1  A: 
SELECT a.prodid,sum(a.Amt1 + b.Amt2) as AmtSum 
     FROM TableA a JOIN TableB b ON (a.prodid = b.prodid) 
     GROUP BY a.prodid;
Salil
A: 

try this... I think it will also work if some product id not in TABLEB

SELECT a.prodid,SUM(a.Amt1),SUM(b.Amt2)
FROM TableA AS a left outer JOIN TableB AS b ON a.prodid = b.prodid
GROUP BY a.prodid
Harendra