tags:

views:

31

answers:

2

Consider the following SQL statement:

SELECT 
invoice.customer_name, 
invoice.customer_id, 
invoice.shop_location,
sum(invoice.cost)
FROM invoice
GROUP BY
invoice.customer_name, invoice.customer_id, invoice.shop_location

What can be done to the statement to identify which rows would be identical to other rows if it weren't for their invoice.shop_location value?

+2  A: 

Try this - Removing Invoice.ShopLocation

SELECT  
invoice.customer_name,  
invoice.customer_id,  
sum(invoice.cost) 
FROM invoice 
GROUP BY 
invoice.customer_name, invoice.customer_id

This gives you all rows which match rest of the fields

Sachin Shanbhag
Perhaps my question isn't clear. I want to know which rows exist in the result only because of a different invoice.shop_location
Craig Johnston
Add `HAVING count(DISTINCT invoice.shop_location) > 1` to Sachin's query would do exactly that.
Mark Bannister
A: 
Select customer_name, Customer_id, count(Shop_location) from
(
    SELECT  
        invoice.customer_name,  
        invoice.customer_id,  
        invoice.shop_location, 
        sum(invoice.cost) 
    FROM invoice 
    GROUP BY 
        invoice.customer_name, 
        invoice.customer_id,
        invoice.shop_location 
 )
 Group by customer_name, Customer_id
 Having    count(Shop_location) > 1

The above query will give you the combination of Customername and id who have more than one shop location

Note : I'm not sure whether this is optimised

The King
I want the original query result to have a value in the row to identify this.
Craig Johnston
on top of my head, I think you could join this query with your query to get your result... I will research and post if there is something more elagant...
The King