File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
backend/templates/sql_examples Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,9 @@ template:
1818 1. 传统写法:WHERE ROWNUM <= 100
1919 2. 现代写法:FETCH FIRST 100 ROWS ONLY
2020 </note>
21+ <note>
22+ 使用传统 ROWNUM 写法时,若遇到需要分组 GROUP BY 的情况,需要将限制条数的 ROWNUM 写在最外层,不然会影响最后查询出数据的总条数
23+ </note>
2124 </rule>
2225
2326 other_rule : |
@@ -71,6 +74,32 @@ template:
7174 AND ROWNUM <= 1000
7275 </output-good>
7376 </example>
77+
78+ <example>
79+ <input>统计用户表 PUBLIC.USERS 各部门人数</input>
80+ <output-bad>
81+ SELECT
82+ "u"."DEPARTMENT" AS "department_name",
83+ count(*) AS "user_count"
84+ FROM "PUBLIC"."USERS" "u"
85+ WHERE "u"."status" = 1
86+ AND ROWNUM <= 100
87+ GROUP BY "u"."DEPARTMENT"
88+ ORDER BY "department_name" -- 错误:ROWNUM 应当写在最外层,这样会导致查询结果条数比实际数据的数量少
89+ </output-bad>
90+ <output-good>
91+ SELECT "department_name", "user_count" FROM (
92+ SELECT
93+ "u"."DEPARTMENT" AS "department_name",
94+ count(*) AS "user_count"
95+ FROM "PUBLIC"."USERS" "u"
96+ WHERE "u"."status" = 1
97+ GROUP BY "u"."DEPARTMENT"
98+ ORDER BY "department_name"
99+ )
100+ WHERE ROWNUM <= 100
101+ </output-good>
102+ </example>
74103 </basic-examples>
75104
76105 example_engine : Oracle 19c
You can’t perform that action at this time.
0 commit comments