| Author |
Topic |
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2010-04-27 : 14:00:54
|
| Hiwe have two tables customer and salesorderwe need to identify all customer that have not yet made any purchases and those that have only made orders with an OrderTotal less than 100please help below query is correct as per my requirement.SELECT * FROM CustomerWHERE 100 > ALL(SELECT OrderTotal FROM SalesOrderWHERE Customer.CustomerID = SalesOrder.CustomerID)Thanks |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-27 : 14:10:28
|
maybe add SUM(OrderTotal) just in case someone like me ordered 1000 times of #2 pencil for 50cents each?quote: Originally posted by WoodHouse Hiwe have two tables customer and salesorderwe need to identify all customer that have not yet made any purchases and those that have only made orders with an OrderTotal less than 100please help below query is correct as per my requirement.SELECT * FROM CustomerWHERE 100 > ALL(SELECT OrderTotal FROM SalesOrderWHERE Customer.CustomerID = SalesOrder.CustomerID)Thanks
|
 |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-04-27 : 14:48:36
|
| Just to start with:Declare @CustTable Table(CustomerId int identity, CustomerName Varchar(500))Declare @SalesOrder TAble(SOrderNo int identity, CustomerId int,OrderTotal int)Insert into @CustTable Select 'OrderAbove100' unionSelect 'OrderAbove500' unionSelect 'OrderBelow100' unionSelect 'NoOrder' Insert into @SalesOrderSelect 1 , 70 unionSelect 1 , 50 unionSelect 2 , 550 unionSelect 3 , 70 Select '0Order', CustomerId from @CustTable where CustomerId not in (Select CustomerId from @SalesOrder)UnionSelect 'LessThan100', CustomerId from @CustTable CT where 100 > (Select Sum(OrderTotal) from @SalesOrder SO where so.customerid = ct.customerid group by so.CustomerId )Regards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-28 : 05:45:42
|
| [code]SELECT CustomerID,other reqd columns...FROM(SELECT c.*,SUM(so.OrderTotal) OVER (PARTITION BY c.CustomerID) AS OrderTotFROM Customer cLEFT JOIN SalesOrder soON so.CustomerID = c.CustomerID)tWHERE OrderTot <=100[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-28 : 06:22:10
|
quote: Originally posted by visakh16
SELECT CustomerID,other reqd columns...FROM(SELECT c.*,SUM(so.OrderTotal) OVER (PARTITION BY c.CustomerID) AS OrderTotFROM Customer cLEFT JOIN SalesOrder soON so.CustomerID = c.CustomerID)tWHERE OrderTot <=100
OR OrderTot IS NULL------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-28 : 06:49:15
|
| how can SUM be NULL?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-28 : 07:06:30
|
quote: Originally posted by visakh16 how can SUM be NULL?
Because you're summing a column on the RHS of a left join. For any customers that do not have sales orders, SUM(so.OrderTotal) will return null.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-28 : 10:23:35
|
| oh ok..I see now..------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|