Results 1 to 2 of 2

Thread: group by max expiration date.

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    group by max expiration date.

    Hi.
    Code:
    SELECT  memberid, ExpirationDate, '{"Inv":"' + cast(IsInvoiced as nvarchar(1)) +'","Marc":"' + cast(AcceptMarketing as nvarchar(1)) +'","FtDate": "'+  convert(nvarchar(20),RegistrationDate,112) +' ' + convert(nvarchar(20),RegistrationDate,114) +'"}' as jsoncol 
    FROM FamilyClubRegistration where paiddate is not null and cardnumber is not null and MemberId is not null and expirationdate is not null and MemberId in ( '1', '2','3')
    and (ExpirationDate) IN (
        SELECT  MAX(ExpirationDate)
        FROM FamilyClubRegistration
        GROUP BY memberid
    )
    What I need to do is only have a row per member per max expiration date.
    The above will give me 2 correct rows and one duplicate, because an expiration date happens to be the same. I did not expected that to occur but clearly some burthead manually inserted the row and they are the same now.
    So is there another way to go about?

    Imagine for example,
    Member 1 has
    memberid, expirationdate
    1,'20230505 12:12:12'
    1,'20230506 12:11:11'

    and somewhere another member has
    memberid, expirationdate
    33, '20230506 12:11:11'

    So I expect double rows there.

    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: group by max expiration date.

    Hmm.
    I think this?

    Code:
    
    SELECT  memberid, '{"Inv":"' + cast(IsInvoiced as nvarchar(1)) +'","Marc":"' + max(cast(AcceptMarketing as nvarchar(1)) +'","FtDate": "'+  convert(nvarchar(20),RegistrationDate,112) +' ' + convert(nvarchar(20),RegistrationDate,114) +'"}') as jsoncol
    FROM FamilyClubRegistration where paiddate is not null and cardnumber is not null and MemberId is not null and expirationdate is not null and MemberId in ( 'VCNWS05NPKQG', 'VCBPN4RC83G5','VC13P00NCJGN')
    and (ExpirationDate) IN (
        SELECT  MAX(ExpirationDate)
        FROM FamilyClubRegistration
        GROUP BY memberid
    )
    group by memberid,IsInvoiced,AcceptMarketing
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width