Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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,我需要查询一个具有以下格式的表,并期望得到所提供的结果。由于ID验证和注册的状态是在同一天,所以应该忽略它,并返回其他ID 表1 ID Status Date 123 Validated 2/3/2016 123 Registered 2/3/2016 234 Validated 2/5/2016 234 Registered 2/7/2016 345 Validated 2/6/2016 456 Registered 2/9/2016 结果 ID Status Da

我需要查询一个具有以下格式的表,并期望得到所提供的结果。由于ID验证和注册的状态是在同一天,所以应该忽略它,并返回其他ID

表1

ID  Status  Date
123 Validated   2/3/2016
123 Registered  2/3/2016
234 Validated   2/5/2016
234 Registered  2/7/2016
345 Validated   2/6/2016
456 Registered  2/9/2016
结果

ID  Status  Date
234 Registered  2/7/2016
456 Registered  2/9/2016

嗯。一种方法使用窗口函数:

select t.*
from (select t.*, min(status) over (partition by id, date) as mins,
             max(status) over (partition by id, date) as maxs
      from t
     ) t
where mins = maxs;

这将返回每个日期只有一个状态的记录

Hmmm。一种方法使用窗口函数:

select t.*
from (select t.*, min(status) over (partition by id, date) as mins,
             max(status) over (partition by id, date) as maxs
      from t
     ) t
where mins = maxs;

这将返回每个日期只有一个状态的记录

这里有一个选项,使用连接和一些样本数据进行测试:

create table #table1 (id int, stat varchar(20), dt date)

insert into #table1 values(123 ,'Validated',   '2/3/2016')
insert into #table1 values(123 ,'Registered',  '2/3/2016')
insert into #table1 values(234 ,'Validated',   '2/5/2016')
insert into #table1 values(234 ,'Registered',  '2/7/2016')
insert into #table1 values(345 ,'Validated',   '2/6/2016')
insert into #table1 values(456 ,'Registered',  '2/9/2016')

Select a.*
from #table1 a
left join #table1 b
on a.id = b.id
    and a.dt = b.dt
    and b.stat <> 'Registered'
where a.stat = 'Registered' and b.stat is null

这里有一个选项,使用连接和一些示例数据进行测试:

create table #table1 (id int, stat varchar(20), dt date)

insert into #table1 values(123 ,'Validated',   '2/3/2016')
insert into #table1 values(123 ,'Registered',  '2/3/2016')
insert into #table1 values(234 ,'Validated',   '2/5/2016')
insert into #table1 values(234 ,'Registered',  '2/7/2016')
insert into #table1 values(345 ,'Validated',   '2/6/2016')
insert into #table1 values(456 ,'Registered',  '2/9/2016')

Select a.*
from #table1 a
left join #table1 b
on a.id = b.id
    and a.dt = b.dt
    and b.stat <> 'Registered'
where a.stat = 'Registered' and b.stat is null

另一种方法是通过子查询,如:

select t.*
from tbl t
where Status = 'Registered'
and Date not in (
  select date from tbl
  where Status = 'Validated'
)

另一种方法是通过子查询,如:

select t.*
from tbl t
where Status = 'Registered'
and Date not in (
  select date from tbl
  where Status = 'Validated'
)
与aph+1基本相同


与aph+1基本相同

请提供获得结果所使用的精确算法,即,如果你为某人手工编写指令,它们会是什么样子?呃,删除了我的答案,因为我现在看到,这有很多变量。我认为海因茨是正确的,我们需要更多的样本数据来提供参数。我们是否可以推断ID中没有匹配项,而您确实希望看到已注册但未验证的状态?请提供获得结果所使用的确切算法,即,如果您为某人手工编写指令,他们会是什么样子?呃,删除了我的答案,因为我现在看到有很多变量。我认为海因茨是正确的,我们需要更多的样本数据来提供参数。我们是否可以推断ID中没有匹配项,而您确实希望看到已注册但未验证的状态?