如何在分组查询中使用distinct?

时间:2014-11-27 05:26:29

标签: sql sql-server sql-server-2008

我从几个表创建了这个视图。每个员工都能够在所有商店工作。每张发票可以有一个产品或多个产品。我要展示的是什么, 商店,员工,个别产品总销售额,总交易额(即发票总数),发票总额 这是一个示例表:

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
我试过了条款,但它没有给出相同的观点。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您的问题是您的视图包含单个产品销售额(每件产品销售一行),但您还可以使用它冗余地计算发票发票金额。按商店,员工和产品进行分组时,您可以将发票金额计算多倍。因此,您必须首先按商店,员工,产品和invoiceno 分组,然后才能进入最后一组:

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;

这是你的SQL小提琴:http://sqlfiddle.com/#!3/e419f/32