0

这对我来说是一个后续问题,但确实是一个新项目。我将分享一些信息,而查询中的其他信息我将无法完全提供,但我必须谨慎行事。

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

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 通过重用他的答案来解决稍微不同的问题,让我从这里开始,我 PIVOT 数据:

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 代码时再次执行此操作。我是否必须重做所有名称略有不同的声明?

4

0 回答 0