2016-11-08 19 views
0

私はSQLを初めて使用し、Quiresを勉強しています。助けて。前もって感謝します。SQLクエリのピボット列のピボット

実は私はこの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 

image

を使用することにより、以下の結果として得られるテーブルをgeetingですが、私は怒鳴る得られたテーブルを必要としています。

image2

+1

データを持つSQLfiddleは、その種の質問に適しています。 –

+1

テーブルではないので、正確なテーブルはsqlserverに存在しません。 – Mihai

答えて

0

あなたは、そのテーブルを持って傾けます。あなたができる最善の方法は、代わりにこの列を作成し、UIに追加のヘッダーを追加することです。

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 
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)