Skip to content

Instantly share code, notes, and snippets.

@carinavockell
Created September 12, 2022 16:33
Show Gist options
  • Save carinavockell/69695a22c8a34678044fd1db1b311bd3 to your computer and use it in GitHub Desktop.
Save carinavockell/69695a22c8a34678044fd1db1b311bd3 to your computer and use it in GitHub Desktop.
These built-in T-SQL functions provide the same functionality as the query above but with less complicated coding and more efficiency.
SELECT
SalesOrderDetailID,
UnitPrice,
CarrierTrackingNumber,
RunningTotal = SUM (UnitPrice) OVER (
PARTITION BY CarrierTrackingNumber ORDER BY SalesOrderDetailID),
RowCnt= COUNT (*) OVER (
PARTITION BY CarrierTrackingNumber ORDER BY SalesOrderDetailID)
FROM Sales.SalesOrderDetail o
WHERE o.CarrierTrackingNumber IS NOT NULL
ORDER BY CarrierTrackingNumber ASC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment