所以我有一张表“dump”:
_____________________________________
| id_seller | client | price | flag |
|====================================|
| 1 | a | 12 | True |
| 1 | a | 10 | True |
| 1 | a | 13← | True |
| 1 | a | 14 | False |
| 1 | a | 9← | False |
| 1 | a | 11 | False |
| 2 | a | 15← | True |
| 2 | a | 13 | True |
| 2 | a | 14 | True |
| 2 | a | 21 | False |
| 2 | a | 23 | False |
| 1 | a | 20← | False |
| 1 | b | 14 | True |
[...]
我正试图获得一些客户和卖家的最低和最高价格,具体取决于以下标志:
_________________________________________________________
| min_price_flag0 | max_price_flag1 | client | id_seller |
|========================================================|
| 9 | 13 | a | 1 |
| 20 | 15 | a | 2 |
| 42 | 21 | b | 1 |
| 84 | 10.5 | b | 2 |
etc..
目前我正在使用此请求,但我不确定它的性能是否良好,也不确定它是否与聚合表中的其他select相关联:
SELECT min_price_flag0,
max_price_flag1,
client,
id_seller
FROM (
SELECT min(price) AS min_price_flag0,
client,
id_seller
FROM dump_table
WHERE id_seller IN (1,2,3) AND
client IN ("a", "b", "c") AND
flag = "False"
GROUP BY client
)
AS min_price_flag0_select
INNER JOIN
(
SELECT max(price) AS max_price_flag1,
client,
id_seller
FROM dump_table
WHERE id_seller IN (1,2,3) AND
client IN ("a", "b", "c") AND
flag = "True"
GROUP BY client
)
AS max_price_flag1_select ON min_price_flag0_select.id_seller = max_price_flag1_select.id_seller AND
min_price_flag0_select.client = max_price_flag1_select.client
我不太擅长sql,对数据库的访问也非常有限,所以我正在寻找更好的选择建议。
1条答案
按热度按时间jrcvhitl1#
你想要条件加重吗(
CASE WHEN
内部MIN
//MAX
):