根据t-SQL中的特定条件排除某一行

时间:2016-02-04 13:39:50

标签: sql-server tsql

首先,我创建了以下股票销售SELECT语句:

SELECT
PostST.TxDate AS TxDate,
PostST.Reference AS InvNum,
Client.Name AS CustomerName,
SalesRep.Code AS SalesRep,
WhseMst.Code AS Whse,
CONCAT (StkItem.Description_1, ' - ',StkItem.Code) AS Item,
_etblLotTracking.cLotDescription AS LotNumber,
CASE 
    WHEN PostST.TrCodeID = 34 THEN (PostST.Quantity * 1)
    WHEN PostST.TrCodeID = 30 THEN (PostST.Quantity * -1)
    ELSE 0 
    END AS QtySold,
CAST 
    (
CASE
    WHEN PostST.TrCodeID = 34 THEN (PostST.Credit / PostST.Quantity)
    WHEN PostST.TrCodeID = 30 THEN (PostST.Debit / (PostST.Quantity * -1))
    ELSE 0
    END 
    as [money] 
    )
    AS CustomerPrice,
StkItem.ItemGroup AS ItemGroup,
StkItem.ulIIStockType AS StockType, 
YEAR (PostST.TxDate) AS Year,
DATENAME (MONTH, (PostST.TxDate)) AS Month

FROM 
PostST
INNER JOIN StkItem
    ON StkItem.StockLink = PostST.AccountLink
INNER JOIN PostAR
    ON PostAR.cAuditNumber = PostST.cAuditNumber
INNER JOIN Client
    ON Client.DCLink = PostAR.AccountLink
INNER JOIN SalesRep
    ON SalesRep.idSalesRep = PostAR.RepID
INNER JOIN WhseMst
    ON WhseMst.WhseLink = PostST.WarehouseID
FULL JOIN _etblLotTracking
    ON _etblLotTracking.idLotTracking = PostST.iLotID

WHERE 
PostST.TrCodeID IN (34, 30) AND  StkItem.ItemGroup <> 'TRAN'

此查询从Sage Evolution运行,数据被转储到Excel中。从那里我操纵以每月和每年的数量查看每个项目和销售历史。

我的问题如下:

在随后的数据中,有时候客户可能已停止购买特定商品。我需要的是这个语句还通过以下条件过滤掉一行:

如果客户没有6个月或更长时间的销售,则必须针对该特定客户过滤该STOCK ITEM。

谢谢!

0 个答案:

没有答案