SQL Multiple Join/Sums on Product table -


i have following table containing product sales data chain store group. data big , ugly, way can stores. information table holds 22million records, growing 300k day , growth rate exponentially increase 100% on monthly basis.

store_purchases( [id] [int] identity(1,1) not null, [storecode] [int] null, [dtdatum] [datetime] null, [barcode] [varchar](50) null, [desc] [varchar](100) null, [qty] [int] null, [amount] [money] null, [tillslipid] [int] null) 

the query need pull top 500 products according sum of sales on date range. each of these products need show barcode, description, sum of quantity sold, sum of sale amounts, number of till slips product on, , sum total of products.

so far have managed come following. query takes 2m20s execute on server , think "basket sum" value incorrect. know if there better way of doing in single query, or in stored procedure.

select   a.barcode,    a.desc,    sum(b.amount) 'basket sum',   count(distinct b.tillslipid) 'basket count',    sales.count,    sales.amount  store_purchases b (nolock),  store_purchases (nolock),  (   select     top 500 barcode,     sum(qty) "count",      sum(amount) "amount"    store_purchases (nolock)   (storecode = 30143)   , dtdatum between '1-feb-2013 00:00:00' , '28-feb-2013 23:59:59'   group barcode   order sum(amount) desc ) sales (a.storecode = 30143) , a.dtdatum between '1-feb-2013 00:00:00' , '28-feb-2013 23:59:59' , a.barcode = sales.barcode  , a.tillslipid = b.tillslipid  , a.storecode = b.storecode , b.dtdatum between '1-feb-2013 00:00:00' , '28-feb-2013 23:59:59' group a.barcode, a.desc, sales.count, sales.amount order bysales.count desc 

any appreciated. thanks.

select top 500 barcode, [desc], sum([count]) [count], sum([amount]) amount, sum(basketsum) [basket sum], count(tillslipid) [basket count] (     select barcode, [desc], tillslipid, [count], [amount], sum(amount) on (partition tillslipid) basketsum     (         select tillslipid, barcode, [desc], sum(qty) [count], sum(amount) [amount]         ez_sparbuck_products p         (storecode = 30143) , dtdatum between '1-feb-2013 00:00:00' , '28-feb-2013 23:59:59'         group barcode, [desc], tillslipid,      ) t ) t group barcode, [desc] 

this makes assumption barcode doesn't have 2 [desc]s, which, given result you're trying achieve, seems case. if isn't case, same query changed use partition barcode , distinct.

for volumes of data doing on, think want clustered index on dtdatum, , maybe kind of partitioning on storecode.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -