[help SQL SERVER] Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cash
    New Member
    • Apr 2007
    • 8

    [help SQL SERVER] Query

    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
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    Try this example:

    Code:
    declare @MyTable table (
    name varchar(50),
    type int, 
    date datetime)
    
    insert into @MyTable values('xpto' , 1 , '10-10-2006')
    insert into @MyTable values('xpto' , 2 , '10-15-2006')
    insert into @MyTable values('xpto' , 2 , '10-16-2006 ')
    insert into @MyTable values('xpto' , 1 , '10-08-2006')
    insert into @MyTable values('xpto' , 2 , '10-12-2006')
    insert into @MyTable values('xpto' , 2 , '10-11-2006')
    insert into @MyTable values('mark' , 1 , '10-10-2006')
    insert into @MyTable values('mark' , 2 , '10-15-2006')
    insert into @MyTable values('mark' , 2 , '10-16-2006')
    insert into @MyTable values('mark' , 1 , '10-13-2006')
    insert into @MyTable values('mark' , 2 , '10-20-2006')
    insert into @MyTable values('mark' , 2 , '10-16-2006')
    
    select name, max(case when type = 1 then date else null end),
    max(case when type = 2 then date else null end)
    from @MyTable
    group by name

    Good Luck.

    Comment

    • cash
      New Member
      • Apr 2007
      • 8

      #3
      BIG THANKS iburyak ;)

      It works!





      Bye

      Comment

      Working...