help query sql server 2005

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

    help query sql server 2005

    HI!

    I have 2 tables. table1(id_tab1, desc) and table2(id_tab2, id_tab1, data, type)


    Example:

    table1 (id_tab1, desc) (int, varchar(50))
    -------------------------------

    ( 1, xpta)
    ( 2, xpte)
    ( 3, xpti)


    table2 (id_tab2, id_tab1, date, type) (int , int, datetime, int)
    -----------------
    (1, 1, 10-10-2007, 1)
    (2, 1, 12-10-2007, 2)
    (1, 2, 10-10-2007, 1)
    (2, 2, 12-10-2007, 2)
    (2, 1, 12-10-2007, 3)
    (1, 3, 10-10-2007, 1)

    I need de result like: (for all id table1 i want the max date for type 1 or 2)

    (table1.id_tab1 , table2.data, tipo)
    ---------------------------------
    ( 1, 12-10-2007, 2)
    ( 2, 12-10-2007, 2)
    ( 3, 10-10-2007, 1)


    My query (don't work) :(

    SELECT table1.id_tab1, MAX( table2.data), tab2. tipo
    WHERE table2. type = '1' OR table2. type = '2'
    FROM table1 INNER JOIN
    table2 ON table2.id_tab1 = table1.id_tab1
    GROUP BY table1.id_tab1, type
    ORDER BY table1.id_tab1


    thanks.
  • almaz
    Recognized Expert New Member
    • Dec 2006
    • 168

    #2
    Originally posted by cash
    My query (don't work) :(

    SELECT table1.id_tab1, MAX(table2.data ), tab2. tipo
    WHERE table2. type = '1' OR table2. type = '2'
    FROM table1 INNER JOIN
    table2 ON table2.id_tab1 = table1.id_tab1
    GROUP BY table1.id_tab1, type
    ORDER BY table1.id_tab1
    Code:
    SELECT table1.id_tab1, MAX(table2.data), table2.type
    FROM table1 INNER JOIN
         table2 ON table2.id_tab1 = table1.id_tab1
    WHERE table2.type = 1 OR table2.type = 2
    GROUP BY table1.id_tab1, table2.type
    ORDER BY table1.id_tab1

    Comment

    Working...