2010-08-05 6 views
0

는 다음과 같은 SQL 문을 고려해SQL : GROUP BY 문에서 더 많은 정보를 얻는 방법

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 

가 아니었다면 다른 행과 동일한 것이다 행을 식별 할 수있는 문을 수행 할 수있는 작업 그 (것)들의 invoice.shop_location 가치를 위해? 제거 Invoice.ShopLocation이 분야의 어떤 일치 나머지

답변

2

이 시도 둘 이상의 상점 위치

참고 : 최적화 여부를 잘 모르겠습니다.

+0

아마도 내 질문에 명확하지 않습니다. 다른 invoice.shop_location 때문에 결과에 어떤 행이 있는지 알고 싶습니다. – CJ7

+1

Sachin의 쿼리에 'HAVING count (DISTINCT invoice.shop_location)> 1'을 추가하면 정확하게 수행 할 수 있습니다. –

1
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 

가 위의 쿼리는 당신이 가지고있는 CUSTOMERNAME의 조합 ID를 줄 것이다 당신에게 모든 행을 제공

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

-

+0

원본 쿼리 결과에 행의 값이 있는지 확인하고 싶습니다. – CJ7

+0

내 머리 꼭대기에, 나는 당신이 당신의 쿼리를 당신의 결과를 얻기 위해이 쿼리에 합류 할 수 있다고 생각합니다. 나는 연구하고 더 많은 elagant가 있다면 게시 할 것입니다 ... –