By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

[help SQL SERVER] Query

P: 8
Hi!

I have a table like this:

name | type | date

xpto , 1 , 10-10-2006
xpto , 2 , 15-10-2006
xpto , 2 , 16-10-2006
xpto , 1 , 08-10-2006
xpto , 2 , 12-10-2006
xpto , 2 , 11-10-2006
mark , 1 , 10-10-2006
mark , 2 , 15-10-2006
mark , 2 , 16-10-2006
mark , 1 , 13-10-2006
mark , 2 , 20-10-2006
mark , 2 , 16-10-2006


I need the results like:
(where date type 1 is -> (max date) of xpto of type 1)
(where date type 2 is -> (max date) of xpto of type 2)
...

Name | date type 1 | date type 2
-------------------------------------------------------
xpto | 10-10-2006 | 16-10-2006
Mark | 13-10-2006 | 20-10-2006


anyone can help me please?


thanks
Apr 11 '07 #1
Share this Question
Share on Google+
2 Replies


iburyak
Expert 100+
P: 1,017
Try this example:

Expand|Select|Wrap|Line Numbers
  1. declare @MyTable table (
  2. name varchar(50),
  3. type int, 
  4. date datetime)
  5.  
  6. insert into @MyTable values('xpto' , 1 , '10-10-2006')
  7. insert into @MyTable values('xpto' , 2 , '10-15-2006')
  8. insert into @MyTable values('xpto' , 2 , '10-16-2006 ')
  9. insert into @MyTable values('xpto' , 1 , '10-08-2006')
  10. insert into @MyTable values('xpto' , 2 , '10-12-2006')
  11. insert into @MyTable values('xpto' , 2 , '10-11-2006')
  12. insert into @MyTable values('mark' , 1 , '10-10-2006')
  13. insert into @MyTable values('mark' , 2 , '10-15-2006')
  14. insert into @MyTable values('mark' , 2 , '10-16-2006')
  15. insert into @MyTable values('mark' , 1 , '10-13-2006')
  16. insert into @MyTable values('mark' , 2 , '10-20-2006')
  17. insert into @MyTable values('mark' , 2 , '10-16-2006')
  18.  
  19. select name, max(case when type = 1 then date else null end),
  20. max(case when type = 2 then date else null end)
  21. from @MyTable
  22. group by name

Good Luck.
Apr 11 '07 #2

P: 8
BIG THANKS iburyak ;)

It works!





Bye
Apr 12 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.