我希望SQL添加两列中的数字,并将两个数字的平均值放在一个单独的列中。
我目前正在以这种方式使用AVG
:
AVG(InpCount + OutCount) OVER (PARTITION BY ProcedureID) as AverageCount,
AVG(InpAmount + OutAmount) OVER (PARTITION BY ProcedureID) as AverageAmount,
我希望SQL给我一个InpCount + OutCount
的平均值,并将该平均值放在上面的AverageCount
列中。如果我也可以对InpAmount + InpCount
字段执行此操作,那么我就完成了。
以下是查询:
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,
row_number() over (partition by ProcedureID order by (select NULL)) as SeqNum
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') AND DrugID = 'LIDO5GEL4'
答案 0 :(得分:0)
您应该明确计算平均值。平均值用于计算不同记录的平均值,而不是不同列的平均值。
您可能希望使用SUM(InpAmount + OutAmount) / 2
代替AVG(InpAmount + OutAmount)
,而使用SUM(InpCount + OutCount) / 2
代替AVG(InpAmount + OutAmount)
。