Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7440833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:54:46+00:00 2026-05-29T10:54:46+00:00

I have a consumer table which has the columns – Email, AccountState and DateCreated

  • 0

I have a consumer table which has the columns – Email, AccountState and DateCreated

AccountState can have the values 1 (active), 2 (inactive) and 3 (archived)

A specific consumer can have multiple rows consisting of the account state’s above.

What I am trying to do is construct a query which returns the following

A. A list of consumer records for each consumer (using email address)

B. Only the records which aren’t the most recent (so if a specific email address has 3 records, 1 for each state, it would return the 2 which aren’t the most recent)

Then once I have this list I want to set all these states to 3 as they need to be archived.

So for the example data shown here

enter image description here

Only rows 13 – 16 would be returned.

I have tried to do this using the query below but it isn’t working.

SELECT                  con.Email,
                        con.Id, 
                        con.DateCreated                 AS DateRegistered,
                        con.DateLastActivity,
                        con.hasiPhone,
                        con.hasAndroid,
                        con.hasSMS,
                        con.CurrencyCode                AS Currency,
                        con.AccountState


        FROM            Consumer                        con

        WHERE           con.AccountState                <> 1
        AND             DateCreated     =(  SELECT  MAX(DateCreated)
                            FROM    Consumer                        con_most_recent
                            WHERE   con_most_recent.AccountState    <> 1
                            AND     con_most_recent.Id              = con.Id)

        order by Email asc
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:54:47+00:00Added an answer on May 29, 2026 at 10:54 am
    ;WITH x AS 
    (
      SELECT rn = ROW_NUMBER() OVER (PARTITION BY EMail ORDER BY DateCreated DESC),
        Email, Id, DateCreated AS DateRegistered --, ... other columns
      FROM dbo.Consumer
      WHERE AccountState <> 1
    )
    SELECT Email, Id, DateRegistered --, ... other columns
    FROM x
    WHERE rn > 1
    ORDER BY Email;
    

    EDIT changing state for these rows

    ;WITH x AS 
    (
      SELECT rn = ROW_NUMBER() OVER (PARTITION BY EMail ORDER BY DateCreated DESC),
        Email, Id, DateCreated AS DateRegistered --, ... other columns
      FROM dbo.Consumer
      WHERE AccountState <> 1
    )
    UPDATE x 
    SET AccountState = 3
    WHERE rn > 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table, which contains many columns of float data type with 15
Classic producer-consumer-problem. I have x app servers which write records in a DB table
I have an online form which collects consumer data and stores in a dedicated
I have a JTable using a custom DefaultTableModel which has some Booleans in the
Morning folks, I have a temporary table with 135,000 rows and 24 columns, of
I have an application that receives files with a flat table in DBF, which
I have an SQL database, which is a feeder table. I put records in
Surprisingly can't find a match for my question. I have one table that I
I have a JTable which is loaded from a data-structure using table model.The data-structure
I have a simple producer/consumer scenario, where there is only ever a single item

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.