![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
We have a pretty straightforward Order, Customer, Products table setup. I'd like to figure out a query that would tell me which products tend to get ordered together. For example: Create Table Orders ( Cust_ID INTEGER, Order_ID INTEGER); INSERT INTO Orders (Cust_ID, Order_ID) VALUES (333,1), (555,2), (777,3); CREATE TABLE Items ( Item_ID INTEGER, Order_ID INTEGER, Item_Desc CHAR(50) , Qty INTEGER); INSERT INTO Items (Item_ID,Order_ID, Item_Desc, Qty) VALUES (900,1,'Widget A', 10), (901,1,'Widget B', 12), (902,1,'Widget G', 12), (903,2,'Widget B', 2), (904,2,'Widget D', 8), (905,3,'Widget B', 5), (906,3,'Widget A', 4), (907,3,'Widget C', 22); I'd like to somehow have it figure out that 'Widget A' gets ordered with 'Widget B' X times and have it sort by frequency. I realize this might be a long running process, but can live with that. Eventually, I'd like it to tell me which products get ordered alot so I can put them next to "related" products in the warehouse. I can do some of the logic in the code; I'd appreciate it if someone could at least get me started on the query. |
|
On a side note, can SQL do recursion? |
#3
| |||
| |||
|
|
We have a pretty straightforward Order, Customer, Products table setup. I'd like to figure out a query that would tell me which products tend to get ordered together. For example: Create Table Orders ( Cust_ID INTEGER, Order_ID INTEGER); INSERT INTO Orders (Cust_ID, Order_ID) VALUES (333,1), (555,2), (777,3); CREATE TABLE Items ( Item_ID INTEGER, Order_ID INTEGER, Item_Desc CHAR(50) , Qty INTEGER); INSERT INTO Items (Item_ID,Order_ID, Item_Desc, Qty) VALUES (900,1,'Widget A', 10), (901,1,'Widget B', 12), (902,1,'Widget G', 12), (903,2,'Widget B', 2), (904,2,'Widget D', 8), (905,3,'Widget B', 5), (906,3,'Widget A', 4), (907,3,'Widget C', 22); I'd like to somehow have it figure out that 'Widget A' gets ordered with 'Widget B' X times and have it sort by frequency. I realize this might be a long running process, but can live with that. Eventually, I'd like it to tell me which products get ordered alot so I can put them next to "related" products in the warehouse. I can do some of the logic in the code; I'd appreciate it if someone could at least get me started on the query. On a side note, can SQL do recursion? Thanks, Lee A nice puzzle. The following seems to work: |
#4
| |||
| |||
|
#5
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |