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

  • SEARCH
  • Home
  • 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 7190303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:29:07+00:00 2026-05-28T19:29:07+00:00

I am new to SQL and trying to achieve something. I have a table

  • 0

I am new to SQL and trying to achieve something.

I have a table having fields like:

CustId   Fname   Lname    Address1    Address2  Zip    State  LoadDate   Flag
1        John     Leo      xxx        xxx       34532    VA    1/1/2012   Y
2        Mike     Saunder  xxx        xxx       94090    CA    1/1/2012   Y

Everytime I insert a new record it will set its flag to Y but lets say if any of the record value changes then new one will be set to Flag Y and old one to N. So for example if Mikes address1 is changed.

CustId   Fname   Lname    Address1    Address2  Zip    State  LoadDate   Flag
1        John     Leo      xxx        xxx       34532    VA    1/1/2012   Y
2        Mike     Saunder  xxx        xxx       94090    CA    1/1/2012   N
3        Mike     Saunder  rrr        xxx       94090    CA    2/1/2012   Y
  • 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-28T19:29:07+00:00Added an answer on May 28, 2026 at 7:29 pm

    Assuming that you’re trying to say that the Fname/Lname pair is a unique key that lets you match rows:

    declare @Folks as table ( CustId int, Fname varchar(10), Lname varchar(10),
      Address1 varchar(10), Address2 varchar(10),
      Zip varchar(9), State varchar(2), LoadDate date, Flag char(1) )
    insert into @Folks ( CustId, Fname, Lname, Address1, Address2, Zip, State, LoadDate, Flag ) values
      ( 1, 'John', 'Leo', 'xxx', 'xxx', '34532', 'VA', '1/1/2012', '-' ),
      ( 2, 'Mike', 'Saunder', 'xxx', 'xxx', '94090', 'CA', '1/1/2012', '-' ),
      ( 3, 'Mike', 'Saunder', 'rrr', 'xxx', '94090', 'CA', '2/1/2012', '-' )
    
    -- Before setting the Flag.
    select * from @Folks
    
    -- Computing a flying flag rather than using a stored value.
    select *, case when LoadDate = ( select MAX(LoadDate) from @Folks as S where S.Fname = O.Fname and S.Lname = O.Lname ) then 'Y' else 'N' end as 'FlyingFlag'
      from @Folks as O
    
    -- Updating all stored values.  Every bloody row.
    update @Folks
      set Flag = case when LoadDate = ( select MAX(LoadDate) from @Folks as S where S.Fname = O.Fname and S.Lname = O.Lname ) then 'Y' else 'N' end
      from @Folks as O
    
    -- Showing the result.
    select * from @Folks
    

    EDIT:
    I still think this is not the direction you want to be heading. For example, it doesn’t gracefully accomodate more than one update per customer per day unless you rely on the CustId to be a tie breaker.

    A marginally better solution would be to replace Flag with a PreviousId field that is NULL in the first row for a customer and in later rows contains the CustId of the row that is being superseded. That links together the changes for each customer and provides a well defined order in the event that a customer is updated repeatedly on a single date.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an SQL table entitled Facility which looks like this: || FacilityID
I am trying to use the new DATETIMEOFFSET data type in SQL 2008 but
What's up with JNDI names? I'm trying to get a javax.sql.DataSource using the new
I'm looking mainly at things like new SQL syntax, new kinds of locking, new
I would like to be able to add a new SQL LOGIN and name
I am totally new to SQL . I have a simple select query similar
Running SQL Server 2008, and I am definitely a new SQL user. I have
I'm very new to SQL so forgive my ineptitude. I'm trying to write a
I am trying to write a SQL Server query but have had no luck
Suppose I have a myTest.sql scripts, which contains thousands of create table blahblah statements.

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.