转置SQL Server日期

时间:2014-06-16 12:33:39

标签: sql sql-server reporting-services

我不确定我是否需要转置,转轴,但这里就是。

我想要达到的目标是每天计算出一定数量的积分,这些积分取决于人数≥2的逗留时间。

目前我正在使用案例陈述来获取我的日期和我的观点,这里有一小部分关于我这样做:

CASE WHEN DATEDIFF(DAY, ADMISSION_OFFERS.TCI_DTTM, ADMISSION_OFFERS.EXPDS_DTTM)
>= 2 THEN DATEADD(DAY, 1, ADMISSION_OFFERS.TCI_DTTM) ELSE NULL END AS [Date 2],
CASE WHEN DATEDIFF(DAY, ADMISSION_OFFERS.TCI_DTTM, ADMISSION_OFFERS.EXPDS_DTTM)
>= 2 THEN '0.7' ELSE NULL END AS [Date 2 Point]

这样做我得到2个人的结果:

|Id     |Date 1     |Date 1 Point   |Date 2     |Date 2 Point   |Date 3     |Date 3 Point   |Date 4     |Date 4 Point   |Date 5     |Date 5 Point   |Date 6     |Date 6 Point   |Type   |
|1234567|21/11/2014 |0.7            |22/11/2014 |0.7            |23/11/2014 |0.7            |24/11/2014 |0.35           |NULL       |0              |NULL       |0              |Knee   |
|9876543|04/10/2014 |0.7            |05/10/2014 |0.7            |06/10/2014 |0.7            |07/10/2014 |0.7            |08/10/2014 |0.7            |09/10/2014 |0.35           |Hip    |

我是如何需要它为SSRS:

Id      |Date       |Point  |Type
1234567 |21/11/2014 |0.7    |Knee
1234567 |22/11/2014 |0.7    |Knee
1234567 |23/11/2014 |0.7    |Knee
1234567 |24/11/2014 |0.35   |Knee
1234567 |NULL       |0      |Knee
1234567 |NULL       |0      |Knee
9876543 |04/10/2014 |0.7    |Hip
9876543 |05/10/2014 |0.7    |Hip
9876543 |06/10/2014 |0.7    |Hip
9876543 |07/10/2014 |0.7    |Hip
9876543 |08/10/2014 |0.7    |Hip
9876543 |09/10/2014 |0.35   |Hip

完成后,我需要将日期作为SSRS的日期字段。

2 个答案:

答案 0 :(得分:1)

使用tmp表进行case语句查询,

WITH tmpTable AS
(Your case statement query)
SELECT Id, Data1, Point1, [TYPE] FROM tmpTable UNION
SELECT Id, Data2, Point2, [TYPE] FROM tmpTable UNION
SELECT Id, Data3, Point3, [TYPE] FROM tmpTable 

答案 1 :(得分:0)

一种简单的方法就是将日期的值合并为一个列表。

例如,如果您将查询结果放入临时表中,您将编写:

SELECT Id, [Date 1] AS Date, [Date 1 Point] AS Point, Type
FROM #Temp
UNION ALL
SELECT Id, [Date 2] AS Date, [Date 2 Point] AS Point, Type
FROM #Temp
UNION ALL
SELECT Id, [Date 3] AS Date, [Date 3 Point] AS Point, Type
FROM #Temp
UNION
SELECT Id, [Date 4] AS Date, [Date 4 Point] AS Point, Type
FROM #Temp