Sunday, August 11, 2013

[T-SQL] Stumped on Query

[T-SQL] Stumped on Query


Stumped on Query

Posted: 10 Aug 2013 12:45 PM PDT

I have two tables with the following columns:[b]Table 1[/b]customer_id123456[b]Table 2[/b]id, customer_id, product, date1, 1, A, 1/1/20132, 1, A, 5/1/20133, 1, A, 6/1/20134, 1, C, 6/1/20135, 2, B, 7/1/20136, 3, C, 8/1/20137, 3, A, 9/1/2013[b]What I need is the following: [/b]I need to find the maximum date for each customer from Table 2. If there is a tie for the maximum date (2 or more), I need to display all the records that are tied. Also if there is more than one record I need to add an instance field to the results that will count up the number of instances (1, 2, 3, etc). If there is only one returned result from Table 2 for the customer, the instance would default to 1.[b]Results table[/b]customer_id, date, product, instance1, 6/1/2013, A, 11, 6/1/2013, C, 22, 7/1/2013, B, 13, 8/1/2013, A, 1I hope this is clear. I cannot do the max function it appears because it only shows one record and I cannot figure out how to get the instance column.Thank you in advance and let me know if I can help to clear anything up that may not be clear here.

No comments:

Post a Comment

Search This Blog