sql server - SQL required data based on date -


i have been working on report required output. scenario block manufacturing firm having multiple orders of same client delivers orders on credit on different dates , clients pays amount partially irrespective of orders. have been stuck in these 2 tables:

orders_master,

 do_no   client_id   site_id order_date  amount 1       1           1       2013-10-27  50000 2       1           1       2013-10-29  47000 3       1           1       2013-10-15  10000 

client_payments,

 p_id    client_id   site_id p_date      amount 1       1           1       2013-11-05  30000 2       1           1       2013-11-10  67000 3       1           1       2013-11-20  10000 

i need write query gives following output rows both tables,

 do_no   client_id   site_id order_date  p_date      order_amount    payment_amount 1       1           1       2013-10-27  null        50000           null 2       1           1       2013-10-29  null        47000           null null    1           1       null        2013-11-05  null            30000 null    1           1       null        2013-11-10  null            67000 3       1           1       2013-11-15  null        10000           null null    1           1       null        2013-11-20  null            10000 

below query returns rows of orders_master table misses last row of required output shows above,

select om.*, cp.* orders_master om left join client_payment cp on  om.order_date = cp.p_date , om.site_id = cp.site_id om.site_id = 1 

i tried different joins not return rows of both columns, if returns repeating values , not nulls

it looks want use union [all] combine 2 tables, rather join:

select  do_no,         client_id,         site_id,         order_date,         p_date = null,         order_amount = amount,         payment_amount = null    orders_master   site_id = 1 union select  do_no = null,         client_id,         site_id,         order_date = null,         p_date = p_date,         order_amount = null,         payment_amount = amount    client_payments   site_id = 1; 

example on sql fiddle


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -