sql - How to use distinct in a grouped query? -
i created view couple tables. each employee able work stores. each invoice have 1 product or more one. want show, store, employee, individual product total sales, total transaction (which means total invoice number), total of invoices.
here example table:
create table performanceview ([store] varchar(6), [employee] varchar(5), [product] varchar(8), [productprice] int,[invoiceno] int, [invoicetotal] int); insert 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);
i have tried code;
select store, employee, product, sum(productprice) producttotal , sum(invoicetotal) invoicetotal, count(distinct invoiceno) transactions performanceview group store,employee,product
but invoice total wrong because of sums same invoices have.
can check in here http://sqlfiddle.com/#!3/e419f/6
have tried on clause did not give same view.
your problem view holds single product sales (one line per piece of product sold), have redundantly invoice invoice amount it. when grouping store, employee , product, count invoice amount many-fold. must first group store, employee, product and invoiceno, , final group:
select store, employee, product, sum(producttotal) producttotal, sum(invoicetotal) invoicetotal, count(*) transactions ( select store, employee, product, sum(productprice) producttotal, min(invoicetotal) invoicetotal performanceview group store, employee, product, invoiceno ) pre_aggregate group store, employee, product;
here sql fiddle: http://sqlfiddle.com/#!3/e419f/32.
Comments
Post a Comment