T-SQL:聚合和/或有条件的条件子

时间:2016-05-25 16:43:50

标签: sql-server tsql

先谢谢你的帮助。这是场景。我有两个表:Lots(存储食品的批次信息)和Trans(存储批次项目的库存交易)。

我正在尝试编写一个查询,根据where子句的3个条件以某种方式列出超过90天的所有事务:

  1. CurrentQty(在Lots表上)> 0
  2. 如果TransactionType = Shipment AND TransactionDate> 90天(从当前日期开始)或.. ..
  3. IF TransactionType = Receipt AND TransactionDate> 90天(从当前日期开始)
  4. 注意:对于每个批次,可能存在许多相同类型的不同交易,如附图所示。可能有很多货物或收据。我需要能够为特定的交易类型选择MAX(TransactionDate),并检查它是否超过90天然后显示记录。

    每个批次(发货或收货)的交易表上始终至少有这两种交易类型中的一种。如果特定批次上没有装运交易类型,那么我想使用Max(TransactionDate)> “收据”交易类型的条件。

    我需要能够为每个批次评估所有这些条件,这是特定的交易。

    下面是我开始编写的查询,但后来又陷入了如何构建其余部分的问题。

    /usr

    TABLES

2 个答案:

答案 0 :(得分:0)

SELECT 
    LOTS.LOTNUMBER, TRANS.ITEMNUMBER, TRANS.DESCRIPTION, 
    TRANS.TRANSACTIONTYPE, TRANS.TRANSACTIONDATE, TRANS.WAREHOUSE, 
    TRANS.QUANTITY
FROM 
    LOTS 
INNER JOIN 
    (Select LotNumber, Max(TransactionDate) MaxTransDate From TRANS Group By LotNumber) Trans ON LOTS.LOTNUMBER = TRANS.LOTNUMBER
WHERE 
    LOTS.CURRENTQUANTITY > 0 And 
    DATEDIFF(d, TRANS.TRANSACTIONDATE, getdate()) > 90 And
    (TRANS.TRANSACTIONTYPE = 'Reciept' Or 
     TRANS.TRANSACTIONTYPE = 'Shipment')

答案 1 :(得分:0)

DECLARE @lots TABLE (
     LotNumber          NVARCHAR(10)
    ,ItemNum            NVARCHAR(12)
    ,CurrentQty         INT
    )

DECLARE @trans TABLE (
     TransNum           INT
    ,ItemNum            NVARCHAR(12)
    ,Description        NVARCHAR(30)
    ,TransType          NVARCHAR(10)
    ,TransDate          DATE
    ,Warehouse          NVARCHAR(5)
    ,Quantity           INT
    )

INSERT INTO @lots VALUES
    ('ABC','10-MAND-5879',925)

INSERT INTO @trans VALUES
     (398741,'10-MAND-5879','10 Lb Mandarin Bag','Receipt','2016-01-01','IXCST',100)
    ,(973541,'10-MAND-5879','10 Lb Mandarin Bag','Shipment','2016-02-04','WTGS',365)
    ,(972547,'10-MAND-5879','10 Lb Mandarin Bag','Receipt','2016-02-29','GKWK',250)
    ,(632403,'10-MAND-5879','10 Lb Mandarin Bag','Shipment','2016-03-01','GWSJ',150)
    ,(692147,'10-MAND-5879','10 Lb Mandarin Bag','Shipment','2016-03-17','ABTK',25)

;WITH MaxShip
    AS (
        SELECT ItemNum, TransNum,
            ROW_NUMBER () OVER (PARTITION BY ItemNum ORDER BY TransDate DESC) AS TransOrder
            FROM @trans
                WHERE TransType = 'Shipment'
            ),

MaxRcpt
    AS (
        SELECT ItemNum, TransNum,
            ROW_NUMBER () OVER (PARTITION BY ItemNum ORDER BY TransDate DESC)  AS TransOrder
            FROM @trans
                WHERE transtype = 'Receipt'
            )

SELECT *
    FROM @trans t
        LEFT JOIN @lots l
            ON t.ItemNum = l.ItemNum
        JOIN MaxShip ms
            ON ms.TransNum = t.TransNum
    WHERE TransOrder = 1 AND CurrentQty > 0 AND DATEADD(dd,90,TransDate) < GETDATE()
UNION
SELECT *
    FROM @trans t
        LEFT JOIN @lots l
            ON t.ItemNum = l.ItemNum
        JOIN MaxRcpt mr
            ON mr.TransNum = t.TransNum
    WHERE TransOrder = 1 AND CurrentQty > 0 AND DATEADD(dd,90,TransDate) < GETDATE()