2014-01-24 40 views
1

首先,感謝所有試圖幫助解決我的問題的人。非常感謝。需要添加被類似行條目劃分的計數器列

我有以下語句來拉選定的數據,並給予SUM和AVG for Inp/Outp Amount & Count。現在我有我需要的數據,我需要消除導致具有相同ProcedureID的條目的額外行的問題,因爲根據ProcedureID進行分區,因此輸出相同。我的想法是爲語句添加一個計數器列,該列也被分區以通過ProcedureID進行計數。然後選擇最高或最低的整數到一個臨時表,我會完成。

SELECT M.ProcedureID, 
     M.SegmentDateTime, 
    M.PriceID, 
    L.DrugID, 
    L.NdcDinNumber, 
    L.Name, 
    M.DeptCorporation, 
    M.InpAmount, 
    M.InpCount, 
    M.OutAmount, 
    M.OutCount, 

     SUM(InpCount) OVER (PARTITION BY ProcedureID) as INtotal, 
     SUM(InpAmount) OVER (PARTITION BY ProcedureID) as IN$Total, 
     SUM(OutCount) OVER (PARTITION BY ProcedureID) as OUTtotal, 
     SUM(OutAmount) OVER (PARTITION BY ProcedureID) as OUT$Total, 
     SUM(InpCount + OutCount) OVER (PARTITION BY ProcedureID) as TotalCount, 
     SUM(InpAmount + OutAmount) OVER (PARTITION BY ProcedureID) as TotalAmount 
     AVG(InpCount + OutCount) OVER (PARTITION BY ProcedureID) as AverageCount, 
     AVG(InpAmount + OutAmount) OVER (PARTITION BY ProcedureID) as AverageAmount 

     FROM BarRevenueByProcedurePriceInfo M 

LEFT JOIN DPhaDrugData L ON 
M.ProcedureID = L.BillNumber 


    WHERE DeptID = '010.4730' 
AND SegmentDateTime = '2013-12-31 00:00:00.000' 
AND M.InpCount > '0' 

OR 

DeptID = '010.4730' 
AND SegmentDateTime = '2013-12-31 00:00:00.000' 
AND M.OutCount > '0' 

    ORDER BY ProcedureID 

因爲我還是相當新的SQL我想我會問專家。提前致謝!

+0

您可以用CTE做到這一點:添加含有'ROW_NUMBER()OVER(由ProcedureID ProcedureID順序分區)作爲seq'列你當前查詢,然後'選擇*從CTE where seq = 1' –

回答

0

這裏有三種可能的方法來做你想做的事情。

這裏是一個row_number()方法:

with p as (
    SELECT M.ProcedureID, M.SegmentDateTime, M.PriceID, 
      L.DrugID, L.NdcDinNumber, L.Name, M.DeptCorporation, M.InpAmount, 
      M.InpCount, M.OutAmount, M.OutCount, 
      row_number() over (partition by procedureID order by (select NULL)) as seqnum, 
      SUM(InpCount) OVER (PARTITION BY ProcedureID) as INtotal, 
      SUM(InpAmount) OVER (PARTITION BY ProcedureID) as IN$Total, 
      SUM(OutCount) OVER (PARTITION BY ProcedureID) as OUTtotal, 
      SUM(OutAmount) OVER (PARTITION BY ProcedureID) as OUT$Total, 
      SUM(InpCount + OutCount) OVER (PARTITION BY ProcedureID) as TotalCount, 
      SUM(InpAmount + OutAmount) OVER (PARTITION BY ProcedureID) as TotalAmount 
      AVG(InpCount + OutCount) OVER (PARTITION BY ProcedureID) as AverageCount, 
      AVG(InpAmount + OutAmount) OVER (PARTITION BY ProcedureID) as AverageAmount 
    FROM BarRevenueByProcedurePriceInfo M LEFT JOIN 
     DPhaDrugData L 
     ON M.ProcedureID = L.BillNumber 
    WHERE DeptID = '010.4730' AND SegmentDateTime = '2013-12-31 00:00:00.000' AND 
      (M.InpCount > '0' or M.OutCount > '0') 
    ) 
select p.* 
from p 
where seqnum = 1 
ORDER BY ProcedureID; 

您可能希望從刪除列的列表中刪除seqnum

您可以使用的另一種方法是在select之後加上distinct

最後,我懷疑你的查詢等效於:

SELECT M.ProcedureID, M.SegmentDateTime, M.PriceID, 
      L.DrugID, L.NdcDinNumber, L.Name, M.DeptCorporation, M.InpAmount, 
      M.InpCount, M.OutAmount, M.OutCount, 
      SUM(InpCount) as INtotal, 
      SUM(InpAmount) as IN$Total, 
      SUM(OutCount) as OUTtotal, 
      SUM(OutAmount) as OUT$Total, 
      SUM(InpCount + OutCount) as TotalCount, 
      SUM(InpAmount + OutAmount) as TotalAmount 
      AVG(InpCount + OutCount) as AverageCount, 
      AVG(InpAmount + OutAmount) as AverageAmount 
    FROM BarRevenueByProcedurePriceInfo M LEFT JOIN 
     DPhaDrugData L 
     ON M.ProcedureID = L.BillNumber 
    WHERE DeptID = '010.4730' AND SegmentDateTime = '2013-12-31 00:00:00.000' AND 
      (M.InpCount > '0' or M.OutCount > '0') 
    GROUP BY M.ProcedureID, M.SegmentDateTime, M.PriceID, 
      L.DrugID, L.NdcDinNumber, L.Name, M.DeptCorporation, M.InpAmount, 
      M.InpCount, M.OutAmount, M.OutCount; 
+0

我試着添加DISTINCT。由於行條目在某些列中包含不同的數據,因此它不會爲每個我需要的記錄提供一條記錄。我添加了row_number()(通過ProcedureID順序(通過選擇NULL))分區作爲seqnum並獲得了計數器列。現在我只需要將每個ProcedureID的最高整數放入一個臨時表中,然後完成。 – YepItsMe

+0

謝謝戈登。使用了兩個建議的變體。這完美地使用CTE。 – YepItsMe

+0

@YepItsMe。 。 。我寫出了整個查詢並將關鍵部分留在外面。 。 。 'seqnum = 1'。 –