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.
| Author |
Topic |
|
cyp
Starting Member
1 Post |
Posted - 2009-05-28 : 16:56:07
|
| Hi!I am currently working with a CRM application with a SQL database.These are the current tables:Table: Customer- customerID- firstname- lastname- email- membernumberTable: Sale- saleID- customerID- membernumber- wareID- warequantumTable: Ware- wareID- name- category- priceWhat I really need help with is to find the customers who shop more than the average shopper within a specific ware-category.Any ideas? :] |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-05-28 : 17:28:16
|
| Post what you've tried so far. How do you define "shop more"? total lifetime number of items? total lifetime sale price? Most sale occurances?Be One with the OptimizerTG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-29 : 14:33:53
|
| [code]SELECT CustomerID,Category,TotalShop,ShoppingAverageFROM(SELECT c.CustomerID,w.Category,SUM(s.warequantum) AS TotalShopFROM Customer cINNER JOIN Sale sON c.CustomerID=s.CustomerIDINNER JOIN Ware wON w.wareID=s.wareIDGROUP BY c.CustomerID,w.Category)mINNER JOIN(SELECT w.Category,SUM(warequantum)*1.0/COUNT(DISTINCT customerID) AS ShoppingAverageFROM Sale sINNER JOIN Ware wON w.wareID=s.wareIDGROUP BY w.Category)nON n.Category=m.CategoryWHERE m.TotalShop>n.ShoppingAverage[/code] |
 |
|
|
|
|
|