Re: PGSQL-to-MYSQL Migration: Error in a 'simple' inner join query

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: "DaNieL(dot)(dot)!" <daniele(dot)pignedoli(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: PGSQL-to-MYSQL Migration: Error in a 'simple' inner join query
Date: 2009-05-05 18:18:18
Message-ID: 1241547498.19563.43.camel@monkey-cat.sm.truviso.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 2009-05-05 at 00:10 -0700, DaNieL..! wrote:
> But still i have some trouble to understand the functionality of the
> orders example.
> My first goal is to retrieve every order, the customer name, and the
> total of the idems per order.. so (from my point of view) i *dont*
> need and *dont* ant to group by the costumer name, becose every
> costumer can have many orders...
> and, as i understand the query:

If I understand you correctly: every customer can have many orders, and
every order can have many items, and you want the sum of all items per
order per customer.

So, you need to group by the combination of order and customer. Grouping
by the combination of those two is _not_ the same as grouping by order,
and then grouping by customer.

> ---
> SELECT
> orders.code,
> customer.name,
> SUM(order_item.price)
> FROM
> orders
> INNER JOIN customer ON (customer.id = orders.id_customer)
> INNER JOIN order_item ON (order_item.id_order = orders.id)
> GROUP BY orders.id, customer.name
> ---

In this query, you should "GROUP BY orders.code, customer.name".

> it *try* to group by the customer name, but *fail* becose the GROUP
> priority is at the order's code.. how much am i far from it?

What is "GROUP priority"? I don't think that has any meaning.

> but aswell i cant understand why i must group for the costumer name,
> when i dont want and dont need it to be *unique*

customer.name will _not_ necessarily be unique, if you "GROUP BY
orders.code, customer.name". The _combination_ of orders.code and
customer.name" will be unique.

Regards,
Jeff Davis

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2009-05-05 19:53:30 Re: recover corrupt DB?
Previous Message Andy Colson 2009-05-05 17:59:21 Re: PGSQL-to-MYSQL Migration: Error in a 'simple' inner join query