0

I have a table with the columns and values below

Date                 Nbr   NewValue OldValue
5/20/2015 14:23:08    123   abc      xyz
5/20/2015 15:02:10    123   xyz      abc
5/21/2015 08:10:02    123   xyz      pqr
5/21/2015 10:10:05    456   lmn      ijk

From the above table i want to select 123 from 5/21/205 and 456 from 5/21/2015. I don't want to select nbr 123 from 5/20 because there is no change in OldValue and NewValue at the end of the day.

How to write select statement for this kind of requirement.

5
  • 3
    Does your data also have the time part (minutes etc) or something else how to actually determine the order of the rows for same date and nbr?
    – James Z
    May 26, 2015 at 18:37
  • Hmm.. Just by using the date (without time) the order of your first two rows will not be guaranteed... there should also be some kind of identity column, so that you can correctly sort out the result you need May 26, 2015 at 18:37
  • What is nbr here? How to know which row corresponds to end of day?? May 26, 2015 at 18:44
  • Why is 5/21 for 456 included and not the initial 5/20 for 123? Also, is it a change on a daily basis? or is it a change overall that you're looking for?
    – xQbert
    May 26, 2015 at 18:49
  • @JamesZ My table has time part too. Can you tell me how to determine order now please.
    – user28455
    May 26, 2015 at 19:22

4 Answers 4

0

If you're using at least SQL Server 2008, you can use a CTE to determine which entry represents the first entry for each [Nbr] on each day and which entry represents the last, then compare the two to see where an actual change has occurred using a self-join as suggested in some other answers. For instance:

-- Sample data from the question.
declare @TestData table ([Date] datetime, [Nbr] int, [NewValue] char(3), [OldValue] char(3));
insert @TestData values
    ('2015-05-20 14:23:08', 123, 'abc', 'xyz'),
    ('2015-05-20 15:02:10', 123, 'xyz', 'abc'),
    ('2015-05-21 08:10:02', 123, 'xyz', 'pqr'),
    ('2015-05-21 10:10:05', 456, 'lmn', 'ijk');

with [SequencingCTE] as
(
    select *,
        -- [OrderAsc] will be 1 if and only if a record represents the FIRST change 
        -- for a given [Nbr] on a given day.
        [OrderAsc] = row_number() over (partition by convert(date, [Date]), [Nbr] order by [Date]),

        -- [OrderDesc] will be 1 if and only if a record represents the LAST change
        -- for a given [Nbr] on a given day.
        [OrderDesc] = row_number() over (partition by convert(date, [Date]), [Nbr] order by [Date] desc)
    from
        @TestData
)

-- Match the original value for each [Nbr] on each day with the final value of the
-- same [Nbr] on the same day, and get only those records where an actual change
-- has occurred.
select
    [Last].*
from
    [SequencingCTE] [First]
    inner join [SequencingCTE] [Last] on
        convert(date, [First].[Date]) = convert(date, [Last].[Date]) and
        [First].[Nbr] = [Last].[Nbr] and
        [First].[OrderAsc] = 1 and
        [Last].[OrderDesc] = 1
where
    [First].[OldValue] != [Last].[NewValue];
0
0
SELECT * FROM YOURTABLE AS T1
INNER JOIN YOURTABLE AS T2 ON T1.NBR=T2.NBR AND T1.OLDVALUE<>T2.NEWVALUE

inner join your table with itself

1
  • Do you have primary key in your table if you have just add "AND T1.ID<>T2.ID" it will work May 26, 2015 at 19:23
0

My understanding of the question is you want to only select the rows with new data

IF TABLE1.OLDVALUE <> TABLE2.OLDVALUE;
BEGIN
SELECT DISTINCT NBR
FROM [TABLENAME] AS TABLE1
INNER JOIN [TABLENAME] AS TABLE2 ON TABLE1.NBR = TABLE2.NBR
WHERE DATE = '5/21/2015' -- I recommend using sysdate here but your choice
END;
0

Use a where not exists clause:

select "Date",Nbr from [Table] a where not exists (select * from [Table] b where a.nbr = b.nbr and b.newvalue = a.oldvalue)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.