使用范围和有限兼容性进行小计

时间:2015-10-22 14:45:04

标签: sql stored-procedures sql-server-2008-r2

在SQL Server 2008 R2上,我尝试创建一个存储过程,根据输入的范围为用户提供商品小计。我将不胜感激。

然而,我遇到了一个问题,其中查询显示正在计算的所有商品,因此用户以最终超过其输入范围的值结束。我将在底部包含一些示例数据以及我尝试过的内容。

这是一个例子(为了清楚起见,我在本例中省略了一些列):

table

正如您所知,对于2000到10000之间的范围,商品998-32在小计中将超过10000但仍然显示,因为每个PO NO单独小于10000.

以下是一些示例数据:

DROP TABLE ##mytable;
CREATE TABLE ##mytable(
   Commodity             VARCHAR(15) NOT NULL
  ,PO_NO                 INTEGER  NOT NULL
  ,LINE_NO               INTEGER  NOT NULL
  ,PO_Line_Description   VARCHAR(82)
  ,Commodity_Description VARCHAR(60) NOT NULL
  ,Fiscal_Year           INTEGER  NOT NULL
  ,Vendor_ID             INTEGER  NOT NULL
  ,Vendor_Name           VARCHAR(20) NOT NULL
  ,QUANTITY              INTEGER  NOT NULL
  ,UNIT_COST             NUMERIC(7,2) NOT NULL
  ,Line_Amount           NUMERIC(7,2) NOT NULL
);
INSERT INTO ##mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-18',1448923,1,'Face Scholastic book order attached.','Sale of Surplus and Obsolete Books',2015,47650,'SCHOLASTIC INC',1,9999.8,9999.8);
INSERT INTO ##mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-32',1416311,2,'First 12 months maintenance agreement to be billed quarterly at .0039 per b/w copy','Sale of Surplus and Obsolete Copy Machines',2015,341479,'RICOH USA, INC',1,5148,5148);
INSERT INTO ##mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-32',1424377,1,NULL,'Sale of Surplus and Obsolete Copy Machines',2015,300590,'KONICA MINOLTA/ CIT',1,2894.58,2894.58);
INSERT INTO ##mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-32',1404031,1,'1st (12) months of (36) month Lease payment for (1) MPC4503 copier.','Sale of Surplus and Obsolete Copy Machines',2015,341479,'RICOH USA, INC',1,2050.68,2050.68);
INSERT INTO ##mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-75',1401552,1,'Blanket order for 50 teachers - each teacher not to exceed $100.00.','Sale of Surplus and Obsolete Paper and Paper Products',2015,27536,'KNOWLEDGE TREE',1,5000,5000);
INSERT INTO ##mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-78',1521390,1,'PL02286>PRESSURE PLUMBER INSTANT DRAIN OPENER 24 SHOT CARTRIDGE','Sale of Surplus and Obsolete Plumbing Equipment and Supplies',2015,402985,'TECH MECH SUPPLY LLC',480,8,3840);

我尝试过使用SUM()OVER(PARTITION BY [] ORDER BY [] ROWS UNBOUNDED PRECEDING)但显然在SQL Server 2012中引入了带有ROW的OVER子句,并且不适用于2008 R2。

我也尝试过使用GROUP BY ROLLUP但是我收到一条消息“在当前兼容模式下不允许使用CUBE()和ROLLUP()分组构造。它们只允许在100或更高模式下使用。”当我问我们的DBA时,他说我们不能从90模式转到100模式,因为很多事情都会破裂。

所以现在我坚持使用下面的查询,它有前面提到的问题,给我我想要的数据的问题,以及最终将超出我指定范围的数据小计。

P.S。我还注意到,如果商品的PO NO在我选择的范围内,而且PO NOs的成本在它之外,这个查询将给我那些在选定范围内的那些非常误导,因为它仍然是一个商品,将是如果全部计算,则在所选范围之外。它们不应该被列入真正在我选定范围内的商品的结果。

ALTER PROCEDURE [dbo].[POreport] (
    @Param1 INT
    ,@Param2 INT
    ,@Param3 INT
    )
AS
BEGIN
    SET NOCOUNT ON;

    SELECT DISTINCT ROW_NUMBER() OVER (
            ORDER BY T.Product_ID
                ,T.PO_NO
                ,T.LINE_NO
            ) AS [RowID]
        ,ISNULL(T.PRODUCT_ID, 'NULL') AS [Commodity]
        ,ISNULL(T.PO_NO, 'NULL') AS [PO NO]
        ,ISNULL(T.LINE_NO, 'NULL') AS [LINE NO]
        ,QUOTENAME(T.DESCRIPTION, '"') AS [PO Line Description]
        ,QUOTENAME(C.DESCRIPTION, '"') AS [Commodity Description]
        ,ISNULL(T.FY, 'NULL') AS [Fiscal Year]
        ,PH.Vendor_ID AS [Vendor ID]
        ,QUOTENAME(V.Vendor_Name, '"') AS [Vendor Name]
        ,T.QUANTITY
        ,T.UNIT_COST
        ,T.QUANTITY * T.UNIT_COST AS [Line Amount]
        ,(
            SELECT CAST(0.00 AS NUMERIC(10, 2))
            ) AS Sub_Total_Cost
    INTO ##TmpPOReport
    FROM dbo.DBVW_FI_REQ_PO_ITEMS T
    INNER JOIN dbo.FI_VENDOR FV ON T.INST_ID = FV.INST_ID
    INNER JOIN dbo.FI_REQ_PO_HEADER PH ON T.PO_NO = PH.PO_NO
    INNER JOIN dbo.FI_VENDOR V ON PH.VENDOR_ID = V.VENDOR_ID
    INNER JOIN dbo.FI_COMMODITY C ON T.PRODUCT_ID = C.FI_COMMODITY_CODE
    WHERE T.INST_ID = 'SC00'
        AND T.FY = @Param1
        AND V.VENDOR_TYPE = 'V'
        AND T.PO_NO IS NOT NULL
        AND (
            T.PRODUCT_ID <> ''
            AND T.PRODUCT_ID IS NOT NULL
            )
        AND T.QUANTITY * T.UNIT_COST BETWEEN @Param2
            AND @Param3
    GROUP BY T.PRODUCT_ID
        ,T.PO_NO
        ,T.LINE_NO
        ,T.DESCRIPTION
        ,C.DESCRIPTION
        ,T.FY
        ,PH.Vendor_ID
        ,V.Vendor_Name
        ,T.QUANTITY
        ,T.UNIT_COST
        ,PH.Created_Date
    ORDER BY Commodity

    DECLARE @PID VARCHAR(15) = 00
        ,@QUANTITY INT
        ,@UNIT_COST NUMERIC(10, 2)
        ,@PrevID VARCHAR(15)
        ,@RowID BIGINT
        ,@PrevRowID BIGINT
        ,@RowAmount NUMERIC(10, 2)
        ,@SubTotal NUMERIC(10, 2) = 0.00

    SET NUMERIC_ROUNDABORT OFF;

    WHILE EXISTS (
            SELECT TOP 1 *
            FROM ##TmpPOReport
            WHERE Sub_Total_Cost = 0.00
            )
    BEGIN
        SET @RowAmount = (
                SELECT TOP 1 (QUANTITY * UNIT_COST)
                FROM ##TmpPOReport
                WHERE Sub_Total_Cost = 0.00
                )

        SELECT TOP 1 @PID = Commodity
            ,@RowID = RowID
        FROM ##TmpPOReport
        WHERE Sub_Total_Cost = 0.00

        IF (@PID = @PrevID)
            AND (@RowID <> @PrevRowID)
        BEGIN
            SET @SubTotal += @RowAmount;

            UPDATE T
            SET Sub_Total_Cost = @SubTotal
            FROM ##TmpPOReport T
            WHERE T.Commodity = @PID
                AND RowID = @RowID

            SET @PrevID = @PID;
            SET @PrevRowID = @RowID
        END
        ELSE
        BEGIN
            SET @SubTotal = @RowAmount;

            UPDATE T
            SET Sub_Total_Cost = @SubTotal
            FROM ##TmpPOReport T
            WHERE T.Commodity = @PID
                AND RowID = @RowID

            SET @PrevID = @PID;
            SET @PrevRowID = @RowID
        END
    END

    SET NUMERIC_ROUNDABORT ON;

    SELECT *
    FROM ##TmpPOReport
    WHERE [Line Amount] BETWEEN @Param2
            AND @Param3

    DROP TABLE ##TmpPOReport
END

谢谢!

1 个答案:

答案 0 :(得分:0)

对不起,如果不清楚的话。如果您想复制SUM OVER ROWS,则需要使用下面的CTE或类似的子查询。这不是一个优化的查询,但它会为您提供商品的运行总和。它使用RANK() OVER PARTITION来获取商品中每个PO的实例或行号。

    DECLARE @mytable TABLE(
       Commodity             VARCHAR(15) NOT NULL
      ,PO_NO                 INTEGER  NOT NULL
      ,LINE_NO               INTEGER  NOT NULL
      ,PO_Line_Description   VARCHAR(82)
      ,Commodity_Description VARCHAR(60) NOT NULL
      ,Fiscal_Year           INTEGER  NOT NULL
      ,Vendor_ID             INTEGER  NOT NULL
      ,Vendor_Name           VARCHAR(20) NOT NULL
      ,QUANTITY              INTEGER  NOT NULL
      ,UNIT_COST             NUMERIC(7,2) NOT NULL
      ,Line_Amount           NUMERIC(7,2) NOT NULL
    );
    INSERT INTO @mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-18',1448923,1,'Face Scholastic book order attached.','Sale of Surplus and Obsolete Books',2015,47650,'SCHOLASTIC INC',1,9999.8,9999.8);
    INSERT INTO @mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-32',1416311,2,'First 12 months maintenance agreement to be billed quarterly at .0039 per b/w copy','Sale of Surplus and Obsolete Copy Machines',2015,341479,'RICOH USA, INC',1,5148,5148);
    INSERT INTO @mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-32',1424377,1,NULL,'Sale of Surplus and Obsolete Copy Machines',2015,300590,'KONICA MINOLTA/ CIT',1,2894.58,2894.58);
    INSERT INTO @mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-32',1404031,1,'1st (12) months of (36) month Lease payment for (1) MPC4503 copier.','Sale of Surplus and Obsolete Copy Machines',2015,341479,'RICOH USA, INC',1,2050.68,2050.68);
    INSERT INTO @mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-75',1401552,1,'Blanket order for 50 teachers - each teacher not to exceed $100.00.','Sale of Surplus and Obsolete Paper and Paper Products',2015,27536,'KNOWLEDGE TREE',1,5000,5000);
    INSERT INTO @mytable(Commodity,PO_NO,LINE_NO,PO_Line_Description,Commodity_Description,Fiscal_Year,Vendor_ID,Vendor_Name,QUANTITY,UNIT_COST,Line_Amount) VALUES ('998-78',1521390,1,'PL02286>PRESSURE PLUMBER INSTANT DRAIN OPENER 24 SHOT CARTRIDGE','Sale of Surplus and Obsolete Plumbing Equipment and Supplies',2015,402985,'TECH MECH SUPPLY LLC',480,8,3840);


    DECLARE @FiscalYear INT
    DECLARE @LowTotalCost INT
    DECLARE @HighTotalCost INT

    SET @FiscalYear=2015
    SET @LowTotalCost=1000
    SET @HighTotalCost=10000




SELECT
    *
FROM
(       

    SELECT Commodity,PO_NO,QUANTITY,UNIT_COST,Line_Amount,
        RunningTotal=
        (
            SELECT SUM(TotalCost) FROM 
            (
                SELECT Commodity,TotalCost =(MT.QUANTITY * MT.UNIT_COST),
                    Instance=RANK()OVER(PARTITION BY Commodity ORDER BY Commodity,PO_NO DESC)
                FROM 
                    @mytable MT
                WHERE
                    (MT.QUANTITY * MT.UNIT_COST) BETWEEN @LowTotalCost AND @HighTotalCost
                    AND
                    (MT.Fiscal_Year=@FiscalYear)
            )AS Y 
            WHERE
                (Y.Commodity=X.Commodity) AND(Y.Instance<=X.Instance)
        )
    FROM
    (
        SELECT Commodity,PO_NO,QUANTITY,UNIT_COST,Line_Amount,
            TotalCost = (MT.QUANTITY * MT.UNIT_COST),
            Instance=RANK()OVER(PARTITION BY Commodity ORDER BY Commodity,PO_NO DESC)
        FROM 
            @mytable MT
        WHERE
            --IS FILTER HERE -->(MT.QUANTITY * MT.UNIT_COST) BETWEEN @LowTotalCost AND @HighTotalCost
            --AND
            (MT.Fiscal_Year=@FiscalYear)
    )AS X
)AS Z
WHERE

    (Commodity IN   
    (
        SELECT Commodity FROM
        (
            SELECT 
                Commodity, 
                Total=SUM(QUANTITY * UNIT_COST) 
            FROM 
                @mytable mt2 
            WHERE 
                mt2.Fiscal_Year=@FiscalYear 
            GROUP BY 
                Commodity 
        )AS A
        WHERE
            A.Total BETWEEN @LowTotalCost AND @HighTotalCost
    )
    )
SELECT 
    *
FROM 
    @mytable MT