拥有此表(顶行是列名):
security quote_type price
sec_1 bid 3.4
sec_1 ask 3.6
sec_2 bid 5.2
sec_2 ask 5.4
sec_3 bid 2.3
sec_4 ask 7.8
需要查询才能获得这些结果:
security bid ask
sec_1 3.4 3.6
sec_2 5.2 5.4
sec_3 2.3 null
sec_4 null 7.8
使用SQL Server。
感谢您的帮助!
答案 0 :(得分:2)
您也可以使用条件聚合或pivot
:
select security,
max(case when quote_type = 'bid' then price end) as bid,
max(case when quote_type = 'ask' then price end) as ask
from t
group by security;
答案 1 :(得分:0)
您可以在桌面上的两个查询之间加入:
SELECT a.security, bid, ask
FROM (SELECT security, price AS bid
FROM mytable
WHERE quote_type = 'bid') a
FULL OUTER JOIN (SELECT security, price AS ask
FROM mytable
WHERE quote_type = 'ask') b ON a.security = b.security