【问题标题】:Calculate Profit Based on First-In, First-Out Pricing By Date Of Sale根据销售日期的先进先出定价计算利润
【发布时间】:2018-02-21 19:42:39
【问题描述】:

假设它们按照购买顺序出售,我如何通过 SQL 找到每天的销售利润?

【问题讨论】:

  • sql 版本太多...要我们猜吗?
  • @maSTAShuFu 标签上写着 sql-server-2008
  • 感谢@Aaron ...你忘了提到 postgres 和 access-vba
  • 甚至没有任何尝试的暗示,这就像“这里有一些数据,为我做这项工作”。

标签: sql sql-server postgresql sql-server-2008


【解决方案1】:

请试试这个解决方案 -

;with cte as 
(
select purchase_date,item,cost, qty as num from purchase
union all
select purchase_date,item,cost, num-1 from cte where num>1
), 
cte2 as
(
select sale_date,item,price, qty as num from sales
union all
select sale_date,item,price, num-1 from cte2 where num>1
)

select sale_date, sum(price-cost) from (

(select sale_date, item, price ,row_number() over (order by sale_date,num) rn from cte2) s
inner join
 (select purchase_date, item, cost ,row_number() over (order by purchase_date,num) rn2 from cte) z
 on s.item=z.item and s.rn=z.rn2)  

按销售日期分组

【讨论】:

  • 非常感谢我尝试在 sql express 2005 中运行此解决方案出现此消息:消息 8120,级别 16,状态 1,第 3 行列 's.sale_date' 在选择列表中无效,因为它是不包含在聚合函数或 GROUP BY 子句中。
猜你喜欢
  • 2013-01-16
  • 2018-10-11
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
相关资源
最近更新 更多