我该如何转动这个(How can I pivot this)

编程入门 行业动态 更新时间:2024-10-27 00:33:09
我该如何转动这个(How can I pivot this)

我试图转动一些信息,但无法解决它。 请帮我。 我正在使用SQL Express 2012.我写了这个例子:

CREATE TABLE #temp ( Mes varchar(2), Qty int, Modelo varchar(50) ) insert into #temp values('01', 11, 'TC') insert into #temp values('01', 21, 'TC2') insert into #temp values('01', 22, 'Plus2') insert into #temp values('02', 12, 'TC') insert into #temp values('02', 22, 'TC2') insert into #temp values('02', 32, 'Plus2') insert into #temp values('03', 13, 'TC') insert into #temp values('03', 23, 'TC2') insert into #temp values('03', 33, 'Plus2') SELECT Modelo, [AAA] as Mes1, [BBB] as Mes2, [CCC] as Mes3 from ( select Mes, Modelo, Qty from #temp ) x PIVOT ( SUM(Qty) FOR Mes IN([AAA], [BBB], [CCC]) ) as p drop table #temp

结果是:

Modelo | Mes1 | Mes2 | Mes3 ------------------------------- Plus2 | NULL | NULL | NULL TC | NULL | NULL | NULL TC2 | NULL | NULL | NULL

我这里有2个问题,第一个没有导致交集模型(行中的Modelo)和列(aaa,bbb,ccc)中的月(Mes)

第二个是如何直接将月份(Mes)名称放在列中而不使用Mes1等。

感谢您的任何帮助。

I'm trying to pivot some info and can't resolve it. Please help me. I'm using SQL Express 2012. I wrote this example:

CREATE TABLE #temp ( Mes varchar(2), Qty int, Modelo varchar(50) ) insert into #temp values('01', 11, 'TC') insert into #temp values('01', 21, 'TC2') insert into #temp values('01', 22, 'Plus2') insert into #temp values('02', 12, 'TC') insert into #temp values('02', 22, 'TC2') insert into #temp values('02', 32, 'Plus2') insert into #temp values('03', 13, 'TC') insert into #temp values('03', 23, 'TC2') insert into #temp values('03', 33, 'Plus2') SELECT Modelo, [AAA] as Mes1, [BBB] as Mes2, [CCC] as Mes3 from ( select Mes, Modelo, Qty from #temp ) x PIVOT ( SUM(Qty) FOR Mes IN([AAA], [BBB], [CCC]) ) as p drop table #temp

The result for this is:

Modelo | Mes1 | Mes2 | Mes3 ------------------------------- Plus2 | NULL | NULL | NULL TC | NULL | NULL | NULL TC2 | NULL | NULL | NULL

I have 2 problems here, first one no result in the intersection Model (Modelo in rows) and Month (Mes) in columns (aaa, bbb, ccc)

The second one, is how to put the month (Mes) name in the column directly without using as Mes1, etc.

Thank you for any help.

最满意答案

您在Mes列中插入('01','02','03')。 像'AAA','BBB'和'CCC'这样的值是无效值,将返回null。 所以你必须改变你的代码如下:

CREATE TABLE #temp ( Mes varchar(2), Qty int, Modelo varchar(50) ) insert into #temp values('01', 11, 'TC') insert into #temp values('01', 21, 'TC2') insert into #temp values('01', 22, 'Plus2') insert into #temp values('02', 12, 'TC') insert into #temp values('02', 22, 'TC2') insert into #temp values('02', 32, 'Plus2') insert into #temp values('03', 13, 'TC') insert into #temp values('03', 23, 'TC2') insert into #temp values('03', 33, 'Plus2') SELECT Modelo, [01] as Mes1, [02] as Mes2, [03] as Mes3 from ( select Mes, Modelo, Qty from #temp ) x PIVOT ( SUM(Qty) FOR Mes IN([01], [02], [03]) ) as p drop table #temp

它将返回:

Modelo | Mes1 | Mes2 | Mes3 --------+-------+-------+----------- Plus2 | 22 | 32 | 33 TC | 11 | 12 | 13 TC2 | 21 | 22 | 23

You inserted ('01', '02', '03') in Mes column. Values like 'AAA', 'BBB' and 'CCC' are invalid values and will return null. So you must chenge your code as follow:

CREATE TABLE #temp ( Mes varchar(2), Qty int, Modelo varchar(50) ) insert into #temp values('01', 11, 'TC') insert into #temp values('01', 21, 'TC2') insert into #temp values('01', 22, 'Plus2') insert into #temp values('02', 12, 'TC') insert into #temp values('02', 22, 'TC2') insert into #temp values('02', 32, 'Plus2') insert into #temp values('03', 13, 'TC') insert into #temp values('03', 23, 'TC2') insert into #temp values('03', 33, 'Plus2') SELECT Modelo, [01] as Mes1, [02] as Mes2, [03] as Mes3 from ( select Mes, Modelo, Qty from #temp ) x PIVOT ( SUM(Qty) FOR Mes IN([01], [02], [03]) ) as p drop table #temp

It will return:

Modelo | Mes1 | Mes2 | Mes3 --------+-------+-------+----------- Plus2 | 22 | 32 | 33 TC | 11 | 12 | 13 TC2 | 21 | 22 | 23

更多推荐

本文发布于:2023-08-06 17:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453766.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我该   pivot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!