Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select A, SUM(A) 1

Status
Not open for further replies.

boggg1

Programmer
Oct 17, 2002
102
0
0
EU
How do I do the following...

SELECT A, B, C, SUM(A)
FROM X

I found the following on an Oracle error site...

ORA-00937: not a single-group group function

Cause: A SELECT list cannot include both a group function, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, and an individual column expression, unless the individual column expression is included in a GROUP BY clause.

Action: Drop either the group function or the individual column expression from the SELECT list or add a GROUP BY clause that includes all individual column expressions listed.

...which presumably means I need a GROUP BY but I cannot seem to get it right.

I could run this as two queries (SELECT A, B, C ...and... SELECT SUM(A) but it seems a waste.

[bigglasses]
 
Perhaps:

SELECT A, B, C, (SELECT SUM(A) FROM X)
FROM X

or

SELECT A, B, C, SUM(A)
FROM X
GROUP BY A, B, C


HTH,
Jarl
 
When I try the second method I get data such as...

25 B1 C1 24
25 B2 C2 24
2 B3 C3 1
3 B4 C4 2

SUM(C) is always 1 less than C !

Where is it getting this last value (the SUM) from ? There are no negative values in the entire column.
I should say that the real query is considerably more complex, using multiple joined tables and where clauses. But nevertheless the Sum is not being performed as wanted.

_______________________

I have avoided the first method because the real query is 30 lines long and to put a SELECT inside a SELECT would make it 60 lines or so. I think.

[bigglasses]
 
JarlH's queries are correct

the problem lies in your scenario, which you have regrettably oversimplified

:)

r937.com | rudy.ca
 
OK, I'll do it as two queries.

Thanks

[bigglasses]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top