--用友U8取两个账套区间采购入库单的第一次、最后一次原币含税单价,不区分供应商的平均单价
ALTER PROCEDURE [dbo].[P_Quick_RdPrice]
@StartTime DATETIME,
@EndTime DATETIME
--SET @StartTime = '2023-01-01';
--SET @EndTime = '2023-08-31';
AS
SELECT TempAll.cInvCode,
Inventory.cInvName,
Inventory.cInvStd,
FristPrice.cVenName AS FristVenName,
FristPrice.cPOID AS FristPoId,
FristPrice.dDate AS FristDate,
FristPrice.iOriTaxCost AS FristPrice,
FristPrice.cMaker AS FristMaker,
LastPrice.cVenName AS LastVenName,
LastPrice.cPOID AS LastPoId,
LastPrice.dDate AS LastDate,
LastPrice.iOriTaxCost AS LastPrice,
LastPrice.cMaker AS LastMaker,
TempC.AvgPrice
FROM
(
SELECT DISTINCT
(cIn.cInvCode) AS cInvCode
FROM
(
SELECT rdrecords01.cInvCode
FROM dbo.RdRecord01
JOIN dbo.rdrecords01
ON rdrecords01.ID = RdRecord01.ID
WHERE dDate
BETWEEN @StartTime AND @EndTime
AND RdRecord01.cVenCode NOT IN ( '02011000', '04010393', '02020145' )
AND RdRecord01.cHandler IS NOT NULL
AND RdRecord01.cRdCode = '101'
AND rdrecords01.cPOID NOT LIKE 'CD%'
UNION ALL
SELECT rdrecords01.cInvCode
FROM UFDATA_004_2020.dbo.RdRecord01
JOIN UFDATA_004_2020.dbo.rdrecords01
ON rdrecords01.ID = RdRecord01.ID
WHERE dDate
BETWEEN @StartTime AND @EndTime
AND RdRecord01.cVenCode NOT IN ( '02011000', '04010393', '02020145' )
AND RdRecord01.cHandler IS NOT NULL
AND RdRecord01.cRdCode = '101'
AND rdrecords01.cPOID NOT LIKE 'CD%'
) cIn
) TempAll
LEFT JOIN dbo.Inventory
ON Inventory.cInvCode = TempAll.cInvCode
LEFT JOIN
(
--第一次入库价格
SELECT *
FROM
(
SELECT Temp.dDate,
Temp.cVenName,
Temp.cInvCode,
Temp.iOriTaxCost,
--Temp.iPOsID,
Temp.cMaker,
Temp.cPOID,
ROW_NUMBER() OVER (PARTITION BY Temp.cInvCode ORDER BY Temp.dDate ASC) AS RowId
FROM
(
SELECT RdRecord01.dDate,
RdRecord01.dnmaketime,
RdRecord01.cVenCode,
(
SELECT cVenName FROM dbo.Vendor WHERE cVenCode = RdRecord01.cVenCode
) AS cVenName,
rdrecords01.cInvCode,
(
SELECT cMaker
FROM dbo.PO_Pomain
WHERE dbo.PO_Pomain.cPOID = rdrecords01.cPOID
) AS cMaker, --采购订单制单人
rdrecords01.cPOID, --采购订单号
--rdrecords01.iPOsID,
--(
-- SELECT PO_Pomain.cMaker
-- FROM dbo.PO_Pomain
-- JOIN dbo.PO_Podetails
-- ON PO_Podetails.POID = PO_Pomain.POID
-- WHERE PO_Podetails.ID = rdrecords01.iPOsID
--) AS cMaker, --采购订单制单人
--(
-- SELECT PO_Pomain.cPOID
-- FROM dbo.PO_Pomain
-- JOIN dbo.PO_Podetails
-- ON PO_Podetails.POID = PO_Pomain.POID
-- WHERE PO_Podetails.ID = rdrecords01.iPOsID
--) AS cPOID, --采购订单号
rdrecords01.iOriTaxCost --原币含税单价
FROM dbo.RdRecord01
JOIN dbo.rdrecords01
ON rdrecords01.ID = RdRecord01.ID
WHERE dDate
BETWEEN @StartTime AND @EndTime
AND RdRecord01.cVenCode NOT IN ( '02011000', '04010393', '02020145' )
AND RdRecord01.cHandler IS NOT NULL
AND RdRecord01.cRdCode = '101'
AND rdrecords01.cPOID NOT LIKE 'CD%'
UNION ALL
SELECT RdRecord01.dDate,
RdRecord01.dnmaketime,
RdRecord01.cVenCode,
(
SELECT cVenName
FROM UFDATA_004_2020.dbo.Vendor
WHERE cVenCode = RdRecord01.cVenCode
) AS cVenName,
rdrecords01.cInvCode,
(
SELECT cMaker
FROM UFDATA_004_2020.dbo.PO_Pomain
WHERE PO_Pomain.cPOID = rdrecords01.cPOID
) AS cMaker, --采购订单制单人
rdrecords01.cPOID, --采购订单号
--rdrecords01.iPOsID,
--(
-- SELECT PO_Pomain.cMaker
-- FROM UFDATA_004_2020.dbo.PO_Pomain
-- JOIN UFDATA_004_2020.dbo.PO_Podetails
-- ON PO_Podetails.POID = PO_Pomain.POID
-- WHERE PO_Podetails.ID = rdrecords01.iPOsID
--) AS cMaker, --采购订单制单人
--(
-- SELECT PO_Pomain.cPOID
-- FROM UFDATA_004_2020.dbo.PO_Pomain
-- JOIN UFDATA_004_2020.dbo.PO_Podetails
-- ON PO_Podetails.POID = PO_Pomain.POID
-- WHERE PO_Podetails.ID = rdrecords01.iPOsID
--) AS cPOID, --采购订单号
rdrecords01.iOriTaxCost --原币含税单价
FROM UFDATA_004_2020.dbo.RdRecord01
JOIN UFDATA_004_2020.dbo.rdrecords01
ON rdrecords01.ID = RdRecord01.ID
WHERE dDate
BETWEEN @StartTime AND @EndTime
AND RdRecord01.cVenCode NOT IN ( '02011000', '04010393', '02020145' )
AND RdRecord01.cHandler IS NOT NULL
AND RdRecord01.cRdCode = '101'
AND rdrecords01.cPOID NOT LIKE 'CD%'
) Temp
) T
WHERE T.RowId = 1
) FristPrice
ON FristPrice.cInvCode = TempAll.cInvCode
--最后一次入库价格
LEFT JOIN
(
SELECT *
FROM
(
SELECT Temp.dDate,
Temp.cVenName,
Temp.cInvCode,
Temp.iOriTaxCost,
--Temp.iPOsID,
Temp.cMaker,
Temp.cPOID,
ROW_NUMBER() OVER (PARTITION BY Temp.cInvCode ORDER BY Temp.dDate DESC) AS RowId
FROM
(
SELECT RdRecord01.dDate,
RdRecord01.dnmaketime,
RdRecord01.cVenCode,
(
SELECT cVenName FROM dbo.Vendor WHERE cVenCode = RdRecord01.cVenCode
) AS cVenName,
rdrecords01.cInvCode,
(
SELECT cMaker
FROM dbo.PO_Pomain
WHERE dbo.PO_Pomain.cPOID = rdrecords01.cPOID
) AS cMaker, --采购订单制单人
rdrecords01.cPOID, --采购订单号
--rdrecords01.iPOsID,
--(
-- SELECT PO_Pomain.cMaker
-- FROM dbo.PO_Pomain
-- JOIN dbo.PO_Podetails
-- ON PO_Podetails.POID = PO_Pomain.POID
-- WHERE PO_Podetails.ID = rdrecords01.iPOsID
--) AS cMaker, --采购订单制单人
--(
-- SELECT PO_Pomain.cPOID
-- FROM dbo.PO_Pomain
-- JOIN dbo.PO_Podetails
-- ON PO_Podetails.POID = PO_Pomain.POID
-- WHERE PO_Podetails.ID = rdrecords01.iPOsID
--) AS cPOID, --采购订单号
rdrecords01.iOriTaxCost --原币含税单价
FROM dbo.RdRecord01
JOIN dbo.rdrecords01
ON rdrecords01.ID = RdRecord01.ID
WHERE dDate
BETWEEN @StartTime AND @EndTime
AND RdRecord01.cVenCode NOT IN ( '02011000', '04010393', '02020145' )
AND RdRecord01.cHandler IS NOT NULL
AND RdRecord01.cRdCode = '101'
AND rdrecords01.cPOID NOT LIKE 'CD%'
UNION ALL
SELECT RdRecord01.dDate,
RdRecord01.dnmaketime,
RdRecord01.cVenCode,
(
SELECT cVenName
FROM UFDATA_004_2020.dbo.Vendor
WHERE cVenCode = RdRecord01.cVenCode
) AS cVenName,
rdrecords01.cInvCode,
(
SELECT cMaker
FROM UFDATA_004_2020.dbo.PO_Pomain
WHERE PO_Pomain.cPOID = rdrecords01.cPOID
) AS cMaker, --采购订单制单人
rdrecords01.cPOID, --采购订单号
--rdrecords01.iPOsID,
--(
-- SELECT PO_Pomain.cMaker
-- FROM UFDATA_004_2020.dbo.PO_Pomain
-- JOIN UFDATA_004_2020.dbo.PO_Podetails
-- ON PO_Podetails.POID = PO_Pomain.POID
-- WHERE PO_Podetails.ID = rdrecords01.iPOsID
--) AS cMaker, --采购订单制单人
--(
-- SELECT PO_Pomain.cPOID
-- FROM UFDATA_004_2020.dbo.PO_Pomain
-- JOIN UFDATA_004_2020.dbo.PO_Podetails
-- ON PO_Podetails.POID = PO_Pomain.POID
-- WHERE PO_Podetails.ID = rdrecords01.iPOsID
--) AS cPOID, --采购订单号
rdrecords01.iOriTaxCost --原币含税单价
FROM UFDATA_004_2020.dbo.RdRecord01
JOIN UFDATA_004_2020.dbo.rdrecords01
ON rdrecords01.ID = RdRecord01.ID
WHERE dDate
BETWEEN @StartTime AND @EndTime
AND RdRecord01.cVenCode NOT IN ( '02011000', '04010393', '02020145' )
AND RdRecord01.cHandler IS NOT NULL
AND RdRecord01.cRdCode = '101'
AND rdrecords01.cPOID NOT LIKE 'CD%'
) Temp
) T
WHERE T.RowId = 1
) LastPrice
ON LastPrice.cInvCode = TempAll.cInvCode
LEFT JOIN
(
SELECT TempB.cInvCode,
SUM(TempB.iSum) / NULLIF(SUM(TempB.iQuantity), 0) AS AvgPrice
FROM
(
SELECT rdrecords01.cInvCode,
rdrecords01.iSum, --本币价税合计
rdrecords01.iQuantity --数量
FROM dbo.RdRecord01
JOIN dbo.rdrecords01
ON rdrecords01.ID = RdRecord01.ID
WHERE dDate
BETWEEN @StartTime AND @EndTime
AND RdRecord01.cVenCode NOT IN ( '02011000', '04010393', '02020145' )
AND RdRecord01.cHandler IS NOT NULL
AND RdRecord01.cRdCode = '101'
AND rdrecords01.cPOID NOT LIKE 'CD%'
UNION ALL
SELECT rdrecords01.cInvCode,
rdrecords01.iSum, --本币价税合计
rdrecords01.iQuantity --数量
FROM UFDATA_004_2020.dbo.RdRecord01
JOIN UFDATA_004_2020.dbo.rdrecords01
ON rdrecords01.ID = RdRecord01.ID
WHERE dDate
BETWEEN @StartTime AND @EndTime
AND RdRecord01.cVenCode NOT IN ( '02011000', '04010393', '02020145' )
AND RdRecord01.cHandler IS NOT NULL
AND RdRecord01.cRdCode = '101'
AND rdrecords01.cPOID NOT LIKE 'CD%'
) TempB GROUP BY TempB.cInvCode
) TempC
ON TempC.cInvCode = TempAll.cInvCode