【发布时间】: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