2

一言で言えば、列でグループ化しようとしていますが、その列がそうでない場合、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   |
+--------+----------+---------------------+-------+

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

結果は次のとおりです。

+--------+----------+---------------------+-------+-------+
| 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     |
+--------+----------+---------------------+-------+-------+

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     |
+--------+----------+---------------------+-------+-------+

ご覧のとおり、すべてがグループ化されている場合を除いて、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

2 番目のselectクエリを実行して、最初の結果を 2 番目の結果の上にスタックしようと考えましたが、順序が原因でうまくいかないと思います。

このように条件付きでグループ化する方法はありますか?

4

2 に答える 2

5

これはそれを行います:

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>
于 2013-01-18T13:59:02.493 に答える
1

試す、

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  otherID IS NOT NULL OR
          otherID != '0'
) AS derivedTable
WHERE  r = 1
于 2013-01-18T13:46:35.200 に答える