1

首先,感谢所有试图帮助我解决问题的人。非常感谢。

我有以下语句来提取所选数据并为 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 还很陌生,我想我会问专家。提前致谢!

4

1 回答 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从已删除列的列表中删除。

您可以使用的另一种方法是distinctselect.

最后,我怀疑您的查询相当于:

    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;
于 2014-01-24T15:31:17.960 回答