试图找出如何使用连接执行动态sql数据透视表

时间:2017-07-30 20:22:21

标签: sql sql-server excel tsql

我仍然是T-SQL的初学者,有一些相对简单的select语句,只有少量连接。我试图将它放入动态数据透视表中。本声明的目的是显示非营利性服务及其结果,新旧结果只是成对相同数据的不同单词。

SELECT Service.Name,
OutcomeIndicator."Desc" AS OutcomeDesc,
NewOutcomeIndicator.Description As NewOutcomeDesc
FROM service 
FULL OUTER JOIN OutcomeIndicator ON ServiceOutcomeIndicator.OutcomeIndicatorID=OutcomeIndicator.OutcomeIndicatorID
FULL OUTER JOIN NewOutcomeIndicator ON NewOutcomeIndicatorMapping.NewOutcomeIndicatorID=NewOutcomeIndicator.NewOutcomeIndicatorID

但是,当我运行此语句时,它显然会返回每一行,包括ServiceName的多个值,因为每个服务可以有多个Outcomes / NewOutcome配对。

+-------------+----------------+------------------+
| ServiceName |  OutcomeDesc1  | NewOutcomeDesc1  |
+-------------+----------------+------------------+
| Service34   | OutcomeDesc14  | NewOutcomeDesc14 |
| Service34   | OutcomeDesc12  | NewOutcomeDesc12 |
| Service35   | OutcomeDesc4   | NewOutcomeDesc4  |
| Service44   | OutcomeDesc80  | NewOutcomeDesc80 |
| Service44   | OutcomeDesc99  | NewOutcomeDesc99 |
| Service51   | OutcomeDesc50  | NewOutcomeDesc50 |

出于报告目的,由于这被复制到excel中,我想要更像这样的东西:

+--------------+-----------------+-------------------+-----------------+-------------------+--------------+---------------------+
| ServiceName  |  OutcomeDesc1   | NewOutcomeDesc1   |  OutcomeDesc2   | NewOutcomeDesc2   | OutcomeDesc3 |  NewOutcomeDesc3    |
+--------------+-----------------+-------------------+-----------------+-------------------+--------------+---------------------+
| Service34    | OutcomeDesc14   | NewOutcomeDesc14  | OutcomeDesc12   | NewOutcomeDesc12  |              |                     |
| Service35    | OutcomeDesc4    | NewOutcomeDesc4   |                 |                   |              |                     |
| Service44    | OutcomeDesc80   | NewOutcomeDesc80  | OutcomeDesc99   | NewOutcomeDesc99  |              |                     |
| Service51    | OutcomeDesc50   | NewOutcomeDesc50  |                 |                   |              |                     |
+--------------+-----------------+-------------------+-----------------+-------------------+--------------+---------------------+

有关如何更改我的sql语句以执行此操作的任何想法?我可以静态地定义列数,因为我知道每个服务的结果不超过3个。

我理解如何在sql中执行静态数据透视表,但由于有数百个服务和结果,我无法定义统计分配每个服务值到列的逻辑。动态数据透视表对我来说也有点难以理解,但是我还没有能够找到这样做的教程,同时还包括一个连接语句。

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

你不需要动态支点,实际上你甚至不需要使用真正的数据透视命令,你可以用CASE和GROUP BY轻松模拟

在检索结果后,您显示在那里,只需使用ROW_NUMBER()函数为每行指定1-3,并在最后使用该RN选择进入哪一列的内容。

这是一个片段,在第一个CTE中添加真实代码。

WITH CTE_Source AS 
(
   --Your query goes here
)
, CTE_RN AS 
(
    SELECT *
    , ROW_NUMBER() OVER (PARTITION BY ServiceName ORDER BY OutcomeDesc) RN
    FROM CTE_Source
)
SELECT ServiceName
, MAX(CASE WHEN RN = 1 THEN OutcomeDesc END) AS OutcomeDesc1
, MAX(CASE WHEN RN = 1 THEN NewOutcomeDesc END) AS NewOutcomeDesc1
, MAX(CASE WHEN RN = 2 THEN OutcomeDesc END) AS OutcomeDesc2
, MAX(CASE WHEN RN = 2 THEN NewOutcomeDesc END) AS NewOutcomeDesc2
, MAX(CASE WHEN RN = 3 THEN OutcomeDesc END) AS OutcomeDesc3
, MAX(CASE WHEN RN = 3 THEN NewOutcomeDesc END) AS NewOutcomeDesc3
FROM CTE_RN
GROUP BY ServiceName

DEMO using your data as SourceTable