Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从选择postgres中选择_Sql_Postgresql_Subquery - Fatal编程技术网

Sql 从选择postgres中选择

Sql 从选择postgres中选择,sql,postgresql,subquery,Sql,Postgresql,Subquery,我有一个这样的问题 SELECT trx.store, trx.sales, trx.qty_sold, trx.average, trx.start_date, trx.end_date, stok.stok_qty FROM (select sto.name as store, sum(odi.subtotal_price) as sales, sum(odi.qty) as qty_sold, ((sum(odi.subtotal_price))/(sum(odi.qty))) as av

我有一个这样的问题

SELECT trx.store, trx.sales, trx.qty_sold, trx.average, trx.start_date, trx.end_date, stok.stok_qty
FROM
(select sto.name as store, sum(odi.subtotal_price) as sales, sum(odi.qty) as qty_sold, ((sum(odi.subtotal_price))/(sum(odi.qty))) as average, 
    min(CAST(ord.date_out AS date)) as start_date, max(CAST(ord.date_out AS date)) as end_date 
from trx_order_detail_item odi
left join trx_order as ord on ord.id = odi.order_id
left join mst_store as sto on sto.id = ord.store_id
where sto.principle_id = 12 and ord.date_out between '2018-07-01' and '2019-02-28' 
    and ord.order_status_id in (select mst_order_status.id from mst_order_status where mst_order_status.id = 3 or mst_order_status.id = 5) 
    and ord.void_status=0
group by sto.name, date_trunc('month', ord.date_out)
order by date_trunc('month', ord.date_out)) AS trx, 
(select sum(siss.jumlah_akhir) as stok_qty
from trx_stok_item_summary as siss 
where siss.input_date IN (select max(sisss.input_date)
            from trx_stok_item_summary as sisss
            join mst_store as sto on sto.id = sisss.store_id
            join mst_item as it on it.id = sisss.item_id
            join mst_item_classifiers as ic on ic.id = sisss.item_variant_id
            where sto.principle_id = 12 and input_date between '2018-07-01' AND  '2019-02-28'
            group by date_trunc('month', sisss.input_date), sisss.item_variant_id
            order by date_trunc('month', sisss.input_date))
group by date_trunc('month', siss.input_date)) AS stok
输出是

Store  |Sales |qty_sold|average |start_date  |end_date    |stok_qty 
===================================================================
Store 1|494000|128     |3859.375|"2018-07-02"|"2018-07-31"|-263
Store 1|494000|128     |3859.375|"2018-07-02"|"2018-07-31"|0
Store 1|327000|44      |7431.818|"2018-08-01"|"2018-08-19"|-263
Store 1|327000|44      |7431.818|"2018-08-01"|"2018-08-19"|0
当我延长日期时,输出循环的次数与数据范围内的月数相同

我想要的输出是:

Store  |Sales |qty_sold|average |start_date  |end_date    |stok_qty 
===================================================================
Store 1|494000|128     |3859.375|"2018-07-02"|"2018-07-31"|-263
Store 1|327000|44      |7431.818|"2018-08-01"|"2018-08-19"|0
知道如何解决这个问题吗?

使用distinct


如果缺少任何类型的联接条件,请尝试:

SELECT trx.store, trx.sales, trx.qty_sold, trx.average, trx.start_date, trx.end_date, stok.stok_qty
FROM
(select sto.name as store, sum(odi.subtotal_price) as sales, sum(odi.qty) as qty_sold, ((sum(odi.subtotal_price))/(sum(odi.qty))) as average, 
    min(CAST(ord.date_out AS date)) as start_date, max(CAST(ord.date_out AS date)) as end_date,
date_trunc('month', ord.date_out) AS trx_month
from trx_order_detail_item odi
left join trx_order as ord on ord.id = odi.order_id
left join mst_store as sto on sto.id = ord.store_id
where sto.principle_id = 12 and ord.date_out between '2018-07-01' and '2019-02-28' 
    and ord.order_status_id in (select mst_order_status.id from mst_order_status where mst_order_status.id = 3 or mst_order_status.id = 5) 
    and ord.void_status=0
group by sto.name, date_trunc('month', ord.date_out)
order by date_trunc('month', ord.date_out)) AS trx, 
(select date_trunc('month', siss.input_date) AS stok_month,
sum(siss.jumlah_akhir) as stok_qty
from trx_stok_item_summary as siss 
where siss.input_date IN (select max(sisss.input_date)
            from trx_stok_item_summary as sisss
            join mst_store as sto on sto.id = sisss.store_id
            join mst_item as it on it.id = sisss.item_id
            join mst_item_classifiers as ic on ic.id = sisss.item_variant_id
            where sto.principle_id = 12 and input_date between '2018-07-01' AND  '2019-02-28'
            group by date_trunc('month', sisss.input_date), sisss.item_variant_id
            order by date_trunc('month', sisss.input_date))
group by date_trunc('month', siss.input_date)) AS stok
WHERE stok.stok_month = trx.trx_month
在以下情况下使用distinct:


stok_数量不同,distinct将不会有帮助。stok_数量似乎丢失了位置,或者您有任何逻辑选择该位置
SELECT trx.store, trx.sales, trx.qty_sold, trx.average, trx.start_date, trx.end_date, stok.stok_qty
FROM
(select sto.name as store, sum(odi.subtotal_price) as sales, sum(odi.qty) as qty_sold, ((sum(odi.subtotal_price))/(sum(odi.qty))) as average, 
    min(CAST(ord.date_out AS date)) as start_date, max(CAST(ord.date_out AS date)) as end_date,
date_trunc('month', ord.date_out) AS trx_month
from trx_order_detail_item odi
left join trx_order as ord on ord.id = odi.order_id
left join mst_store as sto on sto.id = ord.store_id
where sto.principle_id = 12 and ord.date_out between '2018-07-01' and '2019-02-28' 
    and ord.order_status_id in (select mst_order_status.id from mst_order_status where mst_order_status.id = 3 or mst_order_status.id = 5) 
    and ord.void_status=0
group by sto.name, date_trunc('month', ord.date_out)
order by date_trunc('month', ord.date_out)) AS trx, 
(select date_trunc('month', siss.input_date) AS stok_month,
sum(siss.jumlah_akhir) as stok_qty
from trx_stok_item_summary as siss 
where siss.input_date IN (select max(sisss.input_date)
            from trx_stok_item_summary as sisss
            join mst_store as sto on sto.id = sisss.store_id
            join mst_item as it on it.id = sisss.item_id
            join mst_item_classifiers as ic on ic.id = sisss.item_variant_id
            where sto.principle_id = 12 and input_date between '2018-07-01' AND  '2019-02-28'
            group by date_trunc('month', sisss.input_date), sisss.item_variant_id
            order by date_trunc('month', sisss.input_date))
group by date_trunc('month', siss.input_date)) AS stok
WHERE stok.stok_month = trx.trx_month
select distinct on (store, start_date) . . .
<rest of your query here>
order by store, start_date, stok_qty;