2014-11-27 2 views
0

부부 테이블에서이보기를 만들었습니다. 각 직원은 모든 상점에서 일할 수 있습니다. 각 인보이스에는 하나의 제품 또는 둘 이상의 제품이있을 수 있습니다. 표시하고자하는 내용 매장, 사원, 개별 제품 총 판매, 총 거래 (총 송장 수를 의미 함), 송장 총계.
여기 는 예를 들어 테이블 : 나는이 코드를 시도그룹화 된 쿼리에서 distinct를 사용하는 방법?

CREATE TABLE PerformanceView 
([Store] varchar(6), [Employee] varchar(5), [Product] varchar(8), [ProductPrice] int,[InvoiceNo] int, [InvoiceTotal] int); 

INSERT INTO PerformanceView 
([Store], [Employee], [Product], [ProductPrice], [InvoiceNo], [InvoiceTotal]) 
VALUES 
('Store1', 'Empl1', 'Product1', 577.2, 42152, 676.2), 
('Store1', 'Empl1', 'Product1', 99, 42152, 676.2), 
('Store1', 'Empl1', 'Product2', 20, 41969, 20), 
('Store1', 'Empl1', 'Product2', 25, 41649, 25), 
('Store1', 'Empl2', 'Product2', 300, 43112, 300), 
('Store1', 'Empl2', 'Product2', 450, 42440, 450), 
('Store1', 'Empl2', 'Product3', 226, 41666, 2040.14), 
('Store1', 'Empl2', 'Product3', 282.17, 42232, 1678.5), 
('Store1', 'Empl2', 'Product3', 934.67, 41666, 2040.14), 
('Store1', 'Empl2', 'Product3', 1264.56, 42232, 1678.5), 
('Store1', 'Empl2', 'Product4', 49.81, 41666, 2040.14), 
('Store1', 'Empl2', 'Product4', 683.32, 41666, 2040.14), 
('Store1', 'Empl2', 'Product4', 0, 42232, 1678.5), 
('Store1', 'Empl2', 'Product4', 81.92, 42232, 1678.5), 
('Store1', 'Empl2', 'Product4', 146.34, 41666, 2040.14), 
('Store1', 'Empl2', 'Product4', 49.85, 42232, 1678.5), 
('Store1', 'Empl2', 'Product5', 30, 43110, 30), 
('Store1', 'Empl3', 'Product1', 172.5, 42875, 172.5), 
('Store3', 'Empl4', 'Product2', 51, 42801, 51), 
('Store3', 'Empl4', 'Product2', 51, 42488, 51), 
('Store3', 'Empl4', 'Product2', 80.002, 42463, 80.002), 
('Store3', 'Empl4', 'Product2', 22, 42559, 22), 
('Store3', 'Empl4', 'Product2', 20, 42963, 20), 
('Store3', 'Empl4', 'Product2', 35, 42436, 35), 
('Store3', 'Empl4', 'Product2', 107, 43014, 107), 
('Store3', 'Empl4', 'Product2', 59, 42615, 59), 
('Store3', 'Empl4', 'Product2', 110, 42025, 110), 
('Store3', 'Empl4', 'Product2', 89, 42137, 148), 
('Store3', 'Empl4', 'Product2', 50, 42565, 100), 
('Store3', 'Empl4', 'Product2', 59, 42137, 148), 
('Store3', 'Empl4', 'Product5', 25, 42565, 100), 
('Store3', 'Empl4', 'Product5', 25, 42565, 100), 
('Store3', 'Empl5', 'Product2', 22, 42295, 44), 
('Store3', 'Empl5', 'Product2', 22, 42295, 44), 
('Store3', 'Empl5', 'Product2', 439, 41723, 439), 
('Store3', 'Empl5', 'Product2', 197, 42267, 197), 
('Store3', 'Empl5', 'Product2', 150, 41780, 150), 
('Store3', 'Empl5', 'Product2', 539, 42280, 539), 
('Store3', 'Empl5', 'Product2', 63, 41689, 63), 
('Store3', 'Empl5', 'Product2', 269, 42274, 269), 
('Store3', 'Empl5', 'Product2', 134, 41731, 134), 
('Store2', 'Empl3', 'Product1', 0, 43076, 0), 
('Store2', 'Empl3', 'Product1', 149.49, 42557, 666.73), 
('Store2', 'Empl3', 'Product1', 0, 43076, 0), 
('Store2', 'Empl3', 'Product1', 0, 42917, 0), 
('Store2', 'Empl3', 'Product1', 61.01, 42557, 666.73), 
('Store2', 'Empl3', 'Product1', 389.4, 42557, 666.73), 
('Store2', 'Empl3', 'Product1', 66.84, 42557, 666.73), 
('Store2', 'Empl3', 'Product1', 0, 43076, 0), 
('Store2', 'Empl3', 'Product1', 15.42, 41650, 15.42), 
('Store2', 'Empl3', 'Product1', 0, 43088, 0), 
('Store2', 'Empl3', 'Product1', 0, 42920, 0), 
('Store2', 'Empl3', 'Product6', 0, 43076, 0); 

;

select 
STORE, 
EMPLOYEE, 
PRODUCT, 
SUM(ProductPrice) as ProductTotal , 
SUM(InvoiceTotal) as InvoiceTotal, 
Count(Distinct InvoiceNo) as Transactions 
from 
PerformanceView 
GROUP BY STORE,EMPLOYEE,PRODUCT 

그러나 송장 합계가 잘못되었습니다.
여기에서 확인할 수 있습니다. http://sqlfiddle.com/#!3/e419f/6
나는 over 절을 시도했지만 동일한보기를 제공하지 않았습니다. 입력 데이터보고에서

+0

동일한 인보이스가있는 경우 아니요, 총 합계에 포함 시키시겠습니까? 최소 또는 최대? –

+0

각 행에 저장된 InvoiceTotal은 전체 송장의 합계입니까, 아니면 인보이스의 해당 제품 합계입니까? 예를 들어, Invoice 42232는 여러 번 나타나고 동일한 InvoiceTotal을 갖지만 해당 행에 대해 얼마만큼의 제품이 판매되었는지 표시하지 않습니다. 해당 InvoiceTotal 중 얼마나 많은 것이 해당 제품에 속하는지 알기 위해 사용할 수있는 것이 있어야합니다. 그런 다음 요약 할 수 있습니다. – Vinnie

답변

0

귀하의 문제는 단일 제품 판매 (제품 판매 당 한 줄)를 보유하고 있지만, 인보이스 인보이스 금액을 중복 처리하십시오. 상점, 직원 및 제품별로 그룹화 할 때 송장 금액을 여러 배로 계산합니다. 그래서 반드시 저장, 직원, 제품 invoiceno, 오직 다음 마지막 그룹에 도착하여 첫 번째 그룹 : http://sqlfiddle.com/#!3/e419f/32 : 여기에 귀하의 SQL 바이올린이

select 
    store, 
    employee, 
    product, 
    sum(producttotal) as producttotal, 
    sum(invoicetotal) as invoicetotal, 
    count(*) as transactions 
from 
(
    select 
    store, 
    employee, 
    product, 
    sum(productprice) as producttotal, 
    min(invoicetotal) as invoicetotal 
    from performanceview 
    group by store, employee, product, invoiceno 
) as pre_aggregate 
group by store, employee, product; 

입니다.

+0

고마워.이게 내가 원하는거야. – funkoff

0

, 나는 같은 중복 행을주의 사항 :

('Store3', 'Empl4', 'Product5', 25, 42565, 100), 
('Store3', 'Empl4', 'Product5', 25, 42565, 100), 
('Store3', 'Empl5', 'Product2', 22, 42295, 44), 
('Store3', 'Empl5', 'Product2', 22, 42295, 44), 

그래서 이런 형태의 인라인 뷰이 사용을 소독하기 위해 : 지금처럼

select [Store], [Employee], [Product], [ProductPrice], [InvoiceNo], [InvoiceTotal] 
from PerformanceView 
group by [Store], [Employee], [Product], [ProductPrice], [InvoiceNo], [InvoiceTotal] 

:

select 
    pv.STORE, 
    pv.EMPLOYEE, 
    pv.PRODUCT, 
    SUM(pv.ProductPrice) as ProductTotal , 
    SUM(pv.InvoiceTotal) as InvoiceTotal, 
    Count(Distinct pv.InvoiceNo) as Transactions 
from 
(
    select [Store], [Employee], [Product], [ProductPrice], [InvoiceNo], [InvoiceTotal] 
    from PerformanceView 
    group by [Store], [Employee], [Product], [ProductPrice], [InvoiceNo], [InvoiceTotal] 

) pv 
GROUP BY pv.STORE, pv.EMPLOYEE, pv.PRODUCT 
관련 문제