首先,感谢所有试图帮助我解决问题的人。非常感谢。
我有以下语句来提取所选数据并为 Inp/Outp Amount & Count 提供 SUM 和 AVG。现在我有了所需的数据,我需要消除导致具有相同 ProcedureID 的条目出现额外行的问题,因为输出是相同的,因为它是基于 ProcedureID 进行分区的。我的想法是在语句中添加一个计数器列,该语句也被划分为按程序 ID 计数。然后将最高或最低整数选择到一个临时表中,我就完成了。
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 还很陌生,我想我会问专家。提前致谢!