1

まず、私の質問を手伝ってくれたすべての人に感謝します。大変感謝しています。

選択したデータを取得し、Inp/Outp Amount & Count の 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にかなり慣れていないので、専門家に尋ねることにしました。前もって感謝します!

4

1 に答える 1

0

やりたいことを実行するための 3 つの方法を次に示します。

ここに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 に答える