我有一种情况,我需要查询以基于代码值提取数据。
示例:表#temp1具有人员ID,程序分配和数据集的组合。对于任何人“ 1001”,我都希望使用类似的代码“ PS”来提取第一个程序的录入日期以及最后一个程序的发布日期和数据集。
所以,我想要的输出是:
演示代码:
https://rextester.com/ADDL95491
有帮助吗?!
答案 0 :(得分:2)
在我看来,您需要在下面的查询
with cte1 as
(select cid,code,admissiondate,dischargedate,program
from #temp1 t1 where
t1.row_number = (select min(row_number) from #temp1)
) , cte2 as
(select * from #temp1 where dataset is not null
)
select cte1.cid,cte1.code,cte1.admissiondate,
cte2.dischargedate,cte2.dataset
from cte1 left join cte2 on cte1.code=cte2.code
https://rextester.com/BNVK71028
cid code admissiondate dischargedate dataset
1 1001 PR 01/01/2011 5/1/2011 discharge data
2 1001 PS 06/01/2011 7/1/2011 discharge data
3 1001 PQ 08/01/2011