回答編集履歴

1

ちょうせい

2021/09/01 05:32

投稿

yambejp
yambejp

スコア116734

test CHANGED
@@ -1,6 +1,4 @@
1
- 最初からrank付用のカラムをつくっておくか、viewで対応すると楽です
1
+ 調整しました
2
-
3
-
4
2
 
5
3
  ```SQL
6
4
 
@@ -28,14 +26,6 @@
28
26
 
29
27
  (9,5,'n51','n511');
30
28
 
31
-
32
-
33
- create view v as
34
-
35
- select *,(select count(*)+1 from tbl where id<t1.id and k=t1.k) as rank
36
-
37
- from tbl as t1;
38
-
39
29
  ```
40
30
 
41
31
 
@@ -58,8 +48,12 @@
58
48
 
59
49
  max(case when rank=3 then nameB else null end) as nb3
60
50
 
61
- from v
51
+ from (
62
52
 
53
+ select *,(select count(*)+1 from tbl where id<t1.id and k=t1.k) as rank
54
+
55
+ from tbl as t1) as t2
56
+
63
- group by k;
57
+ group by k
64
58
 
65
59
  ```