0

Lets say I have a Table with 3 columns

|aaa | bbb | ccc|
|---------------|
|111 | 123 | uuu|
|333 | 234 | uuu|
|555 | 345 | nnn|

Now I select a sum like:

SELECT *, sum(bbb) as bbb from myTable GROUP BY ccc

I recive

|aaa | bbb | ccc | bbb|
|---------------------|
|333 | 234 | uuu | 357|
|555 | 345 | nnn | 345|

bbb is set new in output...

Is there a way to replace the column that exists so I get:

|aaa | bbb | ccc |
|----------------|
|333 | 357 | uuu |
|555 | 345 | nnn |

I know I could use an other name but using an other name is not the question :)

3
  • just do as select aaa,sum(bbb) as bbb,ccc from myTable GROUP BY ccc Commented Mar 4, 2015 at 12:57
  • Just modify the query to select the columns that you want. But, you have a malformed group by, with a column in the select that is not in the group by. Commented Mar 4, 2015 at 12:57
  • the thing is there are about 25 columns... and typing everycolumn is kind of... annoing since I dont know if there will be some columns added in future ^^ Commented Mar 4, 2015 at 12:58

2 Answers 2

3

You have to add all columns you need to the select clause:

SELECT aaa, sum(bbb) as bbb,ccc from myTable GROUP BY ccc
Sign up to request clarification or add additional context in comments.

5 Comments

the thing is there are about 25 columns... and typing everycolumn is kind of... annoing since I dont know if there will be some columns added in future ^^
There is no other way :(
Also no way to select all fields except one ?
Ok, ill accept this one as soons as i'm allowed :) thx anyways.
It's probably better to mention each column in the query anyway, then changes in the underlying database structure don't affect your application, allowing updates of the database structure before you roll out a new version of your application, reducing the amount of time your application is unusable.
3

Don't include that column in the SELECT part of the query.

SELECT aaa, sum(bbb) as bbb, ccc from myTable GROUP BY ccc;

1 Comment

1up because you were 20sec later than the other poster, otherwise I had accepted this one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.