1

ある国で処理された行が 1 行あり、同じ国で未処理の行が 1 行ある行を選択する必要があります。以下の表では、行を で返しid = 1ます。

最初に、ステータスの行があるかどうかを確認する必要がありTREATEDます。次に、ステータスの行とUNTREATEDステータスの行と同じ国があるかどうかを確認する必要がありTREATEDます。

    Table example 1
    +----+--------+--------------+
    | id | country| status       |
    +----+--------+--------------+
    | 1  | SE     | TREATED      |
    | 2  | DK     | UNTREATED    |
    | 3  | SE     | UNTREATED    |
    +----+--------+--------------+

    Result of query
    +----+--------+--------------+
    | id | country| status       |
    +----+--------+--------------+
    | 1  | SE     | TREATED      |
    +----+--------+--------------+

未処理の行が存在しない場合、または国が同じでない場合は、何も返すべきではありません

    Table example 2
    +----+--------+--------------+
    | id | country| status       |
    +----+--------+--------------+
    | 1  | SE     | TREATED      |
    | 2  | DK     | UNTREATED    |
    | 3  | US     | UNTREATED    |
    +----+--------+--------------+

    Result of query (empty)
    +----+--------+--------------+
    | id | country| status       |
    +----+--------+--------------+
4

3 に答える 3

2

その他のオプションをいくつか示します。

select t1.id,t1.country,t1.status
    from table t1
    join table t2 on t1.country=t2.country and t2.status='UNTREATED'
    where t1.status='TREATED'


 select id,country,status
            from table t1
            where 
                 t1.status='TREATED' and
                 exists (
                      select 1 from table t2 where 
                           t2.country = t1.country and 
                           t2.status = 'UNTREATED'
                 )

結合を使用する最初のものは、おそらく最高のパフォーマンスを発揮すると思います。

于 2012-08-10T09:18:40.953 に答える
0
Select country
from table
where status in ('treated', 'untreated') -- only needed if there are other statuses
group by country
having count(distinct status) = 2
于 2012-08-10T08:33:34.500 に答える
0
SELECT country  FROM table
WHERE status IN ('treated', 'untreated')
GROUP BY country HAVING count(DISTINCT status)=2;

構文はSQLによって異なる場合がありますが、db2.

于 2012-08-10T08:35:23.347 に答える