计算 SQL 服务器中的条目和数据透视表
Counting entries and pivot tables in SQL Server
我有一个关于枢轴 tables 和总结的问题
源select语句输出如下结构
Customer|ProductID
A |1
A |4
B |3
C |2
D |3
D |3
D |4
我想将其转换为具有以下结构的 table:
Customer|Count|1|2|3|4
A |2 |1|0|0|1
B |1 |0|0|1|0
C |1 |0|1|0|0
D |3 |1|0|1|1
"Count"应该是客户购买的产品数量,但我无法让它工作:(
这里是 SQL:
SELECT customerId, CustomerNo, [1], [2], [3], [4] FROM
(SELECT id AS customerId,
CustomerNo,
CASE ProductType
WHEN 1 THEN 1 /* Games */
WHEN 2 THEN 2 /* Cards */
WHEN 4 THEN 3 /* Gadgets */
WHEN 5 THEN 3 /* Accessories */
WHEN 6 THEN 4 /* Smartsphone acc. */
ELSE 255
END AS Products,
FROM
ProductSales
) AS SourceTable
PIVOT (
COUNT(Products)
FOR Products IN ([1], [2], [3], [4])
) AS PivotTable
这工作正常,但我需要在第一个 SELECT 语句中添加某种 COUNT 以计算每个客户拥有的记录数。
任何帮助将不胜感激。提前致谢:)
/汉斯
尝试使用条件聚合而不是数据透视表:
SELECT t.customerid,
COUNT(*) as cnt,
MAX(CASE WHEN t.productid = 1 THEN 1 ELSE 0 END) as first,
MAX(CASE WHEN t.productid = 2 THEN 1 ELSE 0 END) as second,
MAX(CASE WHEN t.productid = 3 THEN 1 ELSE 0 END) as third
FROM YourTable t
GROUP BY t.customerid
根据您当前的查询,使用 PIVOT 没有问题。
只需将以下列添加到 SELECT.
[1]+[2]+[3]+[4] as [Count]
演示
create table t (customer char(1),ProductID int)
insert into t values ('A',1),('A',4),('B',3),('C',2),('D',3),('D',3),('D',4)
select customer
,[1]+[2]+[3]+[4] as [Count]
,[1],[2],[3],[4]
from t pivot (count(ProductID) for ProductID in ([1],[2],[3],[4])) t
+----------+-------+---+---+---+---+
| customer | Count | 1 | 2 | 3 | 4 |
+----------+-------+---+---+---+---+
| A | 2 | 1 | 0 | 0 | 1 |
+----------+-------+---+---+---+---+
| B | 1 | 0 | 0 | 1 | 0 |
+----------+-------+---+---+---+---+
| C | 1 | 0 | 1 | 0 | 0 |
+----------+-------+---+---+---+---+
| D | 3 | 0 | 0 | 2 | 1 |
+----------+-------+---+---+---+---+
我有一个关于枢轴 tables 和总结的问题
源select语句输出如下结构
Customer|ProductID
A |1
A |4
B |3
C |2
D |3
D |3
D |4
我想将其转换为具有以下结构的 table:
Customer|Count|1|2|3|4
A |2 |1|0|0|1
B |1 |0|0|1|0
C |1 |0|1|0|0
D |3 |1|0|1|1
"Count"应该是客户购买的产品数量,但我无法让它工作:(
这里是 SQL:
SELECT customerId, CustomerNo, [1], [2], [3], [4] FROM
(SELECT id AS customerId,
CustomerNo,
CASE ProductType
WHEN 1 THEN 1 /* Games */
WHEN 2 THEN 2 /* Cards */
WHEN 4 THEN 3 /* Gadgets */
WHEN 5 THEN 3 /* Accessories */
WHEN 6 THEN 4 /* Smartsphone acc. */
ELSE 255
END AS Products,
FROM
ProductSales
) AS SourceTable
PIVOT (
COUNT(Products)
FOR Products IN ([1], [2], [3], [4])
) AS PivotTable
这工作正常,但我需要在第一个 SELECT 语句中添加某种 COUNT 以计算每个客户拥有的记录数。
任何帮助将不胜感激。提前致谢:)
/汉斯
尝试使用条件聚合而不是数据透视表:
SELECT t.customerid,
COUNT(*) as cnt,
MAX(CASE WHEN t.productid = 1 THEN 1 ELSE 0 END) as first,
MAX(CASE WHEN t.productid = 2 THEN 1 ELSE 0 END) as second,
MAX(CASE WHEN t.productid = 3 THEN 1 ELSE 0 END) as third
FROM YourTable t
GROUP BY t.customerid
根据您当前的查询,使用 PIVOT 没有问题。
只需将以下列添加到 SELECT.
[1]+[2]+[3]+[4] as [Count]
演示
create table t (customer char(1),ProductID int)
insert into t values ('A',1),('A',4),('B',3),('C',2),('D',3),('D',3),('D',4)
select customer
,[1]+[2]+[3]+[4] as [Count]
,[1],[2],[3],[4]
from t pivot (count(ProductID) for ProductID in ([1],[2],[3],[4])) t
+----------+-------+---+---+---+---+ | customer | Count | 1 | 2 | 3 | 4 | +----------+-------+---+---+---+---+ | A | 2 | 1 | 0 | 0 | 1 | +----------+-------+---+---+---+---+ | B | 1 | 0 | 0 | 1 | 0 | +----------+-------+---+---+---+---+ | C | 1 | 0 | 1 | 0 | 0 | +----------+-------+---+---+---+---+ | D | 3 | 0 | 0 | 2 | 1 | +----------+-------+---+---+---+---+