Ser*_*oke 2 sql oracle plsql group-by
简而言之,我正在尝试按列进行分组,但仅当该列不是null或者0在这种情况下我仍然想要获取这些行,但只是没有分组.我有这张桌子some_table:
+--------+----------+---------------------+-------+
| id | other_id | date_value | value |
+--------+----------+---------------------+-------+
| 1 | abc | 2011-04-20 21:03:05 | 104 |
| 2 | abc | 2011-04-20 21:03:04 | 229 |
| 3 | xyz | 2011-04-20 21:03:03 | 130 |
| 4 | abc | 2011-04-20 21:02:09 | 97 |
| 5 | 0 | 2011-04-20 21:02:08 | 65 |
| 6 | xyz | 2011-04-20 21:02:07 | 101 |
| 7 | | 2011-04-20 21:02:07 | 200 |
| 8 | 0 | 2011-04-20 21:02:07 | 201 |
| 9 | | 2011-04-20 21:02:07 | 202 |
+--------+----------+---------------------+-------+
Run Code Online (Sandbox Code Playgroud)
我想选择行和分组other_id,还包括分组行的计数.此查询有效:
select id, other_id, date_value, value, cnt from
(
SELECT id, other_id, date_value, value,
ROW_NUMBER() OVER (partition by other_id order BY Date_Value desc) r,
count(*) OVER (partition by other_id) cnt
FROM some_table
)
where r = 1
Run Code Online (Sandbox Code Playgroud)
结果是:
+--------+----------+---------------------+-------+-------+
| id | other_id | date_value | value | count |
+--------+----------+---------------------+-------+-------+
| 1 | abc | 2011-04-20 21:03:05 | 104 | 3 |
| 3 | xyz | 2011-04-20 21:03:03 | 130 | 2 |
| 5 | 0 | 2011-04-20 21:02:08 | 65 | 2 |
| 7 | | 2011-04-20 21:02:07 | 200 | 2 |
+--------+----------+---------------------+-------+-------+
Run Code Online (Sandbox Code Playgroud)
问题是,我不希望组具有行other_id的null或0.所以期望的结果是:
+--------+----------+---------------------+-------+-------+
| id | other_id | date_value | value | count |
+--------+----------+---------------------+-------+-------+
| 1 | abc | 2011-04-20 21:03:05 | 104 | 3 |
| 3 | xyz | 2011-04-20 21:03:03 | 130 | 2 |
| 5 | 0 | 2011-04-20 21:02:08 | 65 | 1 |
| 7 | | 2011-04-20 21:02:07 | 200 | 1 |
| 8 | 0 | 2011-04-20 21:02:07 | 201 | 1 |
| 9 | | 2011-04-20 21:02:07 | 202 | 1 |
+--------+----------+---------------------+-------+-------+
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,一切都只是当分组other_id是null或0在这种情况下,他们仍然选择,但不进行分组.我也试图按顺序排序date_value.
我试过在WHERE子句中添加一个OVER子句:
OVER (partition by other_id WHERE other_id IS NOT NULL AND other_id IS NOT '0' order BY Date_Value desc)
-- this produces an error: ORA-00907: missing right parenthesis
Run Code Online (Sandbox Code Playgroud)
我已经考虑过做第二个select查询并尝试将第一个结果堆叠在第二个上面,但我不认为这会因为排序而起作用.
有没有办法像这样分组条件?
这样做:
SQL> select id, other_id, date_value, value, cnt from
2 (
3 SELECT id, other_id, date_value, value,
4 case
5 when other_id is null or other_id = '0' then 1
6 else ROW_NUMBER() OVER (partition by other_id order BY Date_Value desc)
7 end r,
8 case
9 when other_id is null or other_id = '0' then 1
10 else count(*) OVER (partition by other_id)
11 end cnt
12 FROM some_table
13 )
14 where r = 1
15 order by id;
ID OTH DATE_VALUE VALUE CNT
---------- --- ------------------- ---------- ----------
1 abc 2011-04-20 21:03:05 104 3
3 xyz 2011-04-20 21:03:03 130 2
5 0 2011-04-20 21:02:08 65 1
7 2011-04-20 21:02:07 200 1
8 0 2011-04-20 21:02:07 201 1
9 2011-04-20 21:02:07 202 1
6 rows selected.
SQL>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6041 次 |
| 最近记录: |