首先,感谢所有试图帮助解决我问题的人。非常感谢。
我有以下语句来提取所选数据,并为Inp / Outp Amount&提供SUM和AVG。计数。既然我有我需要的数据,我需要消除导致具有相同ProcedureID的条目的额外行的问题,因为输出是相同的,因为它是基于ProcedureID进行分区的。我的想法是在语句中添加一个计数器列,该列也按ProcedureID进行分区计数。然后将最高或最低整数选择为temptable,我就完成了。
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 :(得分: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
。
您可以使用的另一种方法是在distinct
之后添加select
。
最后,我怀疑你的查询等同于:
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;