我有一个具有以下值的数据表“ BondPrices”:
date | type | price
1/5/2019 bid 104.50
1/5/2019 bid 104.75
1/6/2019 bid 104.40
1/6/2019 bid 104.45
1/5/2019 offer 106.75
1/5/2019 offer 107.75
1/6/2019 offer 106.23
1/6/2019 offer 106.47
我想编写一个查询,给出给定日期的最高出价和最低价格,例如以下输出:
date | Highest Bid | Lowest Offer
1/5/2019 104.75 106.75
1/6/2019 104.45 106.23
这样做可以达到最高出价或最低出价
SELECT "date", max("price") AS "Highest Bid"
FROM "BondPrices"
WHERE "type"='bid'
GROUP BY "date"
或
SELECT "date", min("price") AS "Lowest Offer"
FROM "BondPrices"
WHERE "type"='offer'
GROUP BY "date"
但是当我尝试将它们结合在一起(例如与JOIN结合使用)时,会出现语法错误。
如何编写具有多个条件并导致多个列的select语句?
答案 0 :(得分:2)
有几种方法可以在这里给猫皮,这将带您到那里:
SELECT
CAST(date AS DATE) Date,
MAX(CASE WHEN type = 'bid' THEN price ELSE NULL END) HighestBid,
MIN(CASE WHEN type = 'offer' THEN price ELSE NULL END) LowestOffer
FROM
BondPrices
GROUP BY
CAST(date AS DATE)