27,579
社区成员
发帖
与我相关
我的任务
分享
;
with 表一(日期, 发货, 规格, 车号, 吨位) as
(
select convert(date, '2018-08-18'), '单位1', '规格1', '1号车', 20 union all
select convert(date, '2018-08-18'), '单位1', '规格2', '1号车', 10 union all
select convert(date, '2018-08-18'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-20'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格2', '1号车', 10 union all
select convert(date, '2018-08-22'), '单位1', '规格2', '1号车', 30
),
表二(日期, 发货, 规格, 单价) as
(
select convert(date, '2018-08-05'), '单位1', '规格1', 50 union all
select convert(date, '2018-08-05'), '单位1', '规格2', 60 union all
select convert(date, '2018-08-19'), '单位1', '规格1', 90 union all
select convert(date, '2018-08-19'), '单位1', '规格2', 100
)
SELECT a.*,b.单价,合计发货=a.吨位*b.单价
FROM 表一 A
INNER JOIN (SELECT startdate=日期,enddate=dateadd(day,-1,LEAD(日期)over(partition by 发货, 规格 order by 日期)),发货, 规格, 单价 FROM 表二
) B on a.发货 = b.发货 and a.规格 = b.规格 and a.日期 between startdate and isnull(enddate,getdate())
order by a.日期,a.发货,a.规格
日期 发货 规格 车号 吨位 单价 合计发货
---------- ----- ----- ----- ----------- ----------- -----------
2018-08-18 单位1 规格1 1号车 10 50 500
2018-08-18 单位1 规格1 1号车 20 50 1000
2018-08-18 单位1 规格2 1号车 10 60 600
2018-08-20 单位1 规格1 1号车 10 90 900
2018-08-21 单位1 规格1 1号车 10 90 900
2018-08-21 单位1 规格1 1号车 10 90 900
2018-08-21 单位1 规格1 1号车 10 90 900
2018-08-21 单位1 规格2 1号车 10 100 1000
2018-08-22 单位1 规格2 1号车 30 100 3000
;with 表一(日期, 发货, 规格, 车号, 吨位) as
(
select convert(date, '2018-08-18'), '单位1', '规格1', '1号车', 20 union all
select convert(date, '2018-08-18'), '单位1', '规格2', '1号车', 10 union all
select convert(date, '2018-08-18'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-20'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格2', '1号车', 10 union all
select convert(date, '2018-08-22'), '单位1', '规格2', '1号车', 30
),
表二(日期, 发货, 规格, 单价) as
(
select convert(date, '2018-08-05'), '单位1', '规格1', 50 union all
select convert(date, '2018-08-05'), '单位1', '规格2', 60 union all
select convert(date, '2018-08-19'), '单位1', '规格1', 90 union all
select convert(date, '2018-08-19'), '单位1', '规格2', 100
)
select *, 吨位 * 单价 as 合计发货 from (
select *, (select top 1 单价 from 表二
where T1.发货 = 发货 and T1.规格 = 规格 and T1.日期 >= 日期
order by 日期 desc) as 单价
from 表一 as T1) as T
;with 表一(日期, 发货, 规格, 车号, 吨位) as
(
select convert(date, '2018-08-18'), '单位1', '规格1', '1号车', 20 union all
select convert(date, '2018-08-18'), '单位1', '规格2', '1号车', 10 union all
select convert(date, '2018-08-18'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-20'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格1', '1号车', 10 union all
select convert(date, '2018-08-21'), '单位1', '规格2', '1号车', 10 union all
select convert(date, '2018-08-22'), '单位1', '规格2', '1号车', 30
),
表二(日期, 发货, 规格, 单价) as
(
select convert(date, '2018-08-05'), '单位1', '规格1', 50 union all
select convert(date, '2018-08-05'), '单位1', '规格2', 60 union all
select convert(date, '2018-08-19'), '单位1', '规格1', 90 union all
select convert(date, '2018-08-19'), '单位1', '规格2', 100
)
select *, 吨位 * 单价 as 合计发货 from (
select *, (select top 1 max(单价) from 表二 AS T
where T1.发货 = 发货 and T1.规格 = 规格 and T1.日期 >= 日期) as 单价
from 表一 as T1) as T
select a.* ,b.单价,a.吨位*b.单价 as 合计发货 from a left join b on a.单位=b.单位 and a.规格=b.规格 and a.日期=b.日期
select a.* ,b.单价,a.吨位*b.单价 as 合计发货 from a left join b on a.单位=b.单位 and a.规格=b.规格