sqlserver 2008 中没有聚合的数据透视所需的帮助

发布于 2024-11-14 13:43:09 字数 622 浏览 4 评论 0原文

我需要按如下方式旋转表格: 初始表是这样的

GEO PRD PACK    DATATYPE    Sales1  Sales2

T1  P1  M1  22          1     2

T1  P1  M1  23          2     8

T1  P1  M1  24          3     5

T2  P2  M2  22          3     2

T2  P2  M2  23          1     4

T2  P2  M2  24          1     7

,我想要的是:

GEO PRD PACK 22_Sales1 22_Sales2 23_Sales1 23_Sales2 24_Sales1 24_Sales2

T1  P1  M1       1         2         2         8         3         5

T2  P2  M2       3         2         1         4         1         7

这里,不同的数据类型是固定的:总是:22、23、24,不多也不少。

请问,有人可以帮我如何编写正确的查询吗?

I need to pivot a table as follows :
the initial table is like this

GEO PRD PACK    DATATYPE    Sales1  Sales2

T1  P1  M1  22          1     2

T1  P1  M1  23          2     8

T1  P1  M1  24          3     5

T2  P2  M2  22          3     2

T2  P2  M2  23          1     4

T2  P2  M2  24          1     7

and what I want is :

GEO PRD PACK 22_Sales1 22_Sales2 23_Sales1 23_Sales2 24_Sales1 24_Sales2

T1  P1  M1       1         2         2         8         3         5

T2  P2  M2       3         2         1         4         1         7

here, the distinct DATATYPE is fixed : always will be : 22, 23, 24, no less and no more.

Please, can anyone help me how to write the proper query ?

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

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

发布评论

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

评论(3

給妳壹絲溫柔 2024-11-21 13:43:09
select geo, prd, pack,   
  sum(sales_22_1) sales_22_1,
  sum(sales_22_2) sales_22_2,
  sum(sales_23_1) sales_23_1,
  sum(sales_23_2) sales_23_2,
  sum(sales_24_1) sales_24_1, 
  sum(sales_24_2) sales_24_2 
from
(select geo, prd, pack, 
  (case when datatype = 22 then sales1
   else null
   end) as sales_22_1,
(case when datatype = 22 then sales2
   else null
   end) as sales_22_2,
(case when datatype = 23 then sales1
   else null
   end) as sales_23_1,
(case when datatype = 23 then sales2
   else null
   end) as sales_23_2,
(case when datatype = 24 then sales1
   else null
   end) as sales_24_1,
(case when datatype = 24 then sales2
   else null
   end) as sales_24_2
 from mytab)
group by geo, prd, pack

(未经测试,因为我没有可用的 SQL 服务器实例)。

select geo, prd, pack,   
  sum(sales_22_1) sales_22_1,
  sum(sales_22_2) sales_22_2,
  sum(sales_23_1) sales_23_1,
  sum(sales_23_2) sales_23_2,
  sum(sales_24_1) sales_24_1, 
  sum(sales_24_2) sales_24_2 
from
(select geo, prd, pack, 
  (case when datatype = 22 then sales1
   else null
   end) as sales_22_1,
(case when datatype = 22 then sales2
   else null
   end) as sales_22_2,
(case when datatype = 23 then sales1
   else null
   end) as sales_23_1,
(case when datatype = 23 then sales2
   else null
   end) as sales_23_2,
(case when datatype = 24 then sales1
   else null
   end) as sales_24_1,
(case when datatype = 24 then sales2
   else null
   end) as sales_24_2
 from mytab)
group by geo, prd, pack

(untested, since I don't have a SQL server instance available).

以为你会在 2024-11-21 13:43:09

最新版本的 SQL Server 具有PIVOT 功能

这是一个示例 如何在多个 SQL Server 版本中进行数据透视。

对于这个非常流行的 SO 问题,有很多很好的答案:SQL Server PIVOT 示例?

这里是该页面中的一个简单 PIVOT 语句的示例:

SELECT act AS 'Action', [View] as 'View', [Edit] as 'Edit'
FROM (
    SELECT act, cmd FROM data
) AS src
PIVOT (
    MAX(cmd) FOR cmd IN ([View], [Edit])
) AS pvt

Recent versions of SQL Server have a PIVOT function.

Here is an example of how to do pivoting in several SQL Server versions.

And there are many good answers to this very popular SO question: SQL Server PIVOT examples?

Here is an example from that page of what a simple PIVOT statement looks like:

SELECT act AS 'Action', [View] as 'View', [Edit] as 'Edit'
FROM (
    SELECT act, cmd FROM data
) AS src
PIVOT (
    MAX(cmd) FOR cmd IN ([View], [Edit])
) AS pvt
好多鱼好多余 2024-11-21 13:43:09

要对您的数据使用PIVOT,您将使用以下命令。这首先执行 UNPIVOT,然后执行 PIVOT 以获取所需格式的数据:

create table t1
(
    geo varchar(2),
    prd varchar(2),
    pack varchar(2),
    datatype int,
    sales1 int,
    sales2 int
)
insert into t1 values ('T1', 'P1', 'M1', 22, 1, 2)
insert into t1 values ('T1', 'P1', 'M1', 23, 2, 8)
insert into t1 values ('T1', 'P1', 'M1', 24, 3, 5)
insert into t1 values ('T2', 'P2', 'M2', 22, 3, 2)
insert into t1 values ('T2', 'P2', 'M2', 23, 1, 4)
insert into t1 values ('T2', 'P2', 'M2', 24, 1, 7)

select *
from 
(
    select geo, prd, pack, cast(datatype as varchar(5)) + '_' + col col,value
    from t1
    unpivot
    (
        value
        for col in (sales1, sales2)
    ) u
) x
pivot
(
    sum(value)
    for col in ([22_sales1], [22_sales2], [23_Sales1], [23_Sales2], [24_Sales1], [24_Sales2])
) p

请参阅 SQL Fiddle 演示

To use a PIVOT with your data you would use the following. This performs an UNPIVOT first and then a PIVOT to get the data in the format that you need:

create table t1
(
    geo varchar(2),
    prd varchar(2),
    pack varchar(2),
    datatype int,
    sales1 int,
    sales2 int
)
insert into t1 values ('T1', 'P1', 'M1', 22, 1, 2)
insert into t1 values ('T1', 'P1', 'M1', 23, 2, 8)
insert into t1 values ('T1', 'P1', 'M1', 24, 3, 5)
insert into t1 values ('T2', 'P2', 'M2', 22, 3, 2)
insert into t1 values ('T2', 'P2', 'M2', 23, 1, 4)
insert into t1 values ('T2', 'P2', 'M2', 24, 1, 7)

select *
from 
(
    select geo, prd, pack, cast(datatype as varchar(5)) + '_' + col col,value
    from t1
    unpivot
    (
        value
        for col in (sales1, sales2)
    ) u
) x
pivot
(
    sum(value)
    for col in ([22_sales1], [22_sales2], [23_Sales1], [23_Sales2], [24_Sales1], [24_Sales2])
) p

See SQL Fiddle with Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文