March 26, 2025 at 10:41 am
Hi
I have below query and i want Cummulative Total . Total should be reset on change of Account
SELECT
ROW_NUMBER() OVER (PARTITION BY Account ORDER BY A."Account") AS "Row No",
A."Account" AS "Account",(A."Debit") AS "Debit", (A."Credit") AS "Credit"
From Master A
Thanks
March 26, 2025 at 10:50 am
Can you provide some consumable sample data?
If a single account has multiple rows, how are those rows to be ordered?
March 26, 2025 at 12:14 pm
Error - Windowed functions cannot be used in the context of another windowed function or aggregate.
SELECT ROW_NUMBER() OVER (PARTITION BY Account ORDER BY A."Account") AS "RowNo", A."Account" AS "Account", (A."Debit") AS "Debit", SUM(A."Debit" - A."Credit") OVER (PARTITION BY Account ORDER BY ROW_NUMBER() OVER (PARTITION BY Account ORDER BY A."Account")) AS Balance From Master A
Thanks
March 26, 2025 at 12:17 pm
Thanks for what? Did you understand my post?
March 26, 2025 at 12:42 pm
Error - Windowed functions cannot be used in the context of another windowed function or aggregate.
SELECT ROW_NUMBER() OVER (PARTITION BY Account ORDER BY A."Account") AS "RowNo", A."Account" AS "Account", (A."Debit") AS "Debit", SUM(A."Debit" - A."Credit") OVER (PARTITION BY Account ORDER BY ROW_NUMBER() OVER (PARTITION BY Account ORDER BY A."Account")) AS Balance From Master A
Thanks
WITH cte AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY A."Account" ORDER BY A."Account") AS "RowNo",
A."Account",
A."Debit",
A."Credit"
FROM Master A
)
SELECT "RowNo",
"Account",
"Debit",
SUM("Debit" - "Credit") OVER (PARTITION BY "Account" ORDER BY "RowNo") AS "Balance"
FROM cte;
March 26, 2025 at 2:32 pm
Error - Windowed functions cannot be used in the context of another windowed function or aggregate.
SELECT ROW_NUMBER() OVER (PARTITION BY Account ORDER BY A."Account") AS "RowNo", A."Account" AS "Account", (A."Debit") AS "Debit", SUM(A."Debit" - A."Credit") OVER (PARTITION BY Account ORDER BY ROW_NUMBER() OVER (PARTITION BY Account ORDER BY A."Account")) AS Balance From Master A
Thanks
There are several problems here.
ORDER BY
clause is non-deterministic. You're ordering by Account
within a group on Account
, so all of the records in the group necessarily have the same value, so ANY order is valid. Furthermore, this order could change every time you reference that value EVEN WITHIN THE SAME QUERY.ROW_NUMBER()
to set an order, but that order is in turn set by the ORDER BY
clause in the OVER()
clause. Therefore, the ROW_NUMBER()
is superfluous. Simply use the original ORDER BY
clause.RANGE UNBOUNDED PRECEDING
. Since you are using a partition field for your order by clause ALL of your records within the partition are within the range. This means that you are getting a grand total instead of a running total.Here is how I would rewrite your query. NOTE: I have left open the fields that specify a unique or primary key to use in determining the order.
SELECT A.Account AS Account
, (A.Debit) AS Debit
, SUM(A.Debit - A.Credit) OVER (PARTITION BY A.Account ORDER BY <Your unique key fields here> ROWS UNBOUNDED PRECEDING) AS Balance
From Master A
On another note, you shouldn't be using double quotes around your field names. If you want to use anything, you should use open and close square brackets. "[" and "]".
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
March 26, 2025 at 4:25 pm
Use SUM() instead of ROW_NUMBER()?
March 31, 2025 at 6:27 pm
On another note, you shouldn't be using double quotes around your field names. If you want to use anything, you should use open and close square brackets. "[" and "]".
Drew
Hmm... On what grounds do you advise against using double quotes and instead using the ugly square brackets? My understanding is that double quotes are quite okay, whilst single quotes aren't, since they are depricated. Or am I misinformed?
March 31, 2025 at 8:13 pm
drew.allen wrote:On another note, you shouldn't be using double quotes around your field names. If you want to use anything, you should use open and close square brackets. "[" and "]".
Drew
Hmm... On what grounds do you advise against using double quotes and instead using the ugly square brackets? My understanding is that double quotes are quite okay, whilst single quotes aren't, since they are depricated. Or am I misinformed?
It's Microsoft's standard to use square brackets for such things in T-SQL, despite it not being ANSI standard. And with square brackets, you don't run the risk of falling foul of the quoted identifier setting.
SET QUOTED_IDENTIFIER OFF;
SELECT TOP(10)
"name"
,[name]
FROM sys.databases;
April 1, 2025 at 8:58 am
kaj wrote:drew.allen wrote:On another note, you shouldn't be using double quotes around your field names. If you want to use anything, you should use open and close square brackets. "[" and "]".
Drew
Hmm... On what grounds do you advise against using double quotes and instead using the ugly square brackets? My understanding is that double quotes are quite okay, whilst single quotes aren't, since they are depricated. Or am I misinformed?
It's Microsoft's standard to use square brackets for such things in T-SQL, despite it not being ANSI standard. And with square brackets, you don't run the risk of falling foul of the quoted identifier setting.
SET QUOTED_IDENTIFIER OFF;
SELECT TOP(10)
"name"
,[name]
FROM sys.databases;
Thanks, makes sense. I don't usually use double quotes like the OP so hadn't thought about the SET QUOTED_IDENTIFIER setting in that regard. I strive to avoid having to use any of the quoting options, but occationally I do use double quotes as column alias if the output is used in for example an Excel spreadsheet where the column alias work as a column heading and need a more meaningful/readable name than the original column name. But I guess that's a different game.
SET QUOTED_IDENTIFIER OFF;
SELECT TOP(10)
name as "Meaningful column name for use in Excel"
,[name]
FROM sys.databases as d;
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy