按字符串顺序排列为数字sql

时间:2017-02-03 10:21:12

标签: sql string pivot sql-order-by

我有一张如下表格,我需要用数周的时间来旋转表格。我能够创建数据透视表,但是由于我对字符串列进行排序,因此列的顺序会被洗牌。并获得像第3表一样的输出。

MRN                   Weeks
--------------------------------
GIRFTR1H0461081       52
GIRFTR1H0461083       5 - 6
GIRFTR1H0461084       0 - 1
GIRFTR1H0461085       1 - 2
GIRFTR1H0461086       11 - 12
GIRFTR1H0461087       1 - 2

我需要通过Weeks Like获取输出作为数据透视表顺序,

MRN               | 0 - 1 | 1 - 2 | 5 - 6 |11 - 12|  52
---------------------------------------------------------------
GIRFTR1H0461081   |   0   |   0   |   0   |  0    |  0
GIRFTR1H0461083   |   0   |   0   |   1   |  0    |  0
GIRFTR1H0461084   |   1   |   0   |   0   |  0    |  0
GIRFTR1H0461085   |   0   |   1   |   0   |  0    |  0
GIRFTR1H0461086   |   0   |   0   |   0   |  1    |  0
GIRFTR1H0461087       0   |   1   |   0   |  0    |  0

当我试图创建这个表时,列顺序像下面那样被洗牌,

MRN               | 0 - 1 | 1 - 2 |11 - 12| 5 - 6 |  52
---------------------------------------------------------------
GIRFTR1H0461081   |   0   |   0   |   0   |  0    |  0
GIRFTR1H0461083   |   0   |   0   |   0   |  1    |  0
GIRFTR1H0461084   |   1   |   0   |   0   |  0    |  0
GIRFTR1H0461085   |   0   |   1   |   0   |  0    |  0
GIRFTR1H0461086   |   0   |   0   |   1   |  0    |  0
GIRFTR1H0461087   |   0   |   1   |   0   |  0    |  0

我的代码是,

    DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

    select @cols = STUFF((SELECT ',' + QUOTENAME(WeeksBand) 
                          from [test_Tbl]
                          group by WeeksBand
                          order by WeeksBand
                          FOR XML PATH(''), TYPE
                         ).value('.', 'NVARCHAR(MAX)') 
                    ,1,1,'')

    set @query = 'SELECT CurrentSpecialty,' + @cols + ' from 
                (
                   select CurrentSpecialty, WeeksBand, EncounterId
                   from [Tbl_Current_PTL_Test]
                ) x
               pivot 
                (
                   count(EncounterId)
                   for WeeksBand in (' + @cols + ')
                ) p '

   execute(@query);

请帮我解决一下我的问题,

3 个答案:

答案 0 :(得分:1)

它正在进行字符串(alpha)排序,而你实际上想要进行数字排序。

我要改变订单行:

按WeeksBand订购

为:

按Cast排序(SubString(1周,当CHARINDEX时为案例(' - ',WeeksBand)!= 0那么CHARINDEX(' - ',WeeksBand)-1 ELSE LEN(WeeksBand)END)AS INT)

将第一个数字转换为整数值(不是字符串),然后应该正确排序。

答案 1 :(得分:0)

我不明白为什么标准的数据透视查询在这里不起作用。根据您的预期输出,您似乎希望1作为MRN条目的占位符,该条目在该周内存在,否则为0

SELECT [MRN],
       MAX(CASE WHEN [Weeks] = '0 - 1'   THEN 1 ELSE 0 END) AS [0 - 1],
       MAX(CASE WHEN [Weeks] = '1 - 2'   THEN 1 ELSE 0 END) AS [1 - 2],
       MAX(CASE WHEN [Weeks] = '5 - 6'   THEN 1 ELSE 0 END) AS [5 - 6],
       MAX(CASE WHEN [Weeks] = '11 - 12' THEN 1 ELSE 0 END) AS [11 - 12],
       MAX(CASE WHEN [Weeks] = '52'      THEN 1 ELSE 0 END) AS [52]
FROM [yourTable]
GROUP BY [MRN]

答案 2 :(得分:0)

您应该使用2位数字格式化第一个表的周字段:

MRN                   Weeks
--------------------------------
GIRFTR1H0461081       52
GIRFTR1H0461083       05 - 06
GIRFTR1H0461084       00 - 01
GIRFTR1H0461085       01 - 02
GIRFTR1H0461086       11 - 12
GIRFTR1H0461087       01 - 02