34,590
社区成员
发帖
与我相关
我的任务
分享
select count(distinct 序列号)--这个查出来是6
from Return
where time between 11月 and getdate() --这里不同
select count(distinct 序列号) --这个查出来是100
from Shipment
where time between 12月 and getdate()
select count(distinct 序列号)--这个查出来是7
from Return
where time between 前9 and 前8
select count(distinct 序列号) --这个查出来是140
from Shipment
where time between 前9 and 前8
create table XxxxResults
(Indicators varchar(30),
Returns int,
Shipments int,
Results float
)
declare @Start datetime='2012-12-31'--这个时间参数
,
@ERIStart datetime
,
@ERIEnd datetime
set @ERIStart=cast(DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,@Start)-6,0)) as datetime);
--select @ERIStart
set @ERIEnd=cast(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@Start)+1,0)) as datetime);
--select @ERIEnd
insert into XxxxResults
(Indicators,Returns)
SELECT 'ERI',count(distinct t1.SerialNumber) AS ERICounts
FROM XxxxTblServiceReturns t1
left join
TblShipments t2
ON t1.SerialNumber=t2.SerialNumber
WHERE
t2.TransactionDate between @ERIStart and @ERIEnd
--select * from XxxxResults
--接着查询TblShipments在目标区间内出了多少货。
declare @Start datetime='2012-12-31'--这个时间参数
,
@ERIShipStart datetime
,
@ERIShipEnd datetime
set @ERIShipStart=cast(DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,@Start)-6,0)) as datetime);
--select @ERIShipStart
set @ERIShipEnd=cast(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@Start),0)) as datetime);
--select @ERIShipEnd
update XxxxResults
set Shipments=
(SELECT
count(distinct t1.SerialNumber)
FROM TblShipments t1
JOIN
XxxxTblMainFrames t2
ON
t1.PartNumber = t2.PartNumber
WHERE
t1.PartNumber IN
(SELECT
PartNumber
FROM
XxxxTblMainFrames)
AND
TransactionDate BETWEEN @ERIShipStart AND @ERIShipEnd
AND
OrderType NOT LIKE '%RMA%'
AND TransactionType != 'RMA'
AND t1.ProductLine != 'UPGRADE')
where Indicators='ERI'
;with cte as(
select (
select count(distinct 序列号) from Return
where time between 11月 and getdate()
) 退回数,
(select count(distinct 序列号) from Shipment
where time between 12月 and getdate()
) 发货数
union all
select (
select count(distinct 序列号)from Return
where time between 前9 and 前8
),
(select count(distinct 序列号) from Shipment
where time between 前9 and 前8
)
union all...
)
select *,cast(退回数*100.0/发货数 as varchar(20))+'%' from cte