【发布时间】:2016-07-28 14:21:49
【问题描述】:
我有两张桌子
table_a
id b_ref_id qty
52 9 13
53 10 20
54 11 25
table_b
id method date state
9 m1 28/07/16 confirmed
10 m1 29/07/16 done
11 m1 30/07/16 waiting
我的愿望输出
m1 today tomorrow day_after_tomorrow
waiting 13 0 0
confirmed 0 20 0
done 0 0 25
我尝试使用以下查询,但数量重复
select stock_p.method, stock_p.state,
(select sm.qty
from
table_a sm
join table_b spo on (sm.b_ref_id=spo.id)
where
to_char(spo.date,'YYYY-MM-DD')::date = current_date and ) today_qty,
(select sm.qty
from table_a sm
join table_b spo on (sm.b_ref_id=spo.id)
where
to_char(spo.date,'YYYY-MM-DD')::date = (current_date + 1) ) tomorrow_qty,
(select sm.qty
from table_a sm
join table_b spo on (sm.b_ref_id=spo.id)
where
to_char(spo.date,'YYYY-MM-DD')::date = (current_date + 2)) next_three_qty
来自 table_a stock_m 在 stock_m.b_ref_id = stock_p.id 上加入 table_b stock_p 按 stock_p.method,stock_p.state 分组在此处输入代码
【问题讨论】:
-
感谢更新...
-
在我看来,您在这里尝试做两件不同的事情。获取您想要的数据很容易,这是两个表的直接连接。更难的是您对数据所做的调整。这通常最好在不同的工具中完成。顺便说一句,查询似乎与表不匹配。
标签: sql postgresql