简体   繁体   English

如何在 Microsoft SQL 服务器中创建 pivot 表?

[英]How to create a pivot table in Microsoft SQL Server?

First question on Stack overflow.关于堆栈溢出的第一个问题。 I work for a medical testing firm and they need a table that shows how many people in each state received each type of test in 2021. I have all the states in my adr.state table and all the test types in my p.name table.我在一家医学检测公司工作,他们需要一张表格,显示每个 state 中有多少人在 2021 年接受了每种类型的测试。我的adr.state表中有所有状态,我的p.name表中有所有测试类型.

I wrote the following query:我写了以下查询:

SELECT
    adr.state,
    'TaqPath COVID-19 rRT-PCR Assay',
    'TaqPath COVID-19 rRT-PCR Assay (Saliva Specimen)',
    'TaqPath COVID-19 rRT-PCR Assay (Throat Specimen)',
    'Beckman Access SARS-CoV-2 IgG Test',
    'PSA, Free',
    'Colorectal Cancer Current Risk'
FROM
    (SELECT DISTINCT 
         r.requisitionNumber,
         adr.state,
         p.name
     FROM
         copia.copia.Requisition AS r
     JOIN
         copia.copia.OrderedPanel AS op ON op.requisitionKey = r.requisitionKey
     JOIN
         copia.copia.panel AS p ON p.panelKey = op.panelKey
     JOIN
         copia.copia.Location AS loc ON loc.locationKey = r.orderingLocationKey
     JOIN
         copia.copia.Address AS adr ON adr.addressKey = loc.addressKey
     WHERE
         copia.dbo.FixDate(r.finalDeliveryStamp) BETWEEN '1/1/2021' AND '4/24/2021'
         AND p.name NOT LIKE '%body mass index%'
         AND p.name NOT LIKE '%hold account%') p 
    PIVOT 
        (SUM(adr.state) 
            FOR p.name IN (['TaqPath COVID-19 rRT-PCR Assay'],
                           ['TaqPath COVID-19 rRT-PCR Assay (Saliva Specimen)'],
                           ['TaqPath COVID-19 rRT-PCR Assay (Throat Specimen)'],
                           ['Beckman Access SARS-CoV-2 IgG Test'],
                           ['PSA, Free'],
                           ['Colorectal Cancer Current Risk'])
        ) AS PVT;

What am I doing wrong?我究竟做错了什么? I get these errors:我收到这些错误:

Msg 107, Level 15, State 1, Line 34消息 107,第 15 级,State 1,第 34 行
The column prefix 'adr' does not match with a table name or alias name used in the query.列前缀“adr”与查询中使用的表名或别名不匹配。

Msg 4104, Level 16, State 1, Line 2消息 4104,第 16 级,State 1,第 2 行
The multi-part identifier "adr.state" could not be bound.无法绑定多部分标识符“adr.state”。

In review, what I need is a matrix where the columns are the test names and the rows are each type of state, where the values are the sum of test types that match that state.回顾一下,我需要一个矩阵,其中列是测试名称,行是 state 的每种类型,其中值是与 state 匹配的测试类型的总和。

Thank you so much!太感谢了!

In your outer query adr will not be recognized.在您的外部查询中,将无法识别adr Instead use state only:而是仅使用state

SELECT
     state,
    'TaqPath COVID-19 rRT-PCR Assay',
    'TaqPath COVID-19 rRT-PCR Assay (Saliva Specimen)',
    'TaqPath COVID-19 rRT-PCR Assay (Throat Specimen)',
    'Beckman Access SARS-CoV-2 IgG Test',
    'PSA, Free',
    'Colorectal Cancer Current Risk'
FROM
    (SELECT DISTINCT 
         r.requisitionNumber,
         adr.state,
         p.name
     FROM
         copia.copia.Requisition AS r
     JOIN
         copia.copia.OrderedPanel AS op ON op.requisitionKey = r.requisitionKey
     JOIN
         copia.copia.panel AS p ON p.panelKey = op.panelKey
     JOIN
         copia.copia.Location AS loc ON loc.locationKey = r.orderingLocationKey
     JOIN
         copia.copia.Address AS adr ON adr.addressKey = loc.addressKey
     WHERE
         copia.dbo.FixDate(r.finalDeliveryStamp) BETWEEN '1/1/2021' AND '4/24/2021'
         AND p.name NOT LIKE '%body mass index%'
         AND p.name NOT LIKE '%hold account%') p 
    PIVOT 
        (SUM(adr.state) 
            FOR p.name IN (['TaqPath COVID-19 rRT-PCR Assay'],
                           ['TaqPath COVID-19 rRT-PCR Assay (Saliva Specimen)'],
                           ['TaqPath COVID-19 rRT-PCR Assay (Throat Specimen)'],
                           ['Beckman Access SARS-CoV-2 IgG Test'],
                           ['PSA, Free'],
                           ['Colorectal Cancer Current Risk'])
        ) AS PVT;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM