Sql 使用其他列的最小日期检查列的值

Sql 使用其他列的最小日期检查列的值,sql,sql-server,join,Sql,Sql Server,Join,我有一张桌子 id | guid | Status ------ | -----| ---------- 1 | 123 | 2 2 | 456 | 2 3 | 789 | 2 另一张桌子是 id. | modified date | Status ------ | ----------------------- | ---------- 1 | 26-08-2017 04:05

我有一张桌子

  id     | guid | Status 
  ------ | -----| ----------
  1      | 123  | 2
  2      | 456  | 2 
  3      | 789  | 2
另一张桌子是

 id.    | modified date           | Status 
 ------ | ----------------------- | ----------
 1      | 26-08-2017 04:05        | 0
 1      | 26-08-2017 10:50        | 0
 1      | 01-09-2017 02:03        | 0
 1      | 02-09-2017 13:43        | 2
 2      | 26-08-2017  03:04       | 0
 2      | 26-08-2017  11:04       | 2
 2      | 02-09-2017  18:03       | 2
 3      | 01-09-2017  03:45       | 0
 3      | 01-09-2017  12:04       | 0
 3      | 03-09-2017  17:08       | 2
在第一个表中,只要状态值发生变化,它就会记录在另一个表中

我想要那些状态值在该事务的第一个savedi.e min date时保持为0的ID

Id 1在26日第一次保存,然后一整天它的值保持为0。我必须只检查第一个日期。对于Id 2在26日它被保存为0,然后同一天更改为2。因此在这种情况下它不会出现。对于Id 3,它在第一个日期也保持为0

所需的o/p将是 身份证件 1.
3

您可以使用如下查询

select t1.id 
from table2 t1 
inner join
(
select id, 
min(modifieddate) over (partition by id) startDateTime,
max(Status) over (partition by id,cast(modifieddate as date)) maxStatusOnStartDay
from table2
)t2
on t1.id=t2.id and 
cast(t1.modifieddate as date)=cast(t2.startDateTime as date)
and t2.maxStatusOnStartDay=0
group by t1.id
请尝试以下查询:

select id from (
    select id,
       CAST(modified_date as date) as [modified_date],
       [status],
       MIN(CAST(modified_date as date)) over (partition by id) as [first_saved]
    from TABLE_NAME
) as A where modified_date = first_saved
group by id
having SUM([status]) = 0

该示例数据的当前格式不可读。你能试着重新格式化它吗?你能帮我解决这个问题吗