| Author |
Topic |
|
constantinos1987
Starting Member
25 Posts |
Posted - 2008-12-11 : 21:43:58
|
| i want to find the customers with at least once transaction which contained more than the half item_categoriesthe tables i have are:transactions(transID,transCustomer,transStore,transDateTime)transactionItems(transID,itemID,quantity,ivalue)items(itemID,itemGroupID)item_groups(groupID,group_categoryID)item_categories(categoryID) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-11 : 22:49:09
|
| [code]SELECT t.*FROM transactions tINNER JOIN (SELECT ti.transID,COUNT(ig.group_categoryID) AS CatCount FROM transactionItems ti INNER JOIN items i ON i.itemID=ti.itemID INNER JOIN item_groups ig ON ig.groupID=i.itemgroupID GROUP BY ti.transID)tmpON tmp.transID=t.transIDWHERE tmp.CatCount>(SELECT COUNT(*) FROM item_categories)/2.0[/code] |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-11 : 23:13:38
|
| SELECT t.transID,t.transCustomer ,t.transStore ,t.transDateTime FROM transactions11 tCROSS APPLY (SELECT ti.transID,COUNT(ig1.group_categoryID) AS ccountFROM transactionItems11 tiINNER JOIN items11 i ON i.itemID=ti.itemIDINNER JOIN item_groups11 ig1 ON ig1.groupID=i.itemgroupIDGROUP BY ti.transID)tmWHERE tm.transID=t.transID AND tm.ccount >= (SELECT COUNT(*) FROM item_categories11)/2 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-11 : 23:21:43
|
| please note that APPLY operator will work only in sql 2005 and above with compatibility level 90 and above |
 |
|
|
constantinos1987
Starting Member
25 Posts |
Posted - 2008-12-12 : 13:06:45
|
| i have sql 2005the first answer gives me 961 rows and the second one 1544 rowsadding a distinct to the codes and dividing with 7 because i had no answers.........SELECT t.transCustomer,tmp.Category_CountFROM transactions tINNER JOIN (SELECT ti.transID,COUNT ( distinct ig.group_categoryID) AS Category_Count FROM transactionItems ti INNER JOIN items i ON i.itemID=ti.itemID INNER JOIN item_groups ig ON ig.group_ID=i.itemgroupID GROUP BY ti.transID)tmpON tmp.transID=t.transIDWHERE tmp.Category_Count>(SELECT COUNT(*) FROM item_categories)/7order by t.transCustomer descSELECT t.transCustomer ,tm.ccountFROM transactions tCROSS APPLY(SELECT ti.transID,COUNT( distinct ig1.group_categoryID) AS ccountFROM transactionItems tiINNER JOIN items i ON i.itemID=ti.itemIDINNER JOIN item_groups ig1 ON ig1.group_ID=i.itemgroupIDGROUP BY ti.transID)tmWHERE tm.transID=t.transIDAND tm.ccount >= (SELECT COUNT(*) FROM item_categories)/7order by t.transCustomer desci get as answer 2913 at the first staatement and 40083 at the second |
 |
|
|
constantinos1987
Starting Member
25 Posts |
Posted - 2008-12-12 : 16:43:23
|
quote: Originally posted by visakh16
SELECT t.*FROM transactions tINNER JOIN (SELECT ti.transID,COUNT(ig.group_categoryID) AS CatCount FROM transactionItems ti INNER JOIN items i ON i.itemID=ti.itemID INNER JOIN item_groups ig ON ig.groupID=i.itemgroupID GROUP BY ti.transID)tmpON tmp.transID=t.transIDWHERE tmp.CatCount>(SELECT COUNT(*) FROM item_categories)/2.0
is there any way to collect the number of times every customer matches the criteria??? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-12 : 23:40:44
|
quote: Originally posted by constantinos1987
quote: Originally posted by visakh16
SELECT t.*FROM transactions tINNER JOIN (SELECT ti.transID,COUNT(ig.group_categoryID) AS CatCount FROM transactionItems ti INNER JOIN items i ON i.itemID=ti.itemID INNER JOIN item_groups ig ON ig.groupID=i.itemgroupID GROUP BY ti.transID)tmpON tmp.transID=t.transIDWHERE tmp.CatCount>(SELECT COUNT(*) FROM item_categories)/2.0
is there any way to collect the number of times every customer matches the criteria???SELECT t.transCustomer,COUNT(transID) AS transactionsFROM transactions tINNER JOIN (SELECT ti.transID,COUNT(ig.group_categoryID) AS CatCount FROM transactionItems ti INNER JOIN items i ON i.itemID=ti.itemID INNER JOIN item_groups ig ON ig.groupID=i.itemgroupID GROUP BY ti.transID)tmpON tmp.transID=t.transIDWHERE tmp.CatCount>(SELECT COUNT(*) FROM item_categories)/2.0GROUP BY t.transCustomer
number of times they had a transaction which contain greater than half category of items? if yes,use above |
 |
|
|
constantinos1987
Starting Member
25 Posts |
Posted - 2008-12-13 : 12:24:36
|
| thank you guys!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-13 : 12:33:09
|
| welcome |
 |
|
|
|