Skip to content
Advertisement

Is there a better way to execute this SQL query?

I have written this SQL query to get data for each customer in my database.

As you can see, I’m trying to get the total of unpaid orders and the total of orders to my query.

My goal at the end is to get only the users with unpaids orders (I think i will have to make it with a HAVING at the end of the query).

Here is my query

Do you believe there is a better way to get the records ?

How can I get the users with only one total_not_paid and only one total_order ?

Advertisement

Answer

If you want unpaid orders, you can use explicit aggregation and a having clause:

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