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 |
|
mapidea
Posting Yak Master
124 Posts |
Posted - 2008-11-18 : 03:40:59
|
| I have four tablesCustomer Table---------------------CustomerIDCustomerNameAddress1Address2CityCustomerOrder Table----------------------OrderIDCustomerIDDateOfOrderProduct Table---------------ProductIDProductNameProductDescriptionProductPriceOrderProduct Table-----------------OrderIDProductIDPriceofProductSql Query to findlist of customers who bought more than five products in last one week?Regards |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2008-11-18 : 04:02:30
|
| What have you got so far?- Lumbago |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-18 : 04:09:15
|
| Seems like an assignment question.Have a look at JOINs and Aggregation in bokks online and try it out. |
 |
|
|
mapidea
Posting Yak Master
124 Posts |
Posted - 2008-11-18 : 04:12:01
|
| SELECT CustomerId, CustomerName, COUNT(OrderID) as totalFROM Customer INNER JOIN CustomerOrder ON Customer.CustomerId = CustomerOrder.CustomerId GROUP BY CustomerId, CustomerNameHAVING COUNT(OrderID) > 5How can we find the Customers for last one week? |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2008-11-18 : 04:17:16
|
| hey, there we go! Just add:FROM ...WHERE CustomerOrder.DateOfOrder > DATEADD(dd, -7, GETDATE())GROUP BY ...- Lumbago |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-18 : 04:17:59
|
| [code]SELECT CustomerId, CustomerName, COUNT(OrderID) as totalFROM Customer INNER JOIN CustomerOrder ON Customer.CustomerId = CustomerOrder.CustomerId WHERE CustomerOrder.DateOfOrder >= DATEADD(wk,DATEDIFF(wk,0,GETDATE()),-1)AND CustomerOrder.DateOfOrder < DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0)GROUP BY CustomerId, CustomerNameHAVING COUNT(OrderID) > 5[/code] |
 |
|
|
mapidea
Posting Yak Master
124 Posts |
Posted - 2008-11-18 : 04:32:15
|
| Thanks a lot. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-18 : 04:35:47
|
Cheers |
 |
|
|
|
|
|
|
|