こんな感じでユニオンやってます
select name, price from products where project = 10 // prio 1
union
select name, price from products where customer = 5 // prio 2
union
select name, price from products where standard = 9 // prio 3
編集:where句を変更して、もう少し複雑にしました
これは通常、私に返してくれます
+-----+------+-----------+--------------+
|(NO) | name | price | (prio) |
+-----+------+-----------+--------------+
| 1 | a | 10 | (1) |
| 2 | b | 5 | (1) |
| 3 | a | 13 | (2) |
| 4 | b | 2 | (2) |
| 5 | a | 1 | (3) |
| 6 | b | 5 | (3) |
| 7 | c | 3 | (3) |
+-----+------+-----------+--------------+
たとえば、行番号 1 と 3 は重複しておらず、union ステートメントによって削除されないことを理解しています。しかし、これはまさに私がやりたいことです。つまり、名前 (例: "a") が最初の select ステートメント (prio 1) によって返された場合、他の "a":s が優先度の高い select ステートメントから結果セットに入らないようにします。
つまり、これが欲しい:
+-----+------+-----------+--------------+
|(NO) | name | price | (prio) |
+-----+------+-----------+--------------+
| 1 | a | 10 | (1) |
| 2 | b | 5 | (1) |
| 7 | c | 3 | (3) |
+-----+------+-----------+--------------+
これは可能ですか?
使用してみgroup by
ましたが、これには、やりたくない価格でMIN、MAX、AVGなどを使用する必要があります。
select name, avg(price) from (...original query...) group by name
// this is not ok since I donnot want the avg price, I want the "first" price
MS SQL 2000 を使用first(..)
していgroup by
ます。これを試すと、エラーが発生します。
select name, first(price) from (...original query...) group by name
// error: 'first' is not a recognized built-in function name.
ありがとう!