在sqlserver中按类别分组数据 [英] group data by category in sqlserver
问题描述
我有2个表名customer_master,其名称如下:
cust_id cust_name cust_category
1 abc A
2 xyz B
3 qwe C
此表中的
我将客户划分为3类(A,B,C)......
i希望每个类别的客户和客户总数如下所示
来自上面的数据:
totalcuatomer ABC
3 1 1 1
我该怎么办?
i试试这个:
通过cust_category
<从客户群中选择cust_id,count(cust_category) blockquote>尝试使用PIVOT
SELECT SUM(A + B) + C) AS total,SUM(A) AS A,SUM(B) AS B,SUM(C) AS C FROM
(
SELECT * FROM customer
)ss PIVOT
(
COUNT(cust_id) FOR cust_category in (A,B,C)
) as pvt
你好,
试试这个查询。
选择
(选择 count(*)来自 customer) as TotalCustomer,
(选择 COUNT(*)来自 customer where cus_category = ' A') as A,
(选择 COUNT(*)来自 customer 其中 cus_category = ' B') as B,
( select COUNT(*)来自客户其中 cus_category = ' C') as C
谢谢
i have 2 table name customer_master which have below:
cust_id cust_name cust_category
1 abc A
2 xyz B
3 qwe C
in this table i divided customer in 3 category ("A,B,C")......
i want that total customer and customer in each category like below
Exmple data from above:
totalcuatomer A B C
3 1 1 1
how can i do ?
i try this:
select cust_id,count(cust_category) from customer group by cust_category
Try this with PIVOT
SELECT SUM(A+B+C) AS total, SUM(A) AS A,SUM(B) AS B,SUM(C) AS C FROM ( SELECT * FROM customer ) ss PIVOT ( COUNT(cust_id) FOR cust_category in(A,B,C) ) as pvt
Hello ,
Try this query .
select (select count(*) from customer )as TotalCustomer , (select COUNT(*) from customer where cus_category='A')as A , (select COUNT(*) from customer where cus_category='B')as B , (select COUNT(*) from customer where cus_category='C') as C
thanks
这篇关于在sqlserver中按类别分组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!