Hi,
Trying find number of item sold and paid. There are 3 tables and taking value from each table for the result.
[pre]Tables:
1) tProduct_Sold
2) tSub_Product_Details
3) tStore_Location
e.g.
tProduct_Sold
PK
Item_id date
1 1/1/2015
2 1/2/2015
3 1/3/2015
4 1/4/2015
5 1/5/2015
tSub_Product_Detail
FK FK
date Item_id Sub_item_id Sold_Status Payment_Status Location_id
1/1/2015 1 1A Sold Paid 1
1/1/2015 1 1B Sold Paid 1
1/2/2015 2 2A Sold UnPaid 5
1/2/2015 2 2B Sold UnPaid 5
1/3/2015 3 3A Sold In-Process 25
1/4/2015 4 4A Sold Paid 12
1/4/2015 4 4B Sold Paid 12
1/5/2015 5 5A Sold In-Process 12
tStore_Location
PK
Location_Id LocationName
1 Denton
2 Wichita Falls
3 Valley Brook
4 Cape Coral
5 Richmond
10 Charlotte
12 Jackson
25 Edmond
Output required
LocationName CountOfItem_id Paid
Denton 1 1
Edmond 1 0
Jackson 1 1
Richmond 1 1[/pre]
Getting different result, not sure what is missing in the SQL ?
[pre]LocationName CountOfItem_id Sold
Denton 2 0
Edmond 1 0
Jackson 3 0
Richmond 2 0[/pre]
Thanks,
Tech
Trying find number of item sold and paid. There are 3 tables and taking value from each table for the result.
[pre]Tables:
1) tProduct_Sold
2) tSub_Product_Details
3) tStore_Location
e.g.
tProduct_Sold
PK
Item_id date
1 1/1/2015
2 1/2/2015
3 1/3/2015
4 1/4/2015
5 1/5/2015
tSub_Product_Detail
FK FK
date Item_id Sub_item_id Sold_Status Payment_Status Location_id
1/1/2015 1 1A Sold Paid 1
1/1/2015 1 1B Sold Paid 1
1/2/2015 2 2A Sold UnPaid 5
1/2/2015 2 2B Sold UnPaid 5
1/3/2015 3 3A Sold In-Process 25
1/4/2015 4 4A Sold Paid 12
1/4/2015 4 4B Sold Paid 12
1/5/2015 5 5A Sold In-Process 12
tStore_Location
PK
Location_Id LocationName
1 Denton
2 Wichita Falls
3 Valley Brook
4 Cape Coral
5 Richmond
10 Charlotte
12 Jackson
25 Edmond
Output required
LocationName CountOfItem_id Paid
Denton 1 1
Edmond 1 0
Jackson 1 1
Richmond 1 1[/pre]
Code:
SELECT SL.LocationName,
Count(PS.Item_id) AS CountOfItem_id,
Sum(IIf(SPD.[Sold_Status]="Paid",1,0)) AS Sold
FROM (tProduct_Sold PS
LEFT JOIN tSub_Product_Detail SPD ON PS.Item_id = SPD.Item_id)
LEFT JOIN tStore_Location SL ON SPD.Location_id = SL.Location_Id
GROUP BY SL.LocationName;
Getting different result, not sure what is missing in the SQL ?
[pre]LocationName CountOfItem_id Sold
Denton 2 0
Edmond 1 0
Jackson 3 0
Richmond 2 0[/pre]
Thanks,
Tech