Sql 仅使用“最长日期”条目加入

Sql 仅使用“最长日期”条目加入,sql,sql-server,Sql,Sql Server,表名=t1 ID | FileID | Status 1 | 3 | NO 3 | 5 | YES 4 | 3 | NO 表名=T2 ID | FileID | From stage| Date | Remark 1 | 3 | 14 | 20/03/2015 | file is submitted 2 | 3 | 14 | 21/03/2015 | file is not completed

表名=t1

ID | FileID | Status
1  |  3     | NO
3  |  5     | YES
4  |  3     | NO
表名=T2

ID | FileID | From stage| Date       | Remark
1  |  3     | 14        | 20/03/2015 | file is submitted
2  |  3     | 14        | 21/03/2015 | file is not completed
3  |  5     | 14        | 20/03/2015 | file is submitted
仅当文件id没有“是”状态时,我才需要最新日期项的T2表格详细信息

使用以下查询

select *
from (select distinct(t1.FileID)
      from t1
      where t1.FileID not in (select FileID from t1 where Status='YES')
     ) c1 ,t2 
where t2.FileID=c1.FileID and t2.FromStageID='14
order by c1.FileID
取得成果

FIleID | ID | FileID | From stage| Date       | Remark
3      | 1  |  3     | 14        | 20/03/2015 | file is submitted
3      | 2  |  3     | 14        | 21/03/2015 | file is not completed
我想要什么

FIleID | ID | FileID | From stage| Date       | Remark
3      | 2  |  3     | 14        | 21/03/2015 | file is not completed

如果您想要最新的内容,请考虑
row\u number()
,然后根据条件获取第一个

下面使用
left join
而不是
not in
来查找
fileid
s,它们在
t1
中没有适当的匹配项。这更安全,因为
notin
NULL
值的行为不直观

此外,切勿在
from
子句中使用逗号。这是一条简单的规则。始终使用显式
join
语法

select t.*
from (select t2.*,
             row_number() over (partition by fileid order by date desc) as seqnum
      from t2 left join
           t1
           on t2.fileid = t1.fileid and t1.status = 'YES'
      where t1.fileid is null
     ) t
where seqnum = 1;

如果您想要最新的内容,请考虑
row\u number()
,然后根据条件获取第一个

下面使用
left join
而不是
not in
来查找
fileid
s,它们在
t1
中没有适当的匹配项。这更安全,因为
notin
NULL
值的行为不直观

此外,切勿在
from
子句中使用逗号。这是一条简单的规则。始终使用显式
join
语法

select t.*
from (select t2.*,
             row_number() over (partition by fileid order by date desc) as seqnum
      from t2 left join
           t1
           on t2.fileid = t1.fileid and t1.status = 'YES'
      where t1.fileid is null
     ) t
where seqnum = 1;

如果您想要最新的内容,请考虑
row\u number()
,然后根据条件获取第一个

下面使用
left join
而不是
not in
来查找
fileid
s,它们在
t1
中没有适当的匹配项。这更安全,因为
notin
NULL
值的行为不直观

此外,切勿在
from
子句中使用逗号。这是一条简单的规则。始终使用显式
join
语法

select t.*
from (select t2.*,
             row_number() over (partition by fileid order by date desc) as seqnum
      from t2 left join
           t1
           on t2.fileid = t1.fileid and t1.status = 'YES'
      where t1.fileid is null
     ) t
where seqnum = 1;

如果您想要最新的内容,请考虑
row\u number()
,然后根据条件获取第一个

下面使用
left join
而不是
not in
来查找
fileid
s,它们在
t1
中没有适当的匹配项。这更安全,因为
notin
NULL
值的行为不直观

此外,切勿在
from
子句中使用逗号。这是一条简单的规则。始终使用显式
join
语法

select t.*
from (select t2.*,
             row_number() over (partition by fileid order by date desc) as seqnum
      from t2 left join
           t1
           on t2.fileid = t1.fileid and t1.status = 'YES'
      where t1.fileid is null
     ) t
where seqnum = 1;

传统上,SQL Server的
交叉应用版本:

SELECT 
    tt.*
FROM
  T1
cross apply (
SELECT TOP 1 *
from t2
WHERE t2.fileId = t1.FileId
ORDER BY t2.date DESC
) TT
WHERE T1.status = 'YES'
这将产生最佳性能,同时更难理解,并且需要学习新的操作员(如果还没有)


编辑:虽然下面的查询产生了一个更糟糕的执行计划(仍然不是很糟糕),但它更容易阅读。对于SQL Server的传统上为
交叉应用版本的表,仍然可以很好地执行:

SELECT 
    tt.*
FROM
  T1
cross apply (
SELECT TOP 1 *
from t2
WHERE t2.fileId = t1.FileId
ORDER BY t2.date DESC
) TT
WHERE T1.status = 'YES'
这将产生最佳性能,同时更难理解,并且需要学习新的操作员(如果还没有)


编辑:虽然下面的查询产生了一个更糟糕的执行计划(仍然不是很糟糕),但它更容易阅读。对于SQL Server的传统上为
交叉应用版本的表,仍然可以很好地执行:

SELECT 
    tt.*
FROM
  T1
cross apply (
SELECT TOP 1 *
from t2
WHERE t2.fileId = t1.FileId
ORDER BY t2.date DESC
) TT
WHERE T1.status = 'YES'
这将产生最佳性能,同时更难理解,并且需要学习新的操作员(如果还没有)


编辑:虽然下面的查询产生了一个更糟糕的执行计划(仍然不是很糟糕),但它更容易阅读。对于SQL Server的传统上为
交叉应用版本的表,仍然可以很好地执行:

SELECT 
    tt.*
FROM
  T1
cross apply (
SELECT TOP 1 *
from t2
WHERE t2.fileId = t1.FileId
ORDER BY t2.date DESC
) TT
WHERE T1.status = 'YES'
这将产生最佳性能,同时更难理解,并且需要学习新的操作员(如果还没有)


编辑:虽然下面的查询产生了一个更糟糕的执行计划(仍然不是很糟糕),但它更容易阅读。对于具有另一个原因的表,仍然可以很好地执行。您可以使用
CommonTableExpression
ROW\u NUMBER()
来实现此结果:

试试这个:

查询:

;with cte as
(
select t1.FieldId,t2.ID,t2.From_Stage,t2.date1,t2.REmarks,
ROW_NUMBER() Over(PARTITION  by t2.FieldId order by t2.date1 desc) as rk
from t1,T2  t2 where t1.FieldId=t2.FieldId and t1.Status!='YES'
)

select * from cte where rk=1

另一个原因是,您可以使用
CommonTableExpression
ROW\u NUMBER()
来实现此结果:

试试这个:

查询:

;with cte as
(
select t1.FieldId,t2.ID,t2.From_Stage,t2.date1,t2.REmarks,
ROW_NUMBER() Over(PARTITION  by t2.FieldId order by t2.date1 desc) as rk
from t1,T2  t2 where t1.FieldId=t2.FieldId and t1.Status!='YES'
)

select * from cte where rk=1

另一个原因是,您可以使用
CommonTableExpression
ROW\u NUMBER()
来实现此结果:

试试这个:

查询:

;with cte as
(
select t1.FieldId,t2.ID,t2.From_Stage,t2.date1,t2.REmarks,
ROW_NUMBER() Over(PARTITION  by t2.FieldId order by t2.date1 desc) as rk
from t1,T2  t2 where t1.FieldId=t2.FieldId and t1.Status!='YES'
)

select * from cte where rk=1

另一个原因是,您可以使用
CommonTableExpression
ROW\u NUMBER()
来实现此结果:

试试这个:

查询:

;with cte as
(
select t1.FieldId,t2.ID,t2.From_Stage,t2.date1,t2.REmarks,
ROW_NUMBER() Over(PARTITION  by t2.FieldId order by t2.date1 desc) as rk
from t1,T2  t2 where t1.FieldId=t2.FieldId and t1.Status!='YES'
)

select * from cte where rk=1


您的问题中没有与“上次入境日期”对应的数据。请解决这个问题。我是说最晚的日期。。在T2表中有20/3和21/3日期条目我只想要最新日期条目意味着只有21/3日期条目。。我两者都懂了。你应该学会写清楚的问题。我们无法读懂您的心思,只能读懂问题,因此文本应该与示例数据相匹配。您的查询中的
c.FileID
r.FromStageID
是什么?我看不到这样的别名。您能发布实际有效的查询吗?对不起,先生,我错了,我现在已经编辑了。您的问题中没有与“上次输入日期”对应的数据。请解决这个问题。我是说最晚的日期。。在T2表中有20/3和21/3日期条目我只想要最新日期条目意味着只有21/3日期条目。。我两者都懂了。你应该学会写清楚的问题。我们无法读懂您的心思,只能读懂问题,因此文本应该与示例数据相匹配。您的查询中的
c.FileID
r.FromStageID
是什么?我看不到这样的别名。您能发布实际有效的查询吗?对不起,先生,我错了,我现在已经编辑了。您的问题中没有与“上次输入日期”对应的数据。请解决这个问题。我是说最晚的日期。。在T2表中有20/3和21/3日期条目我只想要最新日期条目意味着只有21/3日期条目。。我两者都懂了。你应该学会写清楚的问题。我们无法读懂您的心思,只能读懂问题,因此文本应该与示例数据相匹配。您的查询中的
c.FileID
r.FromStageID
是什么?我看不到这样的别名。你能发布实际有效的查询吗?对不起,先生