user2113001
user2113001

Reputation: 11

SQL display extra column in output

I'm trying to do a mySQL query whereby a & b need to be distinct, but I also want to show column c in the output. c has no bearing on the query - its more like a marker/index that makes the results of a, b make more sense.

SQL:

select distinct a, b
from tbl

Not sure how to force c as 1st column into o/p of above query.

Any ideas?

Many thanks indeed.

Upvotes: 1

Views: 1074

Answers (2)

Michael Fredrickson
Michael Fredrickson

Reputation: 37398

SELECT a, b, MAX(c) AS c
FROM tbl
GROUP BY a, b

While MySql will let you exclude columns from the GROUP BY that aren't in an aggregate function because of their "extension", it would be better to wrap the value in an aggregate such as MAX() in order to make the query portable and the results deterministic.

Upvotes: 2

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33391

Because you don't mentioned in your question which c must be in result set then you can try this:

select c, a, b
from tbl
group by a, b

Upvotes: 0

Related Questions