2016-09-25 5 views
0

重複する値が発生するのを避ける方法テーブルの製品の最後のトランザクションをフェッチしようとしているとき。私のイメージを次のように私のクエリはたぶん何かenter image description here重複した値が発生する製品の最後のトランザクションをフェッチしようとしたとき

SELECT 
    a.branchid, 
    a.TellerID, 
    a.ProductID, 
    a.TransactDateTime, 
    a.ProductStock, 
    a.ProductStockInLocalCrrncy 
FROM ALX_SubInventoryCashTransfers a 
INNER JOIN (
    SELECT 
     branchid, 
     TellerID, 
     ProductID, 
     MAX(TransactDateTime) datetime 
    FROM ALX_SubInventoryCashTransfers 
    GROUP BY branchid, 
      TellerID, 
      ProductID, 
      TransactDateTime 
) tm 
    ON a.BranchID = tm.BranchID 
    AND a.branchid = tm.BranchID 
    AND a.TellerID = tm.TellerID 
    AND a.ProductID = tm.ProductID 
    AND a.TransactDateTime = tm.datetime 
+0

重複を含む実際の出力を表示できますか?インクルードした画面キャプチャには、複数の 'datetime'値が表示されます。これは、特定のテーブルの最大値を持つレコードに制限するため、クエリからは不可能です。 –

+1

TransactDateTimeフィールドをgroup by句から削除してみます。 –

+0

@ tim、それは複製です。私は1つだけの日付値の製品価値が必要です。あなたは写真のIDで見ることができます同じ2回のIDの2回の繰り返しは、私は一日に一度それを必要とします –

答えて

2

from ALX_SubInventoryCashTransfers group by 
branchid,TellerID,ProductID,**TransactDateTime**) tm 

によってグループからTransactDateTimeを削除するあなたは

SELECT 
    a.branchid, 
    a.TellerID, 
    a.ProductID, 
    a.TransactDateTime, 
    a.ProductStock, 
    a.ProductStockInLocalCrrncy 
FROM ALX_SubInventoryCashTransfers a 
INNER JOIN (SELECT 
    branchid, 
    TellerID, 
    ProductID, 
    MAX(TransactDateTime) as MaxDate 
FROM ALX_SubInventoryCashTransfers 
GROUP BY branchid, 
     TellerID, 
     ProductID) tm 
    ON a.BranchID = tm.BranchID 
    AND a.branchid = tm.BranchID 
    AND a.TellerID = tm.TellerID 
    AND a.ProductID = tm.ProductID 
    AND a.TransactDateTime = tm.MaxDate 
+0

私は表示する必要がある日付にaccoding日付が必要です(各製品の最後の値は毎日表示する必要があります) –

+0

私は言っているあなたはサブクエリからmaxdateを使うことができます –

+0

私の更新されたクエリを試すことができます.. –

1

のようなものです:

with cte as (
    select 
     rn = row_number() over (partition by a.ProductID order by a.TransactDateTime desc), 
     a.branchid, a.TellerID, a.ProductID, a.TransactDateTime, a.ProductStock, a.ProductStockInLocalCrrncy 
    from 
     ALX_SubInventoryCashTransfers a 
) 
select 
    a.branchid, a.TellerID, a.ProductID, a.TransactDateTime, a.ProductStock, a.ProductStockInLocalCrrncy 
from 
    cte a 
where 
    (a.rn = 1) 
+0

それはcteで正しく表示されていませんそれはページの賢明なショーのようになります –

1
このクエリを試すことができます

問題が正しく解決したら、日付ごとに最大のdatetimeが必要です。以下のクエリを試してください:

SELECT 
    a.branchid, 
    a.TellerID, 
    a.ProductID, 
    a.TransactDateTime, 
    a.ProductStock, 
    a.ProductStockInLocalCrrncy 
FROM ALX_SubInventoryCashTransfers a 
INNER JOIN (
    SELECT 
     branchid, 
     TellerID, 
     ProductID, 
     MAX(TransactDateTime) datetime 
    FROM ALX_SubInventoryCashTransfers 
    GROUP BY branchid, 
      TellerID, 
      ProductID, 
      CAST(TransactDateTime AS DATE) 
) tm 
    ON a.BranchID = tm.BranchID 
    AND a.branchid = tm.BranchID 
    AND a.TellerID = tm.TellerID 
    AND a.ProductID = tm.ProductID 
    AND a.TransactDateTime = tm.datetime 
関連する問題