使用 group by 计算 sql
Count in sql with group by
我有 table temp 其中包含 4 列
作为
create table Temp
(
seqno int identity (1,1),
name varchar(20),
towncd numeric,
Counttown_cd,
)
我想写查询 return 镇的计数,但如果名称和 towncd 相同
然后计算在先前记录中添加的裹尸布
喜欢
1 man 0001
2 man 0001
3 test 0003
4 man 0001
5 man 0001
应该return
作为
1 man 0001 2
2 man 0001 null
3 test 0003 1
4 man 0001 2
5 man 0001 null
我尝试了以下查询:
SELECT p.seqno ,p.name,P.towncd ,COUNT(P.towncd )Counttown_cd
FROM temp P
GROUP BY P.name,P.towncd ,p.seqno
order by p.seqno
您对问题的修改为原始问题添加了空白和孤岛问题。这可以通过子查询中的两个 row_number()
来解决,如下所示:
select seqno, name, towncd
, Counttown_cd = case
when row_number() over (partition by name,towncd,grp order by seqno) = 1
then count(*) over (partition by name,towncd, grp)
else null
end
from (
select *
, grp = row_number() over (partition by name,towncd order by seqno)
- row_number() over (order by seqno)
from temp
) t
order by seqno
rextester 演示:http://rextester.com/VGXI71945
returns:
+-------+------+--------+--------------+
| seqno | name | towncd | Counttown_cd |
+-------+------+--------+--------------+
| 1 | man | 0001 | 2 |
| 2 | man | 0001 | NULL |
| 3 | test | 0003 | 1 |
| 4 | man | 0001 | 2 |
| 5 | man | 0001 | NULL |
+-------+------+--------+--------------+
原问题回答:
使用 case
表达式和两个 window 函数(row_number()
和 count(*) over()
)仅显示 name,towncd
第一个实例的计数:
select seqno, name, towncd
, Counttown_cd = case
when row_number() over (partition by name,towncd order by seqno) = 1
then count(*) over (partition by name,towncd)
else null
end
from temp
rextester 演示:http://rextester.com/NZSPR13395
returns:
+-------+------+--------+--------------+
| seqno | name | towncd | Counttown_cd |
+-------+------+--------+--------------+
| 1 | man | 0001 | 2 |
| 2 | man | 0001 | NULL |
| 3 | test | 0003 | 1 |
+-------+------+--------+--------------+
我有 table temp 其中包含 4 列 作为
create table Temp
(
seqno int identity (1,1),
name varchar(20),
towncd numeric,
Counttown_cd,
)
我想写查询 return 镇的计数,但如果名称和 towncd 相同 然后计算在先前记录中添加的裹尸布 喜欢
1 man 0001
2 man 0001
3 test 0003
4 man 0001
5 man 0001
应该return 作为
1 man 0001 2
2 man 0001 null
3 test 0003 1
4 man 0001 2
5 man 0001 null
我尝试了以下查询:
SELECT p.seqno ,p.name,P.towncd ,COUNT(P.towncd )Counttown_cd
FROM temp P
GROUP BY P.name,P.towncd ,p.seqno
order by p.seqno
您对问题的修改为原始问题添加了空白和孤岛问题。这可以通过子查询中的两个 row_number()
来解决,如下所示:
select seqno, name, towncd
, Counttown_cd = case
when row_number() over (partition by name,towncd,grp order by seqno) = 1
then count(*) over (partition by name,towncd, grp)
else null
end
from (
select *
, grp = row_number() over (partition by name,towncd order by seqno)
- row_number() over (order by seqno)
from temp
) t
order by seqno
rextester 演示:http://rextester.com/VGXI71945
returns:
+-------+------+--------+--------------+
| seqno | name | towncd | Counttown_cd |
+-------+------+--------+--------------+
| 1 | man | 0001 | 2 |
| 2 | man | 0001 | NULL |
| 3 | test | 0003 | 1 |
| 4 | man | 0001 | 2 |
| 5 | man | 0001 | NULL |
+-------+------+--------+--------------+
原问题回答:
使用 case
表达式和两个 window 函数(row_number()
和 count(*) over()
)仅显示 name,towncd
第一个实例的计数:
select seqno, name, towncd
, Counttown_cd = case
when row_number() over (partition by name,towncd order by seqno) = 1
then count(*) over (partition by name,towncd)
else null
end
from temp
rextester 演示:http://rextester.com/NZSPR13395
returns:
+-------+------+--------+--------------+
| seqno | name | towncd | Counttown_cd |
+-------+------+--------+--------------+
| 1 | man | 0001 | 2 |
| 2 | man | 0001 | NULL |
| 3 | test | 0003 | 1 |
+-------+------+--------+--------------+