T-SQL Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • klabu

    T-SQL Question

    How to write an equivalent SQL like this (which works on Oracle):

    SELECT * FROM sometable
    WHERE rec_id, rec_sequence
    IN (SELECT rec_id, MAX( rec_sequence) FROM alppremh GROUP BY rec_id)

    thanks


  • klabu

    #2
    Re: T-SQL Question

    >
    SELECT * FROM sometable
    WHERE rec_id, rec_sequence
    IN (SELECT rec_id, MAX( rec_sequence) FROM alppremh GROUP BY rec_id)
    >
    thanks
    >
    edit:

    SELECT * FROM sometable
    WHERE rec_id, rec_sequence
    IN (SELECT rec_id, MAX( rec_sequence) FROM alppremh GROUP BY rec_id)
    and rec_id = 4883493


    Comment

    • Aaron Bertrand [SQL Server MVP]

      #3
      Re: T-SQL Question

      SELECT s.*
      FROM sometable s
      INNER JOIN
      (
      SELECT rec_id,
      rs = MAX(rec_sequenc e)
      FROM alppremh
      GROUP BY rec_id
      ) a
      ON s.rec_id = a.rec_id
      AND s.rec_sequence = a.rs;





      "klabu" <noone_at_gmail _dot_comwrote in message
      news:12lf5tld76 k2o21@corp.supe rnews.com...
      How to write an equivalent SQL like this (which works on Oracle):
      >
      SELECT * FROM sometable
      WHERE rec_id, rec_sequence
      IN (SELECT rec_id, MAX( rec_sequence) FROM alppremh GROUP BY rec_id)
      >
      thanks
      >

      Comment

      • Steve Kass

        #4
        Re: T-SQL Question

        [responding only to comp.databases. ms-sqlserver, due to newsreader limitations]

        Another option, probably more efficient in most cases, is

        select * from sometable
        where rec_sequence = (
        select max(rec_sequenc e)
        from alppremh
        where rec_id = 4883493
        )
        and rec_id = 4883493

        Steve Kass
        Drew University
        Cheez-It BIG crackers are bigger than Cheez-Its, and a serving contains fewer crackers - 14 instead of 27. While 14 is not a perfect cube, it is the sum of consecutive perfect squares, which is nearly as wonderful.


        klabu wrote:
        How to write an equivalent SQL like this (which works on Oracle):
        >
        SELECT * FROM sometable
        WHERE rec_id, rec_sequence
        IN (SELECT rec_id, MAX( rec_sequence) FROM alppremh GROUP BY rec_id)
        and rec_id = 4883493
        >
        >

        Comment

        Working...