此查询有效,但它不返回bx_broker没有数据的日期(time_added)的记录:
select bx_broker as Broker, date_trunc('day', time_added) as date, avg(bx_avgpxvsarrival) as AvgPr_vs_Arrival, avg(bx_avgpxvsoppvwapbpsblackrockasia) as AvgPr_vs_VWAP
from best_ex_data
where bx_broker = 'ML'
group by date_trunc('day', time_added), bx_broker
order by date desc
limit 365;
例如,如果我的数据集中有大量记录用于bx_broker =' ML'在所有time_added日期,但只有少数bx_broker =' XYZ'在time_added日期,我目前获得的bx_broker =' XYZ'记录较少。即使当天没有数据,我也总是强迫日期有bx_broker的记录。对于除日期之外的所有字段,没有数据的日期将为NULL,但日期仍将存在于查询结果中,其他所有字段均为NULL值。我希望每个bx_broker都有单独的结果(即我将重新运行每个bx_broker的查询)。
感谢您的帮助!
编辑:
以下是我对bx_broker = ML的查询结果,其中包含best_ex_data表中所有日期的数据:
╔════════╦════════════╦══════════════════╦═══════════════╗
║ broker ║ date ║ avgpr_vs_arrival ║ avgpr_vs_vwap ║
╠════════╬════════════╬══════════════════╬═══════════════╣
║ ML ║ 2016-12-12 ║ 0.00 ║ 0.63 ║
║ ML ║ 2016-12-09 ║ -0.06 ║ -1.74 ║
║ ML ║ 2016-12-08 ║ 0.10 ║ -8.11 ║
║ ML ║ 2016-12-07 ║ -0.40 ║ -9.55 ║
║ ML ║ 2016-12-06 ║ -0.29 ║ -8.84 ║
║ ML ║ 2016-12-05 ║ -0.15 ║ -3.47 ║
║ ML ║ 2016-12-02 ║ 0.23 ║ -4.70 ║
╚════════╩════════════╩══════════════════╩═══════════════╝
以下是我对bx_broker = GSEC的查询结果,该查询没有所有日期的数据:
╔════════╦════════════╦══════════════════╦═══════════════╗
║ broker ║ date ║ avgpr_vs_arrival ║ avgpr_vs_vwap ║
╠════════╬════════════╬══════════════════╬═══════════════╣
║ GSEC ║ 2016-12-09 ║ -0.04 ║ -0.66 ║
║ GSEC ║ 2016-12-07 ║ 0.01 ║ 1.59 ║
║ GSEC ║ 2016-12-06 ║ -0.06 ║ -1.43 ║
║ GSEC ║ 2016-12-05 ║ 0.01 ║ -0.25 ║
║ GSEC ║ 2016-12-02 ║ -0.09 ║ -2.06 ║
╚════════╩════════════╩══════════════════╩═══════════════╝
以下是我想要的bx_broker = GSEC(注意表中存在的日期,即使GSEC没有数据):
╔════════╦════════════╦══════════════════╦═══════════════╗
║ broker ║ date ║ avgpr_vs_arrival ║ avgpr_vs_vwap ║
╠════════╬════════════╬══════════════════╬═══════════════╣
║ GSEC ║ 2016-12-12 ║ ║ ║
║ GSEC ║ 2016-12-09 ║ -0.04 ║ -0.66 ║
║ GSEC ║ 2016-12-08 ║ ║ ║
║ GSEC ║ 2016-12-07 ║ 0.01 ║ 1.59 ║
║ GSEC ║ 2016-12-06 ║ -0.06 ║ -1.43 ║
║ GSEC ║ 2016-12-05 ║ 0.01 ║ -0.25 ║
║ GSEC ║ 2016-12-02 ║ -0.09 ║ -2.06 ║
╚════════╩════════════╩══════════════════╩═══════════════╝
注意:当我运行查询时,我不知道任何一个经纪人都有所有日期的数据。我想我必须选择所有日期并使用它进行一些外部联接以及特定于代理的选择。
答案 0 :(得分:0)
SELECT
bx_broker as Broker,
date_trunc('day', time_added) as date,
avg(bx_avgpxvsarrival) as AvgPr_vs_Arrival,
avg(bx_avgpxvsoppvwapbpsblackrockasia) as AvgPr_vs_VWAP
FROM best_ex_data
RIGHT OUTER JOIN (
SELECT day
FROM generate_series(
date_trunc('year', now())::date,
date_trunc('year', now())::date + interval '365 days',
'1 day'::interval
) AS day
) AS dates
ON dates.day = date_trunc('day', time_added)
WHERE bx_broker = 'ML'
GROUP BY date_trunc('day', time_added), bx_broker
ORDER BY date desc;
答案 1 :(得分:0)
这有效:
select
'GSEC' as Broker,
date_trunc('day', dts.date) as date,
avg(d.AvgPr_vs_Arrival) as AvgPr_vs_Arrival,
avg(d.AvgPr_vs_VWAP) as AvgPr_vs_VWAP
from
(select distinct date_trunc('day', time_added) as date
from best_ex_data
order by date desc
limit 365) as dts
left outer join
(select bx_broker as Broker, date_trunc('day', time_added) as date,
avg(bx_avgpxvsarrival) as AvgPr_vs_Arrival,
avg(bx_avgpxvsoppvwapbpsblackrockasia) as AvgPr_vs_VWAP
from best_ex_data
where bx_broker = 'GSEC'
group by date_trunc('day', time_added), bx_broker
order by date desc
limit 365) as d
on d.date = dts.date
group by dts.date
order by date desc
limit 365;