在SQL

0

I want same output shown in Output table. I have TableA and I want to pivote it and want output table as shown in image. Thanks在SQL

樞軸表我有一個表#tableA

+-----+-------------+-------+------------+ 
| A | Allocations | Seats | EndDate | 
+-----+-------------+-------+------------+ 
| ABC |   450 | 23 | 2017-10-05 | 
| ABC |   23 | 765 | 2017-05-01 | 
| PQR |   54 | 34 | 2017-07-04 | 
| ABC |   234 | 45 | 2017-11-27 | 
| PQR |   987 | 76 | 2017-03-05 | 
| ABC |   76 | 65 | 2017-02-23 | 
| PQR |   89 | 324 | 2017-08-14 | 
| ABC |   45 | 34 | 2017-07-13 | 
+-----+-------------+-------+------------+ 

哪些可以創建和填充如下。

CREATE TABLE #TableA 
    (
    A   VARCHAR(50), 
    Allocations INT, 
    Seats  INT, 
    EndDate  DATETIME 
); 

INSERT INTO #TableA 
VALUES  ('ABC',450,23,'2017-10-05'), 
      ('ABC',23,765,'2017-05-01'), 
      ('PQR',54,34,'2017-07-04'), 
      ('ABC',234,45,'2017-11-27'), 
      ('PQR',987,76,'2017-03-05'), 
      ('ABC',76,65,'2017-02-23'), 
      ('PQR',89,324,'2017-08-14'), 
      ('ABC',45,34,'2017-07-13'); 

A列有ABCPQR唯一值。 日期時間列有多個值。

如何獲得以下輸出?

日期時間來自列A中TableA的所有Datetime值。

Output :- 

    date   | 2017-12-13 | 2017-12-20 | 2017-12-27 | -|-|-|-|-|-|-|-|-| 
    ------------------------------------------------------------------------------- 
    A   | ABC   | ABC   | ABC  | 
    Allocations | 50   | 50   | 50   | 
    Seats  | 27   | 27   | 27   | 
    A   | PQR   | PQR   | PQR  | 
    Alloc  | 50   | 50   | 50   | 
    Seats  | 12   | 12   | 12   | 
+0

單擊該問題以查看圖像。 –

+0

嘿謝謝..我會這樣做 –

+0

期望的輸出如何與示例輸入相關聯?爲什麼每天都有'50/27/50/12'? –

回答

0

這是你應該在報告層而不是SQL中做的事情。

它可能在SQL(demo)中,但不是SQL旨在執行的操作。

WITH T 
    AS (SELECT A, 
       thing, 
       priority, 
       value, 
       d 
     FROM #TableA 
       CROSS APPLY (VALUES(CAST(EndDate AS DATE)))D(d) 
       CROSS APPLY (VALUES(1, 'A', NULL), 
            (2, 'Allocations', Allocations), 
            (3, 'Seats', Seats)) V(priority, thing, value)) 
SELECT thing, 
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-02-23],0) AS VARCHAR(50)) END AS [2017-02-23], 
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-03-05],0) AS VARCHAR(50)) END AS [2017-03-05], 
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-05-01],0) AS VARCHAR(50)) END AS [2017-05-01], 
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-07-04],0) AS VARCHAR(50)) END AS [2017-07-04], 
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-07-13],0) AS VARCHAR(50)) END AS [2017-07-13], 
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-08-14],0) AS VARCHAR(50)) END AS [2017-08-14], 
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-10-05],0) AS VARCHAR(50)) END AS [2017-10-05], 
case when thing = 'A' THEN A ELSE CAST(ISNULL([2017-11-27],0) AS VARCHAR(50)) END AS [2017-11-27] 
FROM T 
PIVOT (SUM(value) FOR d in (
          [2017-02-23], 
          [2017-03-05], 
          [2017-05-01], 
          [2017-07-04], 
          [2017-07-13], 
          [2017-08-14], 
          [2017-10-05], 
          [2017-11-27])) P 
ORDER BY A, priority 

以上甚至沒有解決動態方面。爲此,您需要生成一個基於上述日期和動態SQL字符串#TableA

+0

嘿,謝謝你是完美的..我已經生成了日期的dyamic SQL字符串..我可以如何在上面的查詢中使用它?並且非常感謝.. –

+0

您需要將整個事件連接成一個字符串和'exec'。 –

+0

嘿我在生成動態SQL字符串時遇到問題。你能幫忙嗎? –