SQL Pivot上有三列

时间:2011-11-09 00:20:28

标签: sql pivot

我有一张记录如下的表

  ID - Type - Code - Order
  1  - A - 3  - 1
  1  - A - 5  - 2

  1  - B - 88  -1
  1  - B - 99  -2
  1  - B - 123 -3

  2  - A - 7  - 1
  2  - A - 6  - 2
  2  - A - 1  - 3
  2  - A - 8  - 4

等。

我想要的是转向ID,并生成

ID - A1 - A2 -  A3  -  A4  -  A5  - B1 - B2 - B3  -  B4  -  B5
1  - 3  - 5  - NULL - NULL - NULL - 88 - 99 - 123 - NULL - NULL

等。

如果我将其限制为只有一种代码类型,我可以做到这一点,但我无法弄清楚如何包含第二种代码类型。

我尝试在我的Order字段前加上A和B的前缀,但这搞砸了订单。

编辑:

SELECT  [ID], 
[1]  AS DX1 ,[2]  AS DX2 ,[3]  AS DX3 ,[4]  AS DX4 ,[5]  AS DX5 ,[6]  AS DX6 ,[7]  AS DX7 ,[8]  AS DX8 ,[9]  AS DX9 ,[10] AS DX10,
[11] AS DX11,[12] AS DX12,[13] AS DX13,[14] AS DX14,[15] AS DX15,[16] AS DX16,[17] AS DX17,[18] AS DX18,[19] AS DX19,[20] AS DX20,
[21] AS DX21,[22] AS DX22,[23] AS DX23,[24] AS DX24,[25] AS DX25,[26] AS DX26,[27] AS DX27,[28] AS DX28,[29] AS DX29,[30] AS DX30,
[31] AS DX31,[32] AS DX32,[33] AS DX33,[34] AS DX34,[35] AS DX35,[36] AS DX36,[37] AS DX37,[38] AS DX38,[39] AS DX39,[40] AS DX40,
[41] AS DX41,[42] AS DX42,[43] AS DX43,[44] AS DX44,[45] AS DX45,[46] AS DX46,[47] AS DX47,[48] AS DX48,[49] AS DX49,[50] AS DX50,
[51] AS DX51,[52] AS DX52,[53] AS DX53,[54] AS DX54,[55] AS DX55,[56] AS DX56,[57] AS DX57,[58] AS DX58,[59] AS DX59,[60] AS DX60,
[61] AS DX61,[62] AS DX62,[63] AS DX63,[64] AS DX64,[65] AS DX65,[66] AS DX66,[67] AS DX67,[68] AS DX68,[69] AS DX69,[70] AS DX70,
[71] AS DX71,[72] AS DX72,[73] AS DX73,[74] AS DX74,[75] AS DX75,[76] AS DX76,[77] AS DX77,[78] AS DX78,[79] AS DX79,[80] AS DX80,
[81] AS DX81,[82] AS DX82,[83] AS DX83,[84] AS DX84,[85] AS DX85,[86] AS DX86,[87] AS DX87,[88] AS DX88,[89] AS DX89,[90] AS DX90,
[91] AS DX91,[92] AS DX92,[93] AS DX93,[94] AS DX94,[95] AS DX95,[96] AS DX96,[97] AS DX97,[98] AS DX98,[99] AS DX99,[100] AS DX100
FROM(
SELECT
    FIC.[ID]
    ,ISNULL(Code.Code, '     ') Code
    ,FIC.[DWHCodingLineOrder] AS CodingOrder
FROM  FIC
INNER JOIN DimCode AS Code WITH(NOLOCK) ON FIC.CodeId = Code.CodeId
WHERE FIC.ArchiveFlag = 0 AND FIC.[CodeType] = 'DIAGN'
) AS SourceTable
PIVOT
(
    MAX(DiagCode)
    FOR DiagCodingOrder IN (
    [1] ,[2] ,[3] ,[4] ,[5] ,[6] ,[7] ,[8] ,[9] ,[10],
    [11],[12],[13],[14],[15],[16],[17],[18],[19],[20],
    [21],[22],[23],[24],[25],[26],[27],[28],[29],[30],
    [31],[32],[33],[34],[35],[36],[37],[38],[39],[40],
    [41],[42],[43],[44],[45],[46],[47],[48],[49],[50],
    [51],[52],[53],[54],[55],[56],[57],[58],[59],[60],
    [61],[62],[63],[64],[65],[66],[67],[68],[69],[70],
    [71],[72],[73],[74],[75],[76],[77],[78],[79],[80],
    [81],[82],[83],[84],[85],[86],[87],[88],[89],[90],
    [91],[92],[93],[94],[95],[96],[97],[98],[99],[100])
)AS DiagPivot

我想要做的是,但是在DX100之后,继续进行PROC1 - > PROC100 它与CodeType = Proce而不是Diagn

完全相同

2 个答案:

答案 0 :(得分:0)

看看OUTER APPLY,here就是一个很好的例子。

答案 1 :(得分:0)

您不能直接使用两列作为透视表的列名的来源。但是你可以在你的源表中连接它们,就像这样(如果我理解你的意图正确的话):

SELECT
  ID,
  DX1 , DX2 , DX3 , DX4 , DX5 , DX6 , DX7 , DX8 , DX9 , DX10,
  DX11, DX12, DX13, DX14, DX15, DX16, DX17, DX18, DX19, DX20,
  DX21, DX22, DX23, DX24, DX25, DX26, DX27, DX28, DX29, DX30,
  DX31, DX32, DX33, DX34, DX35, DX36, DX37, DX38, DX39, DX40,
  DX41, DX42, DX43, DX44, DX45, DX46, DX47, DX48, DX49, DX50,
  DX51, DX52, DX53, DX54, DX55, DX56, DX57, DX58, DX59, DX60,
  DX61, DX62, DX63, DX64, DX65, DX66, DX67, DX68, DX69, DX70,
  DX71, DX72, DX73, DX74, DX75, DX76, DX77, DX78, DX79, DX80,
  DX81, DX82, DX83, DX84, DX85, DX86, DX87, DX88, DX89, DX90,
  DX91, DX92, DX93, DX94, DX95, DX96, DX97, DX98, DX99, DX100,
  PROC1 , PROC2 , PROC3 , PROC4 , PROC5 , PROC6 , PROC7 , PROC8 , PROC9 , PROC10,
  PROC11, PROC12, PROC13, PROC14, PROC15, PROC16, PROC17, PROC18, PROC19, PROC20,
  PROC21, PROC22, PROC23, PROC24, PROC25, PROC26, PROC27, PROC28, PROC29, PROC30,
  PROC31, PROC32, PROC33, PROC34, PROC35, PROC36, PROC37, PROC38, PROC39, PROC40,
  PROC41, PROC42, PROC43, PROC44, PROC45, PROC46, PROC47, PROC48, PROC49, PROC50,
  PROC51, PROC52, PROC53, PROC54, PROC55, PROC56, PROC57, PROC58, PROC59, PROC60,
  PROC61, PROC62, PROC63, PROC64, PROC65, PROC66, PROC67, PROC68, PROC69, PROC70,
  PROC71, PROC72, PROC73, PROC74, PROC75, PROC76, PROC77, PROC78, PROC79, PROC80,
  PROC81, PROC82, PROC83, PROC84, PROC85, PROC86, PROC87, PROC88, PROC89, PROC90,
  PROC91, PROC92, PROC93, PROC94, PROC95, PROC96, PROC97, PROC98, PROC99, PROC100
FROM (
  SELECT
    FIC.ID,
    Code = ISNULL(Code.Code, '     ') Code,
    CodingOrder =
      CASE FIC.CodeType WHEN 'DIAGN' THEN 'DX' WHEN 'PROCE' THEN 'PROC' END +
      CAST(FIC.DWHCodingLineOrder AS varchar(20))
      /* CodingOrder is now not just 1, 2, 3 etc.
         but 'DX1', 'DX2', … 'PROC1', 'PROC2', … */
  FROM FIC
    INNER JOIN DimCode AS Code WITH(NOLOCK) ON FIC.CodeId = Code.CodeId
  WHERE FIC.ArchiveFlag = 0 AND FIC.CodeType IN ('DIAGN', 'PROCE')
) AS SourceTable
PIVOT (
  MAX(DiagCode)
  FOR CodingOrder IN (
    DX1 , DX2 , DX3 , DX4 , DX5 , DX6 , DX7 , DX8 , DX9 , DX10,
    DX11, DX12, DX13, DX14, DX15, DX16, DX17, DX18, DX19, DX20,
    DX21, DX22, DX23, DX24, DX25, DX26, DX27, DX28, DX29, DX30,
    DX31, DX32, DX33, DX34, DX35, DX36, DX37, DX38, DX39, DX40,
    DX41, DX42, DX43, DX44, DX45, DX46, DX47, DX48, DX49, DX50,
    DX51, DX52, DX53, DX54, DX55, DX56, DX57, DX58, DX59, DX60,
    DX61, DX62, DX63, DX64, DX65, DX66, DX67, DX68, DX69, DX70,
    DX71, DX72, DX73, DX74, DX75, DX76, DX77, DX78, DX79, DX80,
    DX81, DX82, DX83, DX84, DX85, DX86, DX87, DX88, DX89, DX90,
    DX91, DX92, DX93, DX94, DX95, DX96, DX97, DX98, DX99, DX100,
    PROC1 , PROC2 , PROC3 , PROC4 , PROC5 , PROC6 , PROC7 , PROC8 , PROC9 , PROC10,
    PROC11, PROC12, PROC13, PROC14, PROC15, PROC16, PROC17, PROC18, PROC19, PROC20,
    PROC21, PROC22, PROC23, PROC24, PROC25, PROC26, PROC27, PROC28, PROC29, PROC30,
    PROC31, PROC32, PROC33, PROC34, PROC35, PROC36, PROC37, PROC38, PROC39, PROC40,
    PROC41, PROC42, PROC43, PROC44, PROC45, PROC46, PROC47, PROC48, PROC49, PROC50,
    PROC51, PROC52, PROC53, PROC54, PROC55, PROC56, PROC57, PROC58, PROC59, PROC60,
    PROC61, PROC62, PROC63, PROC64, PROC65, PROC66, PROC67, PROC68, PROC69, PROC70,
    PROC71, PROC72, PROC73, PROC74, PROC75, PROC76, PROC77, PROC78, PROC79, PROC80,
    PROC81, PROC82, PROC83, PROC84, PROC85, PROC86, PROC87, PROC88, PROC89, PROC90,
    PROC91, PROC92, PROC93, PROC94, PROC95, PROC96, PROC97, PROC98, PROC99, PROC100
  )
) DiagPivot