mysql - Combine two tables to display desired output -


i'm creating query display projected qty versus qty sold on monthly basis. below 2 tables , third 1 want output. 2 table same, except date , creation date in different format (i have converted output e.g 201301, no worries it). have noticed in output table, qty sold become 25.00 because adds qty if date falls under same month , year. need consider these 3 important conditions:

  1. to display rows projected qty without sales (ex. item 206)
  2. to display rows without projected qty sales (ex. item 312)
  3. to display row projection , sales (ex. item001 & 040)

i don't know if have used join or union able achieve desired output.

table (sales table)

item     code          sold       date 001       cust001      10.00     2013-01-20 001       cust001      15.00     2013-01-25  040       cust045      16.00     2013-04-07 312       cust001      20.00     2013-03-13 

table b (projection table)

item       custcode    projectedqty       creation date 001        cust001     20.00              2013-01-01 040        cust045     50.00              2013-04-01 206        cust121     60.00              2013-04-01 

output

item       custcode    sold     date          projectedqty         creation date 001       cust001      25.00     201301         20.00                 201301 312       cust001      20.00     201303    null                   null 040        cust045    16.00     201304          50.00                 201304 206       cust121      null     null            60.00                 201304 

thanks guys helping.

try

select coalesce(p.item, s.item) item,        coalesce(p.code, s.code) code,         sold,         concat(s.year, lpad(s.month, 2, '0')) date,         projectedqty,         concat(p.year, lpad(p.month, 2, '0')) creationdate  (   select item, code, year(`date`) year, month(`date`) month      sales    union   select item, custcode, year(`creationdate`), month(`creationdate`)     projection ) left join (   select item, code, sum(sold) sold, year(`date`) year, month(`date`) month     sales    group item, code, year(`date`), month(`date`) ) s on i.item  = s.item     , i.code  = s.code     , i.year  = s.year     , i.month = s.month left join  (   select item, custcode code, sum(projectedqty) projectedqty, year(`creationdate`) year, month(`creationdate`) month     projection    group item, custcode, year(`creationdate`), month(`creationdate`) ) p on i.item  = p.item     , i.code  = p.code     , i.year  = p.year     , i.month = p.month order code, item 

output:

| item |    code |   sold |   date | projectedqty | creationdate | ------------------------------------------------------------------ |  001 | cust001 |     25 | 201301 |           20 |       201301 | |  312 | cust001 |     20 | 201303 |       (null) |       (null) | |  040 | cust045 |     16 | 201304 |           50 |       201304 | |  206 | cust121 | (null) | (null) |           60 |       201304 | 

sqlfiddle


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -