结合两个数据透视表SQL Server

时间:2017-09-07 05:28:23

标签: sql tsql

我正在尝试交叉连接两个数据透视表。

- 第一个Pivot表查询获取drug_names的列并生成行值 - the drug_id。

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

Select @cols = STUFF((SELECT ',' + QUOTENAME(drug) 
                    from _app_drugs
                    group by drug, drug_id,order_number
                    order by order_number
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' from 
             (
                select drug_id, drug
                from _app_drugs
            ) x
            pivot 
            (
                max(drug_id)
                for drug in (' + @cols + N')
            ) p'

exec sp_executesql @query;

--The second pivot table simply gets the signature_labels and displays the label name in the column and displays the label_id as the row value.

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

Select @cols = STUFF((SELECT ',' + QUOTENAME(signature_label) 
                    from _app_signature_labels
                    WHERE _app_signature_labels.isactive=1
                    group by signature_label_id, signature_label,ordernumber
                    order by ordernumber
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' from 
             (
                select signature_label_id,signature_label
                from _app_signature_labels              
            ) x
            pivot 
            (
                max(signature_label_id)
                for signature_label in (' + @cols + N')
            ) p'

exec sp_executesql @query;

现在我只需要知道如何将这两个数据透视表合并为一个表......它们没有公共字段,也不需要一个。

有人可以帮我这个吗? 谢谢

1 个答案:

答案 0 :(得分:0)

您可以在两个全局临时表中执行两个单独的动态SQL语句,然后将结果合并,如下所示:

IF object_id('tempdb..##tmpResult1') IS NOT NULL
BEGIN
    DROP TABLE ##tmpResult1
END

IF object_id('tempdb..##tmpResult2') IS NOT NULL
BEGIN
    DROP TABLE ##tmpResult2
END

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

Select @cols = STUFF((SELECT ',' + QUOTENAME(drug) 
                    from _app_drugs
                    group by drug, drug_id,order_number
                    order by order_number
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' INTO ##tmpResult1 from 
             (
                select drug_id, drug
                from _app_drugs
            ) x
            pivot 
            (
                max(drug_id)
                for drug in (' + @cols + N')
            ) p'

exec sp_executesql @query;

--The second pivot table simply gets the signature_labels and displays the label name in the column and displays the label_id as the row value.

Select @cols2 = STUFF((SELECT ',' + QUOTENAME(signature_label) 
                    from _app_signature_labels
                    WHERE _app_signature_labels.isactive=1
                    group by signature_label_id, signature_label,ordernumber
                    order by ordernumber
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query2 = N'SELECT ' + @cols2 + N' INTO ##tmpResult2 from 
             (
                select signature_label_id,signature_label
                from _app_signature_labels              
            ) x
            pivot 
            (
                max(signature_label_id)
                for signature_label in (' + @cols2 + N')
            ) p'

exec sp_executesql @query2;

SELECT * FROM ##tmpResult1 t1 
  INNER JOIN ##tmpResult2 t2 ON t1.ordernumber = t2.ordernumber

这也是在执行大型PIVOT查询时解决此错误的一种方法:Internal error: An expression services limit has been reached. Please look for potentially complex expressions in your query, and try to simplify them.