mysql - Sum items and not already in use -


insert results(month,region,item, yield12mo,unitsshipped)  select m.period,s.region,s.item,avg(s.yield), sum(s.shippedqty)  sales s, period m s.buydate >= date_sub(m.startdate,interval 12 month) , s.buydate <= m.enddate group s.item; 

sales table contains items, sale dates, prices, etc.

period table contains time frame in question (altered auto-script)

i need find way add yield12mo results if total qty shipped each item between selected date range >= 10 , region + item combo not in results table.

if less 10 shipped in time period or if region + item combo in results, not add.

i know need type of if statement or case statement combined join, i'm new , cannot figure it.

any appreciated. thank you!

this tough 1 because know little schema, think following should work. disclaimer: have ever worked in mssql, not mysql, of syntax may different...

insert results(month, region, item, yield12mo, unitsshipped)  select m.period,s.region,s.item,avg(s.yield), sum(s.shippedqty)  sales s inner join period m -- preferred method, yours may work fine on s.buydate >= date_sub(m.startdate,interval 12 month)     , s.buydate <= m.enddate --first we'll try , filter out exists in results   s.sales not in   (    select sales.sales     sales         inner join results            on  sales.region = results.region           , sales.item = results.item   ) group s.item --we still have make ones have shippedqty >=10. having sum(s.shippedqty) >= 10 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -