需要对PIVOT表结果进行排序,并且需要在单个查询中PIVOT不同的集合

时间:2017-04-25 20:20:45

标签: sql sql-server-2008 pivot dynamic-pivot

对我来说这是一个跟进问题,但实际上是一个新项目。我在查询中分享的一些信息和其他信息我完全无法完成,但我必须谨慎对待其中一些信息。

在这个查询中,我创建了两个临时表,我将引用:

IF OBJECT_ID('tempdb..#SAMHSA_PAT_LIST') IS NOT NULL
DROP TABLE #SAMHSA_PAT_LIST
CREATE TABLE #SAMHSA_PAT_LIST (
MRN VARCHAR(50),
trac_id VARCHAR(50))

INSERT INTO #SAMHSA_PAT_LIST (MRN, trac_id) 
VALUES ('XXXXXXXX','001').....

INSERT INTO #MentalDX (MHDX) 
VALUES ('F03.90'),('F03.91'),('F20.0'),('F20.1').... --hundreds of values

从结构上讲,我接下来会使用以下内容查找特定访问:

IF OBJECT_ID ('tempdb..#grantvisits') IS NOT NULL
DROP TABLE #grantvisits

SELECT
    spl.MRN
    ,spl.trac_id
    ,pev.CONTACT_DATE AS Visits

INTO
    #grantvisits

FROM
    #SAMHSA_PAT_LIST spl
    INNER JOIN dbo.IDENTITY_ID_VIEW iiv
    ON iiv.IDENTITY_ID=spl.MRN
    INNER JOIN dbo.PAT_ENC_VIEW pev
    ON pev.PAT_ID = iiv.PAT_ID

WHERE
    pev.CONTACT_DATE >= @Start_Date
    AND pev.CONTACT_DATE < @End_Date
    AND iiv.IDENTITY_ID = spl.MRN
    AND iiv.IDENTITY_TYPE_ID = XX
    AND pev.APPT_STATUS_C IN ( 2 , 6 , 8 , 9 )

然后我查找与第二个临时表中的值匹配的访问:

IF OBJECT_ID ('tempdb..#dxMentalHealth') IS NOT NULL
DROP TABLE #dxMentalHealth

SELECT 
    gv.MRN
    ,gv.trac_id
    ,edg1m.EXTERNAL_ID AS mdx1
    ,edg2m.EXTERNAL_ID AS mdx2
    ,edg3m.EXTERNAL_ID AS mdx3
    ,edg4m.EXTERNAL_ID AS mdx4
    ,edg5m.EXTERNAL_ID AS mdx5
    ,edg6m.EXTERNAL_ID AS mdx6

INTO
    #dxMentalHealth
FROM
    #grantvisits gv
    INNER JOIN dbo.IDENTITY_ID_VIEW iiv
    ON iiv.IDENTITY_ID=gv.MRN
    .....
    LEFT JOIN CLARITY_EDG edg1m
    ON tdl.DX_ONE_ID = edg1m.DX_ID
    AND edg1m.REF_BILL_CODE_SET_C IN ( 1 , 2 , 1001 )-- 1 = ICD9; 2 = ICD10
    AND edg1m.REF_BILL_CODE IN ( SELECT md.MHDX FROM #MentalDX md )
    ....

接下来,我查看这些访问并查找与MentalDX列表特别匹配的dx代码:

IF OBJECT_ID('tempdb..#mdxcounts') IS NOT NULL
DROP TABLE #mdxcounts


SELECT trac_id
    ,mdx1 AS mdxx
    ,COUNT(*) AS cnt
INTO #mdxcounts
FROM  #dxMentalHealth
WHERE mdx1 IS NOT NULL
GROUP BY
    trac_id
    ,mdx1
UNION
SELECT trac_id
    ,mdx2
    ,COUNT(*) AS cnt
FROM #dxMentalHealth
WHERE mdx2 IS NOT NULL
GROUP BY
    trac_id
    ,mdx2                    
UNION
SELECT trac_id
    ,mdx3
    ,COUNT(*) AS cnt
FROM #dxMentalHealth
WHERE mdx3 IS NOT NULL
GROUP BY
    trac_id
    ,mdx3   
UNION
SELECT trac_id
    ,mdx4
    ,COUNT(*) AS cnt
FROM #dxMentalHealth
WHERE mdx4 IS NOT NULL
GROUP BY
    trac_id
    ,mdx4   
UNION
SELECT trac_id
    ,mdx5
    ,COUNT(*) AS cnt
FROM #dxMentalHealth
WHERE mdx5 IS NOT NULL
GROUP BY
    trac_id
    ,mdx5   
UNION
SELECT trac_id
    ,mdx6
    ,COUNT(*) AS cnt
FROM #dxMentalHealth
WHERE mdx6 IS NOT NULL
GROUP BY
    trac_id
    ,mdx6;

接下来我非常感谢@SqlZim让我在这里重新使用his answer for a slightly different problem

IF OBJECT_ID('tempdb..##mdxpivot') IS NOT NULL
DROP TABLE ##mdxpivot

declare @cols nvarchar(max);
declare @select_cols nvarchar(max);
declare @sql  nvarchar(max);

  select @cols = stuff((
    select distinct 
      ',' + quotename('mdxtemp'
          + right('0' +convert(nvarchar(10),row_number() over (
              partition by trac_id 
              order by     cnt
          )),2)
          )
      from #mdxcounts
      for xml path (''), type).value('.','nvarchar(max)')
    ,1,1,'');

select @select_cols = (
    select distinct 
      char(10)+'        , ' + quotename('mdxtemp'
          + right('0' +convert(nvarchar(10),row_number() over (
              partition by trac_id 
              order by     cnt
          )),2)
          ) +' = isnull('+
        quotename('mdxtemp'
          + right('0' +convert(nvarchar(10),row_number() over (
              partition by trac_id 
              order by     cnt
          )),2)
          )+','''')'
      from #mdxcounts
      for xml path (''), type).value('.','nvarchar(max)')

select @sql = '
 select trac_id' + @select_cols + '
into ##mdxpivot
 from  (
    select 
        trac_id
      , mdxx
      , rn=''mdxtemp''+right(''0'' +convert(nvarchar(10),row_number() over (
              partition by trac_id 
              order by     cnt
          )),2)
    from #mdxcounts
      ) as a
 pivot (max([mdxx]) for [rn] in (' + @cols + ')  ) p'
 --select @sql as CodeGenerated
 exec sp_executesql @sql

我需要做的是按计数对mdxtemp *列中的值进行排序。作为参考并显示我还没有看到#mdxcounts表中三个trac_id的结果如下:

trac_id mdxx    cnt
1   F33.1   504
1   F07.81  378
1   F31.9   189
5   F33.9   1825
5   F50.00  1400
5   F20.9   975
5   F20.3   700
5   F43.10  700
5   F43.10  625
5   F20.3   625
5   F19.10  625
10  F31.9   1547
10  F25.0   169
10  R45.851 91
10  R44.0   91
10  F31.74  39

##mdxpivot的结果显示如下:

trac_id mdxtemp01   mdxtemp02   mdxtemp03   mdxtemp04   mdxtemp05   mdxtemp06   mdxtemp07   mdxtemp08
001 F31.9   F07.81  F33.1                   
005 F43.10  F20.3   F19.10  F43.10  F20.3   F20.9   F50.00  F33.9
010 F31.74  R44.0   R45.851 F25.0   F31.9           

如何重新排序值?此外,我将不得不在查询中第二次执行此操作,而不是#MentalDX临时表中的所有DX代码。我是否必须用略微不同的名称重做所有声明?

0 个答案:

没有答案