SQL query to pull only the highest seq# in each month

Discussion in 'General MSSQL Discussion' started by SkeerNC, Apr 19, 2021.

  1. SkeerNC

    SkeerNC

    Joined:
    Apr 19, 2021
    Messages:
    2
    Likes Received:
    0
    My query is returning the following information. See attachment. sql.png
    I need to ONLY see the row with the 'highest' Sequence No. for each Month.
    Can anyone assist me?

    SELECT (sh.trn_seq_no) AS Seq, sh.new_bal, YEAR(sh.trn_dt) AS Yr, MONTH(sh.trn_dt) AS Mth,Day(sh.trn_dt) as Day
    FROM SH_HIST sh INNER JOIN
    MEMBER_ACCT m ON sh.acn = m.acn
    WHERE (m.mbr_ssn_no ='xxxxxxxxx')
    AND
    (sh.sh_sfx = '9') AND (sh.trn_cd <> '0') AND (sh.trn_cd <> 'MEMO') AND (sh.trn_cd NOT LIKE '*%')
    GROUP BY YEAR(sh.trn_dt), MONTH(sh.trn_dt),sh.trn_seq_no,sh.new_bal,sh.trn_dt
    ORDER By Yr DESC, Mth DESC, Seq DESC
     
    SkeerNC, Apr 19, 2021
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.