这里的
示例是一个方法:
- 首先获得所有产品都在列(以有序的方式)
- 第二组他们通过这一列获得cou NT
- 分离出来,作为列
下面是查询
--Create table t1 ([order] int, row int, art varchar(30))
--insert into t1 values
--(1,1,'A'),
--(1,2,'B'),
--(1,3,'C'),
--(2,1,'A'),
--(2,2,'B'),
--(3,1,'C'),
--(3,2,'D'),
--(4,1,'A'),
--(4,2,'B'),
--(5,1,'B'),
--(5,2,'A')
select
count(1) as [count],
articles,
max(numberOfArticles) as numberOfArticles
from
(
select
[order],
count(1) as numberOfArticles,
Stuff(
(
SELECT
',' + art
FROM t1 AS i
WHERE i.[order]=o.[order]
ORDER by art asc
FOR XML PATH ('')
),
1, 1, '') as articles
from t1 o
group by [order]
)t
group by articles
--drop table t1
输出:
仅限于3种可能的'Art'值多少? –