我正在做这样的工会
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-clause 使其更复杂一些
这通常会给我回来
+-----+------+-----------+--------------+
|(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 语句删除。然而,这正是我想要做的。也就是说,如果第一个 select 语句(prio 1)返回了一个名称(例如“a”),我不希望任何其他“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.
谢谢!