SELECT
uid AS '用户 ID',
(CASE sid
WHEN 10001 THEN 'A'
WHEN 10002 THEN 'B'
WHEN 10003 THEN 'C'
WHEN 10004 THEN 'D'
END) AS '商品 ID',
COUNT(*) AS '数量'
FROM MyTable
WHERE sid IN (10001, 10002, 10003, 10004)
GROUP BY uid, sid
ORDER BY uid, sid
用户 ID | 商品 ID | 数量 |
---|---|---|
4 | A | 102 |
4 | B | 5 |
4 | C | 57 |
4 | D | 227 |
5 | A | 1 |
5 | C | 1 |
5 | D | 1 |
7 | A | 10 |
7 | B | 1 |
7 | C | 11 |
7 | D | 1 |
105 | A | 7 |
105 | B | 1 |
105 | C | 1 |
105 | D | 2 |
142 | A | 1 |
142 | B | 1 |
142 | C | 1 |
260 | C | 10 |
437 | A | 9 |
437 | B | 1 |
437 | C | 1 |
437 | D | 2 |
722 | A | 1 |
823 | A | 2 |
1
chenxytw 2022-07-16 23:30:02 +08:00
虽然你发错节点了,但我想你要找的应该是类型 group having 这个语句,用作聚合结果的筛选。不过 SQLServer 里面叫啥自己搜吧。
|