Skip to content
Advertisement

subquery shows more that one row group by

I am trying to get the data for the best 5 customers in a railway reservation system. To get that, I tried getting the max value by summing up their fare every time they make a reservation. Here is the code.

this throws the error:[21000][1242] Subquery returns more than 1 row

If I remove the group by from the subquery the result is:(its a tabular form)

But that’s not what I want, as you can see, I want to add the name ‘Jade’ with the total fare.

Advertisement

Answer

I just don’t see the point for the subquery. It seems like you can get the result you want with a sum()

This sums all reservations of each client, and use that information to sort the resulstet. This guarantees one row per customer.

The query assumes that username is the primary key of table customer. If that’s not the case, you need to add columns firstname and lastname to the group by clause.

Note that this uses standard joins (with the inner join ... on keywords) rather than old-school, implicit joins (with commas in the from clause: these are legacy syntax, that should not be used in new code.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement