- Не уверен, что я делаю это правильно, за исключением того, что я делаю, не работает.У меня есть список (vend_num), который мне нужен для выполнения запроса по одному и компиляции результатов.Если кто-нибудь может увидеть / объяснить, где я сошел с рельсов, это было бы очень признательно.Я прочесываю Интернет и этот сайт и не могу обернуть его вокруг себя.
--drop table ##ENE_vendUpdate
if object_id('tempdb..##ENE_vendUpdate') is not null
begin
drop table ##ENE_vendUpdate
end
select vend_num
into ##ENE_vendUpdate
from vendor
--select * from ##ENE_vendUpdate
if @@ROWCOUNT > 0
BEGIN
declare @vend_num varchar(7)
declare warrwebcur cursor
for
select vend_num from ##ENE_vendUpdate
open warrwebcur
fetch warrwebcur into @vend_num
while @@FETCH_STATUS = 0
BEGIN
Select a.vend_num,
(SELECT COUNT(*)
FROM po
join matltran mtl on mtl.ref_num = po.po_num
join vendor v on v.vend_num = po.vend_num
where v.vend_num = @vend_num
AND cast(po.CreateDate as datetime) > DATEADD(year,-1,GETDATE())
AND mtl.reason_code = 'PPQ')
AS TOTAL_RMAS,
(SELECT count(*)
FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num
where v.vend_num = @vend_num
AND cast(poi.rcvd_date as datetime) > DATEADD(year,-1,GETDATE())
AND poi.stat IN ('C','F'))
AS PO_Lines_Complete,
(SELECT count(*)
FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num where v.vend_num = @vend_num
AND cast(poi.rcvd_date as datetime) > DATEADD(year,-1,GETDATE()))
AS PO_Lines_Total,
(SELECT sum(qty)
FROM po
join matltran mtl on mtl.ref_num = po.po_num
join vendor v on v.vend_num = po.vend_num
where v.vend_num = @vend_num
AND cast(po.CreateDate as datetime) > DATEADD(year,-1,GETDATE())
AND mtl.reason_code = 'PPQ')
AS RMA_Pieces ,
(SELECT sum(poi.qty_received)
FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num where v.vend_num = @vend_num
AND cast(poi.rcvd_date as datetime) > DATEADD(year,-1,GETDATE())
AND poi.stat in ('C','F') )
AS PCs_Received,
(SELECT sum(poi.qty_ordered)
FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num where v.vend_num = @vend_num
AND cast(poi.rcvd_date as datetime) > DATEADD(year,-1,GETDATE()) AND poi.stat in ('C','F'))
AS PCs_Due,
(SELECT count(*)
FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num
where v.vend_num = @vend_num
AND poi.rcvd_date <= poi.due_date
AND cast(poi.rcvd_date as datetime) > DATEADD(year,-1,GETDATE()) )
AS PO_Lines_OnTime,
(SELECT count(*)
FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num
where v.vend_num = @vend_num
AND cast(po.CreateDate as datetime) > DATEADD(year,-1,GETDATE())
AND poi.rcvd_date is null)
AS PO_Lines_due,
(select count(*)
FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num
where v.vend_num = @vend_num
AND cast(poi.rcvd_date as datetime) > DATEADD(year,-1,GETDATE())
AND poi.stat in ('C','F'))
AS ONTIME_PERCT,
(select count(*)
FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num
where v.vend_num = @vend_num AND cast(poi.rcvd_date as datetime) > DATEADD(year,-1,GETDATE())
AND poi.stat in ('C','F'))
AS TOTAL_ROW_COUNT,
(SELECT count(*)
FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num
where v.vend_num = @vend_num
AND cast(poi.rcvd_date as datetime) > DATEADD(year,-1,GETDATE())
AND poi.stat IN ('C','F')
AND poi.qty_ordered <> poi.qty_received)
AS PO_Lines_Accurate
FROM (SELECT DISTINCT v.vend_num FROM vendor v
join po on v.vend_num = po.vend_num
join vendaddr on vendaddr.vend_num = v.vend_num
join PoItem poi on poi.po_num = po.po_num
where v.vend_num = @vend_num AND cast(poi.rcvd_date as datetime) > DATEADD(year,-1,GETDATE())
) A