回答編集履歴
2
調整
answer
CHANGED
@@ -15,4 +15,10 @@
|
|
15
15
|
) as t
|
16
16
|
order by id
|
17
17
|
|
18
|
+
```
|
19
|
+
|
20
|
+
- unionしない別解
|
21
|
+
```SQL
|
22
|
+
select id,status,(select case when status is null then 1 else count(*) end from tbl where status=t1.status) as cnt from tbl as t1
|
23
|
+
where not exists (select 1 from tbl where t1.id>id and t1.status=status)
|
18
24
|
```
|
1
参考
answer
CHANGED
@@ -1,1 +1,18 @@
|
|
1
|
-
そもそもstatusでgroup byしてidを表示することはできませんよ
|
1
|
+
そもそもstatusでgroup byしてidを表示することはできませんよ
|
2
|
+
|
3
|
+
# sample
|
4
|
+
|
5
|
+
```SQL
|
6
|
+
create table tbl(id int,status int null);
|
7
|
+
insert into tbl values(1,1000),(2,1000),(3,2000),(4,2000),(5,null),(6,null),(7,3000);
|
8
|
+
```
|
9
|
+
|
10
|
+
- 集計
|
11
|
+
```SQL
|
12
|
+
select * from(
|
13
|
+
select min(id) as id,status,count(*) as cnt from tbl where status is not null group by status
|
14
|
+
union all select id,status,1 from tbl where status is null
|
15
|
+
) as t
|
16
|
+
order by id
|
17
|
+
|
18
|
+
```
|