2

h2データベースの複数の列から個別の値を選択する必要があります。これにより、データベースの内容に基づいてユーザーへの提案のリストを作成できます。言い換えれば、私は次のようなものが必要です

SELECT DISTINCT a FROM table
SELECT DISTINCT b FROM table
SELECT DISTINCT c FROM table

1つのクエリで。十分に明確でない場合は、このテーブル(列ID、thing、other、stuff)を指定したクエリが必要です

0 a 5 p
1 b 5 p
2 a 6 p
3 c 5 p

このような結果になります

a 5 p
b 6 -
c - -

ここで、「-」は空のエントリです。

4

1 に答える 1

2

これは少し複雑ですが、次のように実行できます。

select max(thing) as thing, max(other) as other, max(stuff) as stuff
from ((select row_number() over (order by id) as seqnum, thing, NULL as other, NULL as stuff
       from (select thing, min(id) as id from t group by thing
            ) t
      ) union all
      (select row_number() over (order by id) as seqnum, NULL, other, NULL
       from (select other, min(id) as id from t group by other
            ) t
      ) union all
      (select row_number() over (order by id) as seqnum, NULL, NULL, stuff
       from (select stuff, min(id) as id from t group by stuff
            ) t
      )
     ) t
group by seqnum

これは、各列の個別の値にシーケンス番号を割り当てることです。次に、これらを組み合わせて、シーケンス番号ごとに1つの行にします。組み合わせはunion all/group byアプローチを使用します。別の定式化では、を使用しfull outer joinます。

このバージョンでは、id列を使用して、元のデータに表示されるのと同じ順序で値を保持します。

H2(元々は質問に含まれていませんでした)では、rownum()代わりに関数を使用できます(ここに記載されています)。ただし、順序を指定できない場合があります。

于 2013-03-19T21:00:08.107 に答える