Not 100% sure what you need, it might be this:
SELECT *,
SUM(ThisNumber) OVER (PARTITION BY ProcedureID)
FROM BarRevenueByProcedurePriceInfo
WHERE DeptID = '010.4730'
AND SegmentDateTime = '2013-11-30 00:00:00.000'
ORDER BY ProcedureID
Domanda
I'm new to SQL and have only been working with it for about 4 months. However I need to build a stored procedure that can calculate the total for like items (using numerical identifier ie. calculate total E.ThisNumber Where E.ThatNumber = 'Whatever'
)
This is the code I have so far to retrieve the records I need:
SELECT *
FROM BarRevenueByProcedurePriceInfo
WHERE DeptID = '010.4730'
AND SegmentDateTime = '2013-11-30 00:00:00.000'
ORDER BY ProcedureID
Here's the output:
ProcedureID/
RowUpdateTime Dept Code E.ThatNumber E.ThisNumber E.WhatIneed
2013-11-30 010.4730 4200028 108.15 108.15
2013-11-30 010.4730 4200028 18.18 126.33
2013-11-30 010.4730 4200028 63.63 189.81
2013-11-30 010.4730 4200069 5.00 5.00
2013-11-30 010.4730 4200069 5.01 10.01
2013-11-30 010.4730 4200069 6.00 16.01
Notice how the count resets when the E.ThatNumber value is different than the previous entry. Any help would be greatly appreciated. Thank you!
William Harley
Soluzione
Not 100% sure what you need, it might be this:
SELECT *,
SUM(ThisNumber) OVER (PARTITION BY ProcedureID)
FROM BarRevenueByProcedurePriceInfo
WHERE DeptID = '010.4730'
AND SegmentDateTime = '2013-11-30 00:00:00.000'
ORDER BY ProcedureID
Altri suggerimenti
SQL Server 2012 supports the cumulative sum as:
SELECT *,
sum(thisnumber) over (partition by procedureId order by (select NULL)) as whatIneed
FROM BarRevenueByProcedurePriceInfo
WHERE DeptID = '010.4730' AND
SegmentDateTime = '2013-11-30 00:00:00.000'
ORDER BY ProcedureID;
In earlier versions, you need to use a correlated subquery or join with aggregation.
By the way, there is no obvious ordering to the rows. If you are doing a cumulative sum, you should really have a column that specifies the order.