July 14, 2006 at 2:59 am
Hi!
I have got a problem writing a simple query.
I have a table
create table Orders (OrderID int, CustID int, ProdID int, Quant int)
I need to find out how many Customers who ordered one product, also ordered another. For example, count customers who orderd ProdID=1 and also ordered ProdID=2, and so on.
July 14, 2006 at 3:04 am
I would suggest using Group By;
select CustID, ProdId, sum(Quant)
from Orders
Group By CustID, ProdId
July 14, 2006 at 3:29 am
Thanks, but it is not what I need.
I'll clarify.
create table Orders (OrderID int identity(1,1), CustID int, ProdID int, Quant int)
insert into Orders (CustID, ProdID, Quant)
values (1, 100, 10)
insert into Orders (CustID, ProdID, Quant)
values (2, 100, 10)
insert into Orders (CustID, ProdID, Quant)
values (1, 200, 10)
insert into Orders (CustID, ProdID, Quant)
values (4, 300, 10)
insert into Orders (CustID, ProdID, Quant)
values (5, 100, 10)
insert into Orders (CustID, ProdID, Quant)
values (2, 200, 10)
Actually Quantity is not necessary here.
Product 100 have ordered 2 customers, product 100 and 200 ordered 1 customer, and so on.
As a result I have to get symmetric table
100 | 200 | 300 | |
100 | 3 | 1 | 0 |
200 | 1 | 2 | 0 |
300 | 0 | 0 | 1 |
I tried
select ProdID, CustID, count(CustID) from Orders
group by ProdID, CustID with cube
But it does give only part of result I need.
July 14, 2006 at 6:58 am
OK, most probably you'll have a table Customers somewhere. You can use that to find out who has ordered both:
SELECT COUNT(distinct c.CustID)
FROM Customers c
JOIN Orders o ON o.CustID = c.CustID AND ProdID = 1
JOIN Orders o ON o.CustID = c.CustID AND ProdID = 2
Each of the joins will leave in resultset only those customers that have ordered the respective product. Together, only those that ordered both products will be displayed.
Good when you just want to find out this... if you want to create a cross-report (symmetric table), there should be some better solution. Unfortunately, I'm not sure I understand what does the table in your example show, so I won't go into that.
EDIT: You wrote "Product 100 have ordered 2 customers" ... is that just a typo or misunderstanding on my side? It seems to me that product 100 have ordered 3 customers. Or are you counting only those that have product 100 and no other products? But then it would be 1, not 2...
July 14, 2006 at 7:16 am
For just 2 ProdID the following query also works
select count(a.CustID) from (select CustID from Orders where ProdID=100) a
join (select CustID from Orders where ProdID=200) b
on a.CustID=b.CustID
But I would like to have a query which populates the whole cross reference table.
July 14, 2006 at 8:02 am
This will get the data for all nonempty permutations of two products.
You may then want to generate a dynamic crosstab from the data.
o1.CustID = o2.CustID
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
July 14, 2006 at 8:45 am
Great, thanks.
Query only lacks GROUP BY clause
July 14, 2006 at 8:47 am
Can you guess what it should be?
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
July 14, 2006 at 9:38 am
Sure
And any ideas how to preset the results in the form of matrix? Pivot is not smart in SQL Server 2000.
And how to get 0 in the results when there is no row count?
July 15, 2006 at 4:28 am
table #Salesperson
nocount on
table #Salesperson (SalespersonID int not null unique clustered)
#Salesperson
top 1023 SalesPersonID from Sales.SalesPerson order by SalesPersonID
@cr_SalesPerson int, @sql varchar(8000)
@cr_SalesPerson = min(SalesPersonID) from #Salesperson
@cr_SalesPerson is not null
@cr_SalesPerson = min(SalesPersonID) from #Salesperson
@cr_SalesPerson is not null
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
July 18, 2006 at 5:44 am
I am sorry, I did not get what this script does and how it works in my case. I need to write a query which returns "0" instead of rows which do not appear in the result set.
July 18, 2006 at 9:54 am
I thought you also wanted a crosstab/pivot table...?
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply