我有下表来转动。
表格:
CREATE TABLE Emp_Month
(
EMPID int,
[Month] varchar(10)
);
插入:
INSERT INTO Emp_Month VALUES(101,'July');
INSERT INTO Emp_Month VALUES(102,'June');
INSERT INTO Emp_Month VALUES(103,'May');
INSERT INTO Emp_Month VALUES(104,'April');
INSERT INTO Emp_Month VALUES(105,'March');
INSERT INTO Emp_Month VALUES(201,'July');
INSERT INTO Emp_Month VALUES(202,'July');
INSERT INTO Emp_Month VALUES(203,'June');
INSERT INTO Emp_Month VALUES(204,'July');
INSERT INTO Emp_Month VALUES(205,'June');
INSERT INTO Emp_Month VALUES(301,'January');
INSERT INTO Emp_Month VALUES(302,'January');
INSERT INTO Emp_Month VALUES(303,'February');
INSERT INTO Emp_Month VALUES(304,'February');
INSERT INTO Emp_Month VALUES(305,'February');
预期输出:我想打印COUNT(last_months_from_current_date)
Last_6_Month Last_5_Month Last_4_Month Last_3_Month Last_2_Month Last_1_Month
--------------------------------------------------------------------------------------------
13 10 9 8 7 4
我的尝试:
select *
from
(
SELECT t.Month,t.[Month] as Mon
FROM Emp_Month t
) src
pivot
(
COUNT(Mon)
for [Month] in ([Last_6_Month],[Last_5_Month],[Last_4_Month],[Last_3_Month],[Last_2_Month],[Last_1_Month])
) piv;
但是获得所有零值。
答案 0 :(得分:1)
@MAK这是您尝试的查询:
select *
from
(
SELECT t.Month,t.[Month] as Mon
FROM Emp_Month t
) src
pivot
(
COUNT(Mon)
for [Month] in ([Last_6_Month],[Last_5_Month],[Last_4_Month],[Last_3_Month],[Last_2_Month],[Last_1_Month])
) piv;
查询中的以下行需要在 MON 列中包含实际值。从您的示例中,似乎实际的月份名称在那里,虽然您输入自己的值,因此数字将为0.尝试输入您实际拥有的月份名称。
( [Last_6_Month],[Last_5_Month],[Last_4_Month],[Last_3_Month],[Last_2_Month],[Last_1_Month] )中的[月]
类似的东西:
[月],[[月],[七月],[四月])答案 1 :(得分:1)
你缺少的是要做一个总计,你必须有一些方法来订购你的设置。因此,这里有一个月的数字建设。这不是最好的,但它正在为这个例子而努力。
在分组并进行总计之后,一个支点有点超过顶部,但是你要求一个,所以你去了。希望它有所帮助。
(*我对接受的答案进行了一些更改,使得数据库的列标题自动更新。否则,您需要每月修改一次)
$2a$08$LMXAGOARNn4XmnC/rQuhfujrWVwgK/RuHuGpLtXvcv/yruY1v3yXa
结果:
;WITH
MonthsCountedOrdered AS
(
SELECT count(EMPID) empCount, Month, Datediff(M, [Month] + ' 1, 2017', getdate()) + 1 monthsPast
FROM Emp_Month
GROUP BY [Month]
)
, RunningTotal AS
(
SELECT
Month
, SUM(empCount) OVER (Order By monthsPast ROWS UNBOUNDED PRECEDING) tot
, 'Last_' + cast(monthsPast as varchar(2)) + '_Month' as ColumnHeader
FROM
MonthsCountedOrdered
WHERE
monthsPast <= 6
)
SELECT *
FROM
(
SELECT t.ColumnHeader,t.tot
FROM RunningTotal t
) src
pivot
(
Sum(tot)
for [ColumnHeader] in (Last_6_Month,Last_5_Month,Last_4_Month,Last_3_Month,Last_2_Month,Last_1_Month)
) piv;
答案 2 :(得分:0)
我首先要计算的是EMPID字段而不是月份,尽管两种情况都适用于这种情况。
此外,由于月份列中的所有数据都与您的数据透视表中的列列表不匹配,因此您无需转动。如果您想查看过去6个月的数据透视,那么您必须添加新列或创建视图或子查询以添加显示[Last_6_Month]
值的列如果您只想数月,那么您可以随时使用
SELECT *
FROM
(
SELECT t.Month,EMPID
FROM Emp_Month t
) src
PIVOT
(
COUNT(EMPID)
FOR [Month] in (
[January],[February],[March],[April],[May],[June],[July],
[August],[September],[October],[November],[December]
)
) piv;
答案 3 :(得分:0)
通过动态sql方法我这样做了,这可能是你需要的代码
DECLARE @DynamicCol NVARCHAR(max),
@Sql NVARCHAR(max)
SELECT @DynamicCol = Stuff((SELECT DISTINCT ', ' + Quotename([month])
FROM #emp_month
FOR xml path ('')), 1, 1, '')
SET @Sql=N'SELECT ' + @DynamicCol + 'FROM
(
SELECT t.[MONTH],COUNT(t.[Month]) AS Mon
FROM #Emp_Month t GROUP BY [MONTH]
)AS SRC
PIVOT
(
MAX([Mon]) FOR [Month] IN (' + @DynamicCol + ')
) AS PVT '
PRINT @Sql
EXEC (@Sql)
您也可以设置以下条件
SET @Sql=N'SELECT ' + @DynamicCol + 'FROM
(
SELECT t.[MONTH],t.[Month] AS Mon
FROM #Emp_Month t
)AS SRC
PIVOT
(
COUNT([Mon]) FOR [Month] IN (' + @DynamicCol + ')
) AS PVT '