简体   繁体   English

SQL编号比较和汇总

[英]SQL number comparison and aggregation

and using snowflake database. 并使用雪花数据库。 There is a task I'm trying to solve but don't know where to start. 我正在尝试解决一个任务,但不知道从哪里开始。 I want to do calculate the percentage of BIDS higher than/lower than/equal to PRICE and the accepted price. 我想计算出BIDS高于/低于/等于PRICE的百分比以及可接受的价格。 There are thousands of IDs, below is one of them. 有数千个ID,下面是其中之一。 The example data has 2 bids equal to price(180), one bid higher than price (200), and one bid lower than price (100). 该示例数据具有2个等于价格(180)的投标,一个高于价格(200)的投标,和一个低于价格(100)的投标。 The accepted price's difference in percentage with price (100/180 = 0.55) 接受价格与价格的百分比差异(100/180 = 0.55)

ID,     PRICE,ID,   BIDS,ACCEPTED
2134323,180,2134323,200,FALSE
2134323,180,2134323,180,FALSE
2134323,180,2134323,180,FALSE
2134323,180,2134323,100,TRUE

Expected output: 预期产量:

ID,     PRICE ACCEPTED HIGHER LOWER EQUAL
2134323 180    0.55      0.25   0.25  0.5

Any suggestion how to complete this? 有什么建议如何完成吗?

Conditional aggregation should work: 条件聚合应该起作用:

select id, price
       max(case when accepted = 'TRUE' then bids end) / price as accepted,
       avg(case when bids > price then 1.0 else 0 end) as higher,
       avg(case when bids < price then 1.0 else 0 end) as lower,
       avg(case when bids = price then 1.0 else 0 end) as equal
from t
group by id, price;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM