gpt4 book ai didi

c# - C# 中的数据透视表

转载 作者:可可西里 更新时间:2023-11-01 08:46:34 25 4
gpt4 key购买 nike

我需要在 .net 中创建数据透视表。不能使用任何第三方控件(除非它是免费的)。我试图找到一般说明如何创建数据透视表(算法或步骤)的文档,但几乎所有内容都与 excel 相关。 有谁知道如何在 C# 中创建数据透视表???谢谢

最佳答案

在这里帮忙 http://msdn.microsoft.com/en-us/library/aa172756%28SQL.80%29.aspx

实际表:

Year   Quarter  Amount    
1990 1 1.1
1990 2 1.2
1990 3 1.3
1990 4 1.4
1991 1 2.1
1991 2 2.2
1991 3 2.3
1991 4 2.4
1992 4 2.4

期望的输出:(这里 Q 代表季度)

Year       Q-1       Q-2       Q-3       Q-4      
1990 1.1 1.2 1.3 1.4
1991 2.1 2.2 2.3 2.4
1992 0.0 0.0 0.0 2.4

查询:

Use Northwind    
GO

CREATE TABLE Pivot
( Year SMALLINT,
Quarter TINYINT,
Amount DECIMAL(2,1) )
GO

INSERT INTO Pivot VALUES (1990, 1, 1.1)
INSERT INTO Pivot VALUES (1990, 2, 1.2)
INSERT INTO Pivot VALUES (1990, 3, 1.3)
INSERT INTO Pivot VALUES (1990, 4, 1.4)
INSERT INTO Pivot VALUES (1991, 1, 2.1)
INSERT INTO Pivot VALUES (1991, 2, 2.2)
INSERT INTO Pivot VALUES (1991, 3, 2.3)
INSERT INTO Pivot VALUES (1991, 4, 2.4)
INSERT INTO Pivot VALUES (1992, 4, 2.4)
GO

SELECT * FROM Pivot
GO

SELECT Year,
SUM(CASE Quarter WHEN 1 THEN Amount ELSE 0 END) AS Q1,
SUM(CASE Quarter WHEN 2 THEN Amount ELSE 0 END) AS Q2,
SUM(CASE Quarter WHEN 3 THEN Amount ELSE 0 END) AS Q3,
SUM(CASE Quarter WHEN 4 THEN Amount ELSE 0 END) AS Q4
FROM Northwind.dbo.Pivot
GROUP BY Year
GO

另一个输出:

SELECT P1.*, (P1.Q1 + P1.Q2 + P1.Q3 + P1.Q4) AS YearTotal    
FROM (SELECT Year,
SUM(CASE P.Quarter WHEN 1 THEN P.Amount ELSE 0 END) AS Q1,
SUM(CASE P.Quarter WHEN 2 THEN P.Amount ELSE 0 END) AS Q2,
SUM(CASE P.Quarter WHEN 3 THEN P.Amount ELSE 0 END) AS Q3,
SUM(CASE P.Quarter WHEN 4 THEN P.Amount ELSE 0 END) AS Q4
FROM Pivot AS P
GROUP BY P.Year) AS P1
GO

关于c# - C# 中的数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1069677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com