有计算2个日期期间的剩余货物的代码:
剩余=首次开始数量+当期收入-当期销售额。
是否可以更改 dt1 派生表以获得最后收入的收入价格?不是总金额而是上次收入的价格?
我想要的输出是获取包含这些列的结果表: 商品 ID、商品、最后收入价格、已售数量、其余
我正在使用火鸟3。 数据库表: 商品:商品_id,商品... 收入:收入_id、收入_dt、qnt、商品_id... 销售:sale_id、invenue_id、sale_dt、qnt、goods_id...
SELECT
g.goods ,
g.goods_id,
coalesce(dt2.startqnt,0) as startqnt,
coalesce(dt2.income,0)as income_qnt,
coalesce(dt2.sales,0)as sales_qnt,
coalesce(dt2.rest,0) as rest
FROM goods g
LEFT JOIN
(select --dt2
goods_id,
coalesce(sum(startincome),0) as startincome,
coalesce(sum(startsale),0) as startsale,
sum(coalesce(startincome,0)-coalesce(startsale,0)) as startqnt,
coalesce(sum(income_qnt),0) as income_qnt,
coalesce(sum(sale_qnt),0) as sales_qnt,
sum(coalesce(startincome,0)-coalesce(startsale,0)+coalesce(income_qnt,0)-coalesce(sale_qnt,0)) as rest
from(
select --dt1
i.goods_id,
sum(case when i.income_dt<:d1 then coalesce(i.qnt,0) end) as startincome,
0 as startsale,
sum(case when (cast(i.income_dt as date)>=:d1) and (cast(i.income_dt as date)<=:d2) then coalesce(i.qnt,0) end) as income_qnt,
0 as sale
from income i
where i.income_dt<=:d2
group by goods_id
union all
select
s.goods_id,
0,
sum(case when (cast(s.sale_dt as date) <:d1) then coalesce(s.qnt,0) end) as startsale,
0,
sum(case when (cast(s.sale_dt as date) >=:d1) and (cast(s.sale_dt as date)<=:d2) then coalesce(s.qnt,0) end) as sale_qnt
from sale s , income i
where
s.sale_dt<=:d2 and
s.income_id=i.income_id
group by goods_id
) dt1
group by goods_id )dt2
on g.goods_id=dt2.goods_id
order by goods
要获取每种产品的最后收入价格,您需要添加逻辑来获取每种产品的最新收入日期,然后将其与收入表连接起来以获得相关价格。
下面是 SQL 的修改版本,应该可以达到预期的结果:
SELECT
g.goods,
g.goods_id,
coalesce(dt2.startqnt,0) as startqnt,
coalesce(dt2.income_qnt,0) as income_qnt,
coalesce(dt2.sales_qnt,0) as sales_qnt,
coalesce(dt2.rest,0) as rest,
coalesce(dt2.last_income_price,0) as last_income_price
FROM goods g
LEFT JOIN
(select
goods_id,
coalesce(sum(startincome),0) as startincome,
coalesce(sum(startsale),0) as startsale,
sum(coalesce(startincome,0)-coalesce(startsale,0)) as startqnt,
coalesce(sum(income_qnt),0) as income_qnt,
coalesce(sum(sale_qnt),0) as sales_qnt,
sum(coalesce(startincome,0)-coalesce(startsale,0)+coalesce(income_qnt,0)-coalesce(sale_qnt,0)) as rest,
li.price as last_income_price
from(
select
i.goods_id,
sum(case when i.income_dt<:d1 then coalesce(i.qnt,0) end) as startincome,
0 as startsale,
sum(case when (cast(i.income_dt as date)>=:d1) and (cast(i.income_dt as date)<=:d2) then coalesce(i.qnt,0) end) as income_qnt,
0 as sale_qnt
from income i
where i.income_dt<=:d2
group by goods_id
union all
select
s.goods_id,
0,
sum(case when (cast(s.sale_dt as date) <:d1) then coalesce(s.qnt,0) end) as startsale,
0,
sum(case when (cast(s.sale_dt as date) >=:d1) and (cast(s.sale_dt as date)<=:d2) then coalesce(s.qnt,0) end) as sale_qnt
from sale s , income i
where s.sale_dt<=:d2 and s.income_id=i.income_id
group by goods_id
) dt1
-- Get the last income price for each product
LEFT JOIN (
SELECT
i.goods_id,
i.price
FROM income i
WHERE i.income_dt = (
SELECT MAX(i2.income_dt)
FROM income i2
WHERE i2.goods_id = i.goods_id
)
) li on dt1.goods_id = li.goods_id
group by goods_id, li.price
) dt2
on g.goods_id = dt2.goods_id
order by g.goods