2016-11-24 53 views
0

在查詢下面你將看到一個Item: 4500520400500TSQL我不能日期添加適當的過濾器/ recid

在此查詢我想列出了最新的日期此項目的唯一規則10- 10-2016在這種情況下。或與最新RecID

enter image description here

我不能讓這與選擇中選擇querys工作。之前嘗試過 在這種情況下,我得到的所有物品,但我只想要最後的項目recid

我試過了WHERE Select MAX(RECID)但是隻給出了整個表的記錄,而不是已經做出選擇的記錄。

SELECT A.DATAAREAID, A.ITEMID 
, B.ACTIVATIONDATE, B.PRICE, B.CREATEDDATETIME, B.RECID 
, 'DEFAULTORDERTYPE' = CASE 
    WHEN C.DEFAULTORDERTYPE = 0 THEN 'Purch' 
    WHEN C.DEFAULTORDERTYPE = 1 THEN 'Production' 
    WHEN C.DEFAULTORDERTYPE = 2 THEN 'Transfer' 
    WHEN C.DEFAULTORDERTYPE = 3 THEN 'Kanban' 
    ELSE 'Bestaat niet' 
    END 
, 'PRODUCTTYPE' = CASE 
    WHEN D.PRODUCTTYPE = 1 THEN 'Item' 
    WHEN D.PRODUCTTYPE = 2 THEN 'Service' 
    ELSE 'Bestaat niet' 
    END 
, E.MODELGROUPID AS 'Item Model Group' 
FROM INVENTTABLE AS A 
LEFT JOIN INVENTITEMPRICE AS B ON A.ITEMID = B.ITEMID AND A.PARTITION = B.PARTITION AND A.DATAAREAID = B.DATAAREAID 
LEFT JOIN INVENTITEMSETUPSUPPLYTYPE AS C ON A.ITEMID = C.ITEMID AND A.DATAAREAID = C.ITEMDATAAREAID AND A.PARTITION = C.PARTITION 
LEFT JOIN ECORESPRODUCT AS D ON A.PRODUCT = D.RECID AND A.PARTITION = D.PARTITION 
LEFT JOIN INVENTMODELGROUPITEM AS E ON A.ITEMID = E.ITEMID AND A.DATAAREAID = E.ITEMDATAAREAID 
WHERE A.KNK_ITEMHIDDEN = 0 
AND (B.PRICE = 0 OR B.price IS NULL) 
ORDER BY A.DATAAREAID, A.ITEMID, B.ACTIVATIONDATE 

回答

0

你可以使用一個窗口函數來獲取每個ITEMID最大B.CREATEDDATETIME值,然後只有在B.CREATEDDATETIME值相匹配的是最大的回報記錄:

SELECT records.DATAAREAID, 
     records.ITEMID, 
     records.ACTIVATIONDATE, 
     records.PRICE 
     records.CREATEDDATETIME, 
     records.RECID, 
     records.DEFAULTORDERTYPE, 
     records.PRODUCTTYPE, 
     records.[Item Model Group] 
FROM 
(
    SELECT A.DATAAREAID, 
      A.ITEMID, 
      B.ACTIVATIONDATE, 
      B.PRICE, 
      B.CREATEDDATETIME, 
      MAX(B.CREATEDDATETIME) OVER (PARTITION BY A.ITEMID) MAXCREATEDDATETIME, 
      B.RECID, 
      'DEFAULTORDERTYPE' = CASE WHEN C.DEFAULTORDERTYPE = 0 THEN 'Purch' 
             WHEN C.DEFAULTORDERTYPE = 1 THEN 'Production' 
             WHEN C.DEFAULTORDERTYPE = 2 THEN 'Transfer' 
             WHEN C.DEFAULTORDERTYPE = 3 THEN 'Kanban' 
             ELSE 'Bestaat niet' 
           END, 
      'PRODUCTTYPE' = CASE WHEN D.PRODUCTTYPE = 1 THEN 'Item' 
            WHEN D.PRODUCTTYPE = 2 THEN 'Service' 
            ELSE 'Bestaat niet' 
            END, 
      E.MODELGROUPID AS 'Item Model Group' 
    FROM INVENTTABLE AS A 
    LEFT JOIN INVENTITEMPRICE AS B ON A.ITEMID = B.ITEMID 
            AND A.PARTITION = B.PARTITION 
            AND A.DATAAREAID = B.DATAAREAID 
    LEFT JOIN INVENTITEMSETUPSUPPLYTYPE AS C ON A.ITEMID = C.ITEMID 
              AND A.DATAAREAID = C.ITEMDATAAREAID 
              AND A.PARTITION = C.PARTITION 
    LEFT JOIN ECORESPRODUCT AS D ON A.PRODUCT = D.RECID 
           AND A.PARTITION = D.PARTITION 
    LEFT JOIN INVENTMODELGROUPITEM AS E ON A.ITEMID = E.ITEMID 
             AND A.DATAAREAID = E.ITEMDATAAREAID 
    WHERE A.KNK_ITEMHIDDEN = 0 
    AND (B.PRICE = 0 OR B.price IS NULL) 
) records 
WHERE records.CREATEDDATETIME = records.MAXCREATEDDATETIME 
ORDER BY records.DATAAREAID, 
     records.ITEMID, 
     records.ACTIVATIONDATE 

這類似於給here答案。