364,111 Members | 2129 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Nested Query

ddtpmyra
100+
P: 320
Hello,
I dont know how to write this thought to a query statement but heres the idea.

I have list of orders of customer, now I want a flag that will tell me this is a first time order.

Expand|Select|Wrap|Line Numbers
  1. Table_Customer
  2. Name            Address 
  3. customer 1  454 B St. Brooklyn
  4. customer 2  454 Hyde St. San Francisco
  5. customer 3  445 Way Ln Michigan
  6.  
  7. table_history_order
  8. Name            item_order      qty     date_order
  9. customer 1      short           250     01/02/2012
  10. customer 1      shirt           250     01/03/2012
  11. customer 2      pants           250     01/04/2012
  12.  
  13. From the table above my query result should be
  14. Name       date_order     first_order
  15. customer1  01/03/2012     N
  16. customer2  01/04/2012     Y
I started my query like this but I dont know how to add the flag that will tell me that is a first or not firt customer's order.

Expand|Select|Wrap|Line Numbers
  1. Select name, date_order, 
  2. first_order 
  3. Case 
  4.   WHEN select count(*) from table_history_order =>2 'i dont know how to write the where condition that will link to the custmer name outside this nested query. 
  5.   THEN first_order 'Y' 
  6.   WHEN select count(*) from table_history_order <=1 
  7.   THEN first_order 'N'
  8. END as first_order
  9. from cutomer c left join customer_order co on c.name = co.name
Feb 17 '12 #1

✓ answered by yarbrough40

Use a subquery.
try this:
Expand|Select|Wrap|Line Numbers
  1. SELECT Table_Customer.[Name],th.date_order,CASE WHEN th.name_count = 1 THEN 'Y' ELSE 'N' END AS first_order 
  2. FROM  Table_Customer
  3. LEFT JOIN(SELECT [Name],  MAX(date_order) as date_order, COUNT([NAME]) as name_count 
  4. FROM table_history_order GROUP BY [Name])th
  5. ON th.[Name] = Table_Customer.Name
  6.  
  7.  
Share this Question
Share on Google+
3 Replies


yarbrough40
100+
P: 290
Use a subquery.
try this:
Expand|Select|Wrap|Line Numbers
  1. SELECT Table_Customer.[Name],th.date_order,CASE WHEN th.name_count = 1 THEN 'Y' ELSE 'N' END AS first_order 
  2. FROM  Table_Customer
  3. LEFT JOIN(SELECT [Name],  MAX(date_order) as date_order, COUNT([NAME]) as name_count 
  4. FROM table_history_order GROUP BY [Name])th
  5. ON th.[Name] = Table_Customer.Name
  6.  
  7.  
Feb 19 '12 #2

NeoPa
Expert Mod 15k+
P: 20,530
Why is the data from line #9 not reflected in your output? I would have expected to see that with a Y for [First_Order].
Feb 20 '12 #3

NeoPa
Expert Mod 15k+
P: 20,530
I would expect something like the following should work assuming the question should have been what I think it should :
Expand|Select|Wrap|Line Numbers
  1. SELECT   tHO1.Name
  2.        , tHO1.Item_Order
  3.        , tHO1.Qty
  4.        , tHO1.Date_Order
  5.        , Max(CASE WHEN tHO2.Name Is Null THEN 'Y' ELSE 'N' END) AS [First_Order]
  6. FROM     [History_Order] AS [tHO1]
  7.          LEFT JOIN
  8.          [History_Order] AS [tHO2]
  9.   ON     (tHO1.Name = tHO2.Name)
  10.  AND     (tHO1.Date_Order > tHO2.Date_Order)
  11. GROUP BY tHO1.Name
  12.        , tHO1.Item_Order
  13.        , tHO1.Qty
  14.        , tHO1.Date_Order
Feb 20 '12 #4

Post your reply

Help answer this question



Didn't find the answer to your Microsoft SQL Server question?

You can also browse similar questions: Microsoft SQL Server