使用大写字母在同一行中列出多个日期
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用大写字母在同一行中列出多个日期相关的知识,希望对你有一定的参考价值。
我正在查询将来的听证会日期。是否可以在聆讯1的同一行中列出第二个聆讯日期-而不是将案件2的行列出。
这里是一个例子:
基本上我不希望第一条记录,我只希望第二条记录都在同一行中。
这是该部分的代码。
SELECT *
INTO #EventStatusInfoFinal
FROM (
SELECT DISTINCT ESI1.*,
(CASE WHEN ESI1.NextHearingDate = ESI2.NextHearingDate THEN ESI1.NextHearingDate
ELSE ESI1.NextHearingDate
END) AS 'NextHearingDate1',
(CASE WHEN ESI1.NextHearingDate != ESI2.NextHearingDate THEN ESI2.NextHearingDate
ELSE ''
END) AS 'NextHearingDate2',
RANK () OVER (PARTITION BY ESI1.CaseID ORDER BY ESI1.NextHearingDate ASC) AS RANK3
FROM #EventStatusInfo ESI1
INNER JOIN #EventStatusInfo ESI2 ON ESI2.CaseID = ESI1.CaseID
) AS X
WHERE X.RANK3 = 1
答案
您可以尝试使用聚合-
SELECT *
INTO #EventStatusInfoFinal
FROM (
SELECT DISTINCT ESI1.CaseID ,
max(CASE WHEN ESI1.NextHearingDate = ESI2.NextHearingDate THEN ESI1.NextHearingDate
ELSE ESI1.NextHearingDate
END) AS 'NextHearingDate1',
max(CASE WHEN ESI1.NextHearingDate != ESI2.NextHearingDate THEN ESI2.NextHearingDate
ELSE null
END) AS 'NextHearingDate2',
RANK () OVER (PARTITION BY ESI1.CaseID ORDER BY ESI1.NextHearingDate ASC) AS RANK3
FROM #EventStatusInfo ESI1
INNER JOIN #EventStatusInfo ESI2 ON ESI2.CaseID = ESI1.CaseID
group by ESI1.CaseID
) AS X
WHERE X.RANK3 = 1
另一答案
这是删除不需要的行的一种简单方法:
SELECT *
FROM (
SELECT DISTINCT ESI1.*,
(CASE WHEN ESI1.NextHearingDate = ESI2.NextHearingDate THEN ESI1.NextHearingDate
ELSE ESI1.NextHearingDate
END) AS 'NextHearingDate1',
(CASE WHEN ESI1.NextHearingDate != ESI2.NextHearingDate THEN ESI2.NextHearingDate
ELSE NULL
END) AS 'NextHearingDate2',
RANK () OVER (PARTITION BY ESI1.CaseID ORDER BY ESI1.NextHearingDate ASC) AS RANK3
FROM EventStatusInfo ESI1
INNER JOIN EventStatusInfo ESI2 ON ESI2.CaseID = ESI1.CaseID
) AS X
WHERE X.RANK3 = 1
and NextHearingDate2 is not null --this is the part I added.
但是如果您向我们提供您正在使用的表中的示例数据,那么也许我们可以为您提供更好的代码。在此之前,我希望这会对您有所帮助。
以上是关于使用大写字母在同一行中列出多个日期的主要内容,如果未能解决你的问题,请参考以下文章
创建一个循环以在同一行中记录下一个记录时间和日期(基于唯一 ID)
使用 bootstrap 3 在同一行获取两个日期时间选择器?