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 |
|
MGA
Starting Member
28 Posts |
Posted - 2010-04-26 : 04:31:38
|
| hello every1;i have the following tables - Products ( Prduct_Id int primary key, Product_Name varchar(50))- Adding_Products( Add_Id int primary key, Product_Id foreign key Products(Product_Id), Quantity int)if the data in the products isProduct_Id Product_Name 1 a 2 b 3 cand the data in the Adding_Products is add_Id Product_Id Quantity 1 1 5 2 2 10 3 1 2 4 1 3 5 3 20 6 3 2 How to select the following data :Product_Name Total_Quantity a 10 b 10 c 22 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-04-26 : 04:40:12
|
| select a.Product_Name,sum(isnull(b.Quantity,0)) from products a left join Adding_Products b on a.Product_Id=b.Product_Id |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-26 : 05:03:02
|
| [code]SELECT P.Product_Name, Total_Quantity = COALESCE(D.Sum, 0)FROM Products POUTER APPLY (SELECT SUM(Quantity) AS Sum FROM Adding_Product WHERE Product_ID = P.Product_ID) DAlso:SELECT Product_name,COALESCE((SELECT SUM(Quantity) FROM Adding_Products WHERE P.product_ID = product_id), 0) AS Total_QuantityFROM Products P[/code] |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-04-26 : 05:08:36
|
Your query is not correct. You missed GROUP BY clausequote: Originally posted by sakets_2000 select a.Product_Name,sum(isnull(b.Quantity,0)) from products a left join Adding_Products b on a.Product_Id=b.Product_Id
|
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-26 : 05:30:09
|
quote: Originally posted by ms65g Your query is not correct.quote: Originally posted by sakets_2000 select a.Product_Name,sum(isnull(b.Quantity,0)) from products a left join Adding_Products b on a.Product_Id=b.Product_Id
Yes.Because he forgot to put a group by clause for Product_Name.PBUH |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-26 : 06:04:19
|
quote: Originally posted by ms65g Your query is not correct.quote: Originally posted by sakets_2000 select a.Product_Name,sum(isnull(b.Quantity,0)) from products a left join Adding_Products b on a.Product_Id=b.Product_Id
Yes, it needs a "GROUP BY a.Product_Name". However, the left join method is much faster than the outer apply or subquery methods. Run this and then look at the messages to see the difference:CREATE TABLE Products(Product_Id int primary key,Product_Name varchar(50))GOCREATE TABLE Adding_Products(Add_Id int primary key,Product_Id INT CONSTRAINT FK_Adding_Products foreign key (Product_Id) REFERENCES Products(Product_Id),Quantity int)GOSET NOCOUNT ONDECLARE @Count INT SET @Count = 0WHILE @Count < 1000BEGIN INSERT INTO Products SELECT @Count, CAST(@Count AS VARCHAR(50)) SET @Count = @Count + 1ENDSET @Count = 0WHILE @Count < 100000BEGIN INSERT INTO Adding_Products SELECT @Count, RAND() * 1000, RAND() * 1000 SET @Count = @Count + 1ENDGOSET STATISTICS IO ONGO SET STATISTICS TIME ONGO PRINT 'Method 1'select a.Product_Name,sum(isnull(b.Quantity,0)) from products a left join Adding_Products b on a.Product_Id=b.Product_IdGROUP BY a.Product_NamePRINT 'Method 2'SELECT P.Product_Name, Total_Quantity = COALESCE(D.Sum, 0)FROM Products POUTER APPLY (SELECT SUM(Quantity) AS Sum FROM Adding_Products WHERE Product_ID = P.Product_ID) DPRINT 'Method 3'SELECT Product_name,COALESCE((SELECT SUM(Quantity) FROM Adding_Products WHERE P.product_ID = product_id), 0) AS Total_QuantityFROM Products PGOSET STATISTICS IO OFFGO SET STATISTICS TIME OFFGO DROP TABLE Adding_ProductsDROP TABLE Products ------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
portia_in_sql
Starting Member
1 Post |
Posted - 2010-04-29 : 05:23:54
|
| Lets keep it simple and sweet...Following query will give you the desired output:select b.Product_Name,SUM(Quantity) As Total_Quantity from Adding_Products a JOIN Products bON a.Product_Id=b.Prduct_IdGROUP BY b.Product_NamePortia |
 |
|
|
|
|
|
|
|