T-SQL 2008 中的 Unpivot 与 Union 查询

发布于 2024-07-26 09:05:57 字数 4480 浏览 5 评论 0原文

我从中检索数据的数据库的表结构如下

表:ClientSales

ClientSalesId                 int identity (1, 1) (PK)
ClientId                      int (FK)
TermId                        int (FK)
StudentType1Population        int
StudentType1Adjustment        int
StudentType1Sales             int
StudentType1SalesAdjustment   int
StudentType2Population        int
StudentType2Adjustment        int
StudentType2Sales             int
StudentType2SalesAdjustment   int
StudentType3Population        int
StudentType3Adjustment        int
StudentType3Sales             int
StudentType3SalesAdjustment   int
StudentType4Population        int
StudentType4Adjustment        int
StudentType4Sales             int
StudentType4SalesAdjustment   int
StudentType5Population        int
StudentType5Adjustment        int
StudentType5Sales             int
StudentType5SalesAdjustment   int

我必须在报告中显示它,如下所示

对于 ClientId = 1 和 Term Id = 1

                Population   PopulationAdjustment  Sales  SalesAdjustment
StudentType1    313          18                    123    22
StudentType2    233          14                    156    33
StudentType3    234          12                    112    41
StudentType4    233          13                    198    29
StudentType5    343          10                    134    36

我可以通过两种方式执行此操作


SELECT
       'StudentType1'              as DemographicType
       StudentType1Population      as Population,
       StudentType1Adjustment      as PopulationAdjustment,
       StudentType1Sales           as Sales,
       StudentType1SalesAdjustment as SalesAdjustment,
FROM ClientSales
WHERE 1=1
       AND ClientId = 1
       AND TermId = 1

UNION

SELECT
       'StudentType2'              as DemographicType
       StudentType2Population      as Population,
       StudentType2Adjustment      as PopulationAdjustment,
       StudentType2Sales           as Sales,
       StudentType2SalesAdjustment as SalesAdjustment,
FROM ClientSales
WHERE 1=1
       AND ClientId = 1
       AND TermId = 1

-- yada yada yada for the rest of the types...........


SELECT
       ClientId,
       Population
FROM
(
       SELECT
              ClientId,
              StudentType1Population,
              StudentType2Population,
              StudentType3Population,
              StudentType4Population,
              StudentType5Population
       FROM ClientSales
) PVTPopulation
UNPIVOT
(
       Population for StudentType IN
       (
              StudentType1Population,
              StudentType2Population,
              StudentType3Population,
              StudentType4Population,
              StudentType5Population
       )
) as UnPvtPopulation

INNER JOIN

(
       SELECT
              ClientId,
              StudentType1PopulationAdjustment,
              StudentType2PopulationAdjustment,
              StudentType3PopulationAdjustment,
              StudentType4PopulationAdjustment,
              StudentType5PopulationAdjustment
       FROM ClientSales
) PVTPopulation
UNPIVOT
(
       PopulationAdjustment for StudentType IN
       (
              StudentType1PopulationAdjustment,
              StudentType2PopulationAdjustment,
              StudentType3PopulationAdjustment,
              StudentType4PopulationAdjustment,
              StudentType5PopulationAdjustment
       )
) as UnPvtPopulationAdjustment

       ON UnPvtPopulationAdjustment.ClientSalesId = UnPvtPopulation.ClientSalesId
       AND REPLACE (UnPvtPopulationAdjustment.StudentType, 'PopulationAdjustment', '') = REPLACE (UnPvtPopulation.StudentType, 'Population', '')

INNER JOIN

(
       SELECT
              ClientId,
              StudentType1Sales,
              StudentType2Sales,
              StudentType3Sales,
              StudentType4Sales,
              StudentType5Sales
       FROM ClientSales
) PVTSales
UNPIVOT
(
       Sales for StudentType IN
       (
              StudentType1Sales,
              StudentType2Sales,
              StudentType3Sales,
              StudentType4Sales,
              StudentType5Sales
       )
) as UnPvtSales

       ON UnPvtSales.ClientSalesId = UnPvtPopulation.ClientSalesId
       AND REPLACE (UnPvtSales.StudentType, 'Sales', '') = REPLACE (UnPvtPopulation.StudentType, 'Population', '')

所以这里有问题:

  • 作为最佳实践,我应该使用 UNPIVOT 还是 UNION
  • 有没有更好的方法来编写这个 UNPIVOT?

The database that I am retrieving data from has the table structure like this

Table: ClientSales

ClientSalesId                 int identity (1, 1) (PK)
ClientId                      int (FK)
TermId                        int (FK)
StudentType1Population        int
StudentType1Adjustment        int
StudentType1Sales             int
StudentType1SalesAdjustment   int
StudentType2Population        int
StudentType2Adjustment        int
StudentType2Sales             int
StudentType2SalesAdjustment   int
StudentType3Population        int
StudentType3Adjustment        int
StudentType3Sales             int
StudentType3SalesAdjustment   int
StudentType4Population        int
StudentType4Adjustment        int
StudentType4Sales             int
StudentType4SalesAdjustment   int
StudentType5Population        int
StudentType5Adjustment        int
StudentType5Sales             int
StudentType5SalesAdjustment   int

I have to display it unpivoted in a report as follows

For ClientId = 1 and Term Id = 1


Population PopulationAdjustment Sales SalesAdjustment
StudentType1 313 18 123 22
StudentType2 233 14 156 33
StudentType3 234 12 112 41
StudentType4 233 13 198 29
StudentType5 343 10 134 36

I can do this two ways


SELECT
       'StudentType1'              as DemographicType
       StudentType1Population      as Population,
       StudentType1Adjustment      as PopulationAdjustment,
       StudentType1Sales           as Sales,
       StudentType1SalesAdjustment as SalesAdjustment,
FROM ClientSales
WHERE 1=1
       AND ClientId = 1
       AND TermId = 1

UNION

SELECT
       'StudentType2'              as DemographicType
       StudentType2Population      as Population,
       StudentType2Adjustment      as PopulationAdjustment,
       StudentType2Sales           as Sales,
       StudentType2SalesAdjustment as SalesAdjustment,
FROM ClientSales
WHERE 1=1
       AND ClientId = 1
       AND TermId = 1

-- yada yada yada for the rest of the types...........

OR


SELECT
       ClientId,
       Population
FROM
(
       SELECT
              ClientId,
              StudentType1Population,
              StudentType2Population,
              StudentType3Population,
              StudentType4Population,
              StudentType5Population
       FROM ClientSales
) PVTPopulation
UNPIVOT
(
       Population for StudentType IN
       (
              StudentType1Population,
              StudentType2Population,
              StudentType3Population,
              StudentType4Population,
              StudentType5Population
       )
) as UnPvtPopulation

INNER JOIN

(
       SELECT
              ClientId,
              StudentType1PopulationAdjustment,
              StudentType2PopulationAdjustment,
              StudentType3PopulationAdjustment,
              StudentType4PopulationAdjustment,
              StudentType5PopulationAdjustment
       FROM ClientSales
) PVTPopulation
UNPIVOT
(
       PopulationAdjustment for StudentType IN
       (
              StudentType1PopulationAdjustment,
              StudentType2PopulationAdjustment,
              StudentType3PopulationAdjustment,
              StudentType4PopulationAdjustment,
              StudentType5PopulationAdjustment
       )
) as UnPvtPopulationAdjustment

       ON UnPvtPopulationAdjustment.ClientSalesId = UnPvtPopulation.ClientSalesId
       AND REPLACE (UnPvtPopulationAdjustment.StudentType, 'PopulationAdjustment', '') = REPLACE (UnPvtPopulation.StudentType, 'Population', '')

INNER JOIN

(
       SELECT
              ClientId,
              StudentType1Sales,
              StudentType2Sales,
              StudentType3Sales,
              StudentType4Sales,
              StudentType5Sales
       FROM ClientSales
) PVTSales
UNPIVOT
(
       Sales for StudentType IN
       (
              StudentType1Sales,
              StudentType2Sales,
              StudentType3Sales,
              StudentType4Sales,
              StudentType5Sales
       )
) as UnPvtSales

       ON UnPvtSales.ClientSalesId = UnPvtPopulation.ClientSalesId
       AND REPLACE (UnPvtSales.StudentType, 'Sales', '') = REPLACE (UnPvtPopulation.StudentType, 'Population', '')

So here are the questions:

  • As a best practice, should I use UNPIVOT or UNION
  • Is there a better way to write this UNPIVOT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甚是思念 2024-08-02 09:05:57

我发现 UNPIVOT 就像一个 where 子句..您可以根据需要使用任意多个

SELECT Col1, Col2, Unp1, Unp2, Unp3
FROM TBL
UNPIVOT (XX For Unp1 (ColXX1, ColXX2))
UNPIVOT (YY For Unp2 (ColYY1, ColYY2))
UNPIVOT (ZZ For Unp2 (ColZZ1, ColZZ2))

I found out that an UNPIVOT is just like a where clause.. you can use as many as you need

SELECT Col1, Col2, Unp1, Unp2, Unp3
FROM TBL
UNPIVOT (XX For Unp1 (ColXX1, ColXX2))
UNPIVOT (YY For Unp2 (ColYY1, ColYY2))
UNPIVOT (ZZ For Unp2 (ColZZ1, ColZZ2))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文