tags:

views:

86

answers:

1

A query in a system I maintain returns

QID AID DATA
1   2   x
1   2   y
5   6   t

As per a new requirement, I do not want the (QID, AID)=(1,2) pair to be repeated. We also dont care what value is selected from "data" column. either x or y will do.

What I have done is to enclose the original query like this

SELECT * FROM (<original query text>) Results group by QID,AID

Is there a better way to go about this? The original query uses multiple joins and unions and what not, So I would prefer not to touch it unless its absolutely necesary

+2  A: 

If you don't care which DATA will be selected, GROUP BY is nice, though using ungrouped and unaggregated columns in SELECT clause of a GROUP BY statement is MySQL specific and not portable.

Quassnoi
erm how is GROUP BY MySQL specific? i'm like 99.9% sure it's part of the SQL standard. If it's not it's in almost every implementation.
xenoterracide
@xenoterracide: SQL standard does not allow using ungrouped columns in the `SELECT` clause. This query (with `*`) would fail in any engine except `MySQL`.
Quassnoi
@Quassnoi so what would be the standard sql solution in this case?
Midhat
@Midhat: no single one, since all engines use different syntax for `LIMIT` / `TOP` / `ROW_NUMBER`.
Quassnoi