我是SQL
的新手并且正在学习编写Quires。帮我。在此先感谢。
实际上,我正在使用此query
select * from
(select case Temp
when '0' then 'Temperature'
when '1' then 'PULSE RATE/MIN'
when '2' then 'RESPIRATION/MIN'
when '3' then 'BLOOD PRESSURE'
when '4' then 'URINE'
end as Temp,
Value,
convert(nvarchar(15),Date,103) Date,
concat(case Time
when '0' then '2'
when '1' then '6'
when '2' then '10'
end,' ',
case AMPM
when '0' then 'AM'
when '1' then 'PM'
end)[Tim]
from HMS_Chart_Clinical
where status = '0' and IPNO='21460') as s
pivot(max(Value) for [date] in ([07/11/2016],[08/11/2016])) as datapivot
pivot(max(Tim) for [Tim] in ([2 AM],[6 AM],[10 AM],[2 PM],[6 PM],[10 PM])) as datapivot
但我需要下面的结论表。
答案 0 :(得分:0)
你不能拥有那张桌子。您可以做的最好是创建此列,并在UI中添加aditional标头。
07/11/2016 02:00 am
07/11/2016 06:00 am
07/11/2016 10:00 am
07/11/2016 02:00 pm
07/11/2016 06:00 pm
07/11/2016 10:00 pm
08/11/2016 02:00 am
08/11/2016 06:00 am
08/11/2016 10:00 am
08/11/2016 02:00 pm
08/11/2016 06:00 pm
08/11/2016 10:00 pm
因此请将您的查询更改为
concat(convert(nvarchar(15),Date,103) Date,
case Time
when '0' then '2'
when '1' then '6'
when '2' then '10'
end,' ',
case AMPM
when '0' then 'AM'
when '1' then 'PM'
end) as [Tim]
并使用单个支点
pivot(max(Tim) for [Tim] in ([07/11/2016 02 AM],
[07/11/2016 06 AM],
[07/11/2016 10 AM],
[07/11/2016 02 PM],
[07/11/2016 06 PM],
[07/11/2016 10 PM]) .....) as datapivot
答案 1 :(得分:0)
我遇到与此问题无关的问题。所以不要过多地影响你原来的思维方式:
DECLARE @HMS_Chart_Clinical TABLE (
[ID] INT PRIMARY KEY NOT NULL IDENTITY (1,1),
[Temp] INT, --0,1,2,3,4
[Value] INT,
[Date] DATE,
[Time] INT, -- 0,1,2
[AMPM] INT, -- 0,1
[status] BIT,
[IPNO] INT
)
INSERT @HMS_Chart_Clinical ([Temp], [Value], [Date], [Time], [AMPM], [status], [IPNO]) VALUES (3, 125, '20161107', 2, 0, 0, 21460)
INSERT @HMS_Chart_Clinical ([Temp], [Value], [Date], [Time], [AMPM], [status], [IPNO]) VALUES (1, 26, '20161108', 2, 1, 0, 21460)
INSERT @HMS_Chart_Clinical ([Temp], [Value], [Date], [Time], [AMPM], [status], [IPNO]) VALUES (1, 99, '20161107', 1, 1, 0, 21460)
INSERT @HMS_Chart_Clinical ([Temp], [Value], [Date], [Time], [AMPM], [status], [IPNO]) VALUES (0, 106, '20161107', 0, 0, 0, 21460)
IF OBJECT_ID('tempdb..#PivotSource') IS NOT NULL
DROP TABLE #PivotSource
CREATE TABLE #PivotSource
(
Temp VARCHAR(MAX),
Value INT,
[DateTime] VARCHAR(MAX)
)
INSERT INTO #PivotSource
SELECT CASE Temp
WHEN '0' THEN 'Temperature'
WHEN '1' THEN 'PULSE RATE/MIN'
WHEN '2' THEN 'RESPIRATION/MIN'
WHEN '3' THEN 'BLOOD PRESSURE'
WHEN '4' THEN 'URINE'
END AS Temp,
Value,
CONVERT(NVARCHAR(15),[Date],103) + ' ' +
CASE [Time]
WHEN '0' THEN '2'
WHEN '1' THEN '6'
WHEN '2' THEN '10'
END + ' ' +
CASE AMPM
WHEN '0' THEN 'AM'
WHEN '1' THEN 'PM'
END AS [DateTime]
FROM @HMS_Chart_Clinical
WHERE [status] = '0'
AND IPNO='21460'
DECLARE @PivotColumns NVARCHAR(MAX)
;WITH AllDatesAndTimes AS (
SELECT DISTINCT [Date],
TimeString.[Time],
TimeString.[SortOrder]
FROM @HMS_Chart_Clinical
CROSS JOIN (SELECT '2 AM' AS [Time], 0 AS SortOrder
UNION ALL SELECT '6 AM' AS [Time], 1 AS SortOrder
UNION ALL SELECT '10 AM' AS [Time], 2 AS SortOrder
UNION ALL SELECT '2 PM' AS [Time], 3 AS SortOrder
UNION ALL SELECT '6 PM' AS [Time], 4 AS SortOrder
UNION ALL SELECT '10 PM' AS [Time], 5 AS SortOrder) TimeString
)
SELECT @PivotColumns = COALESCE(@PivotColumns + '], [', '') + CONVERT(NVARCHAR(15),[Date],103) + ' ' + [Time]
FROM AllDatesAndTimes
ORDER BY [Date],[SortOrder]
DECLARE @PivotQuery NVARCHAR(MAX) = 'SELECT * FROM #PivotSource PIVOT(MAX(Value) FOR [DateTime] IN ([' + @PivotColumns + '])) AS DATAPIVOT'
EXEC(@PivotQuery)