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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SUM and JOIN Query ???

Author  Topic 

chulz90
Starting Member

26 Posts

Posted - 2012-11-29 : 02:08:58
hello guys, i've some case. please help me to fix this case

tb_item
item Name brand
B1 item 1 M1
B2 item 2 M2
B3 item 3 M3
B4 item 4 M4

tb_brand
brand brand name
M1 brand 1
M2 brand 2
M3 brand 3
M4 brand 4

tb_quantity
item quantity
B1 10
B2 30
B3 20
B4 25
B1 30
B1 60
B2 50
B3 20

the question is, how to summarize in tb_quantity with the view below, using SUM and JOIN

this is the SQL View
item Name total brand name
B1 item 1 100 brand 1
B2 item 2 80 brand 2
B3 item 3 40 brand 3
B4 item 4 25 brand 4

please help me

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-29 : 02:29:23
[code]
SELECT i.item, name, total, brandname
FROM @tb_item i
JOIN @tb_brand b ON i.brand = b.brand
JOIN (SELECT item, SUM(quantity) total FROM @tb_quantity GROUP BY item) q ON q.item = i.item
[/code]

--
Chandu
Go to Top of Page

chulz90
Starting Member

26 Posts

Posted - 2012-11-29 : 02:39:58
quote:
Originally posted by bandi


SELECT i.item, name, total, brandname
FROM @tb_item i
JOIN @tb_brand b ON i.brand = b.brand
JOIN (SELECT item, SUM(quantity) total FROM @tb_quantity GROUP BY item) q ON q.item = i.item


--
Chandu



thank's so much chandu
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-29 : 04:11:29
quote:
Originally posted by chulz90


thank's so much chandu



Welcome

--
Chandu
Go to Top of Page
   

- Advertisement -