【问题标题】:How to use distinct in a grouped query?如何在分组查询中使用 distinct?
【发布时间】:2015-01-25 15:16:07
【问题描述】:

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

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 子句,但它没有给出相同的观点。

【问题讨论】:

  • 如果有相同的invoiceNo,你想在总数中包括什么?最小值还是最大值?
  • InvoiceTotal 是与每一行一起存储的是整个发票的总计,还是发票中仅该产品的总计?例如,Invoice 42232 多次出现并且具有相同的 InvoiceTotal,但没有说明该行销售了多少产品。您需要有一些东西可以用来了解该 InvoiceTotal 中有多少属于该产品,然后您可以对其进行总结。

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


【解决方案1】:

您的问题是您的视图包含单个产品销售(每件产品销售一条线),但您也有多余的发票发票金额。当按商店、员工和产品分组时,您会多次计算发票金额。因此,您必须首先按商店、员工、产品和 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

【讨论】:

  • 谢谢 这就是我想要的。
【解决方案2】:

通过查看输入数据,我注意到重复的行,例如:

('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

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2021-03-27
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多