Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I need to find out customers having more than one order for the same productXyz table ---------Customer_IdOrder_IDProdcut_ID AbcMnrI can get information likeSelect customer_Id,Product_Id, Count(Customer_ID) Cnt from Xyz Group By customer_Id,Product_Id having Count(Customer_ID) >= 2 Order By Customer_Id But I want to include rest of the columns and I don’t want to use any temp tables either
tkizer
Almighty SQL Goddess
38200 Posts
Posted - 2004-09-02 : 15:10:51
So which row do you want to show Abc and Mnr for? Let's say the customer has 2 orders and Abc equals 0 and Mnr equals 0 in the first one, but in the second one, Abc equals 1 and Mnr equals 1. Which one do you want to show?Please post some data for us and the expected result set to make this clearer.Tara
Shastryv
Posting Yak Master
145 Posts
Posted - 2004-09-02 : 15:17:34
Actually ABC and mnr are columns. Let’s say table structure is Customer_IdOrder_IDProdcut_ID Prod_NameProd_CostNow I want to get list of the Customer_Id,Order_ID,Prodcut_ID,Prod_Name,Prod_Cost for the customers having more than one order for the same product_ID
tkizer
Almighty SQL Goddess
38200 Posts
Posted - 2004-09-02 : 15:21:18
This should work:
SELECT x.Customer_Id, x.Order_ID, x.Product_ID, x.Prod_Name, x.Prod_CostFROM Xyz xINNER JOIN ( SELECT customer_Id, Product_Id, COUNT(Customer_ID) AS Cnt FROM Xyz GROUP BY customer_Id, Product_Id HAVING COUNT(Customer_ID) >= 2 ) tON x.Customer_Id = t.customer_Id AND x.Product_ID = t.Product_Id
Tara
Shastryv
Posting Yak Master
145 Posts
Posted - 2004-09-02 : 15:32:54
Wow that’s fast Tara. Works excellentI came up to Select * from Xyzwhere Customer_Id in (Select Customer_Id from XyzGroup by Customer_ID,Product_ID Having Count(Customer_ID) >= 2 )But couldn’t figure our rest…I guess I need to upgrade my T-SQL Skill. But that thank for your reply