Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL选择问题_Sql_Sql Server - Fatal编程技术网

SQL选择问题

SQL选择问题,sql,sql-server,Sql,Sql Server,我有一个包含以下列和值的表 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 从上表中,我想从205年5月21日选择123,从20

我有一个包含以下列和值的表

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
从上表中,我想从205年5月21日选择123,从2015年5月21日选择456。我不想从5/20中选择nbr 123,因为一天结束时OldValue和NewValue没有变化

如何为这种需求编写select语句

SELECT * FROM YOURTABLE AS T1
INNER JOIN YOURTABLE AS T2 ON T1.NBR=T2.NBR AND T1.OLDVALUE<>T2.NEWVALUE

将您的表与其自身连接起来

我对这个问题的理解是您只想选择包含新数据的行

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;

使用where not exists子句:

从[表]a中选择日期、Nbr
如果不存在,请从[Table]b中选择*,其中a.nbr=b.nbr和b.newvalue=a.oldvalue

如果您至少使用SQL Server 2008,则可以使用CTE来确定每天哪个条目代表每个[nbr]的第一个条目,哪个条目代表最后一个条目,然后比较这两种方法,看看在什么地方发生了实际的变化,就像其他答案中所建议的那样,使用自连接。例如:

-- 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];

您的数据是否也有时间部分分钟等其他信息?如何实际确定相同日期和nbr的行顺序?嗯。。仅使用日期而不使用时间,前两行的顺序将无法保证。。。还应该有某种类型的标识栏,这样您就可以正确地整理出所需的结果这里的nbr是什么?如何知道哪一行对应于一天的结束??为什么456包括5/21,而123不包括最初的5/20?还有,这是每天的变化吗?还是你想要的整体变化?@JamesZ我的桌子也有部分时间。你能告诉我现在如何确定顺序吗?如果你有add和T1.IDT2.ID,你的表中有主键吗