sql server - T-SQL Return Records that do not Exist -


i'm new sql , need way return either null or 0 years , quarters no records exist each surgeon. there 2 product families should return (knee , shoulder) , need missing year/quarter if surgeon has ever used product in family. example if smith, john ever had knee not need nulls shoulder.

here query have far, haven't been able figure out how include years , quarters have no values.

select  q.surgeon_name ,         q.product_family ,         first_date ,         q.year ,         q.quarter ,         cast(round(q.monthly_average, 2) money) 'monthly average'    ( select distinct                     rtrim(confirmed_to) 'surgeon_name' ,                     product_family ,                     year(rev_ship_date) 'year' ,                     datename(quarter, cast(rev_ship_date datetime)) 'quarter' ,                     ( sum(amount) / 3 ) 'monthly_average'                vsoicl                     left join product_line_desc on vsoicl.part_id = product_line_desc.part_id               vsoicl.product_line <> 'mirr'                     , product_family not null                     , product_family <> 'null'                     , confirmed_to <> ''                     , rev_ship_date >= '2013-01-01'           group  confirmed_to ,                     product_family ,                     year(rev_ship_date) ,                     datename(quarter, cast(rev_ship_date datetime))         ) q         left join ( select distinct                             rtrim(confirmed_to) 'surgeon_name' ,                             product_family ,                             cast(min(rev_ship_date) date) 'first_date'                        vsoicl                             left join product_line_desc on vsoicl.part_id = product_line_desc.part_id                       vsoicl.product_line <> 'mirr'                             , product_family not null                             , product_family <> 'null'                     group confirmed_to ,                             product_family                   ) f on q.surgeon_name = f.surgeon_name                             , q.product_family = f.product_family order surgeon_name ,         product_family ,         year ,         quarter 

any assistance appreciated.

i figured out late last night.

select  snyq.surgeon_name, snyq.product_family, f.first_date, snyq.year, snyq.quarter, sn_sales.[monthly_average] ( select *      (select distinct rtrim(confirmed_to) 'surgeon_name'     ,product_family      vsoicl     left join product_line_desc      on vsoicl.part_id = product_line_desc.part_id       vsoicl.product_line <> 'mirr'     , product_family not null     , product_family <> 'null'     , rev_ship_date between '2013-01-01' , getdate()      group confirmed_to, product_family) sn,   ( select distinct year(rev_ship_date) year, datepart(quarter,rev_ship_date) quarter     vsoicl     rev_ship_date between '2013-01-01' , getdate()) yq ) snyq left join  (     select q.surgeon_name ,q.product_family   ,q.year  ,q.quarter  ,cast(round(q.monthly_average,2) money) 'monthly_average'  (select distinct rtrim(confirmed_to) 'surgeon_name' ,product_family ,year(rev_ship_date) 'year' ,datename(quarter, cast(rev_ship_date datetime)) 'quarter' ,(sum(amount) / 3) 'monthly_average'  vsoicl left join product_line_desc  on vsoicl.part_id = product_line_desc.part_id   vsoicl.product_line <> 'mirr' , product_family not null , product_family <> 'null' , confirmed_to <> '' , rev_ship_date >= '2013-01-01'  group confirmed_to, product_family, year(rev_ship_date), datename(quarter, cast(rev_ship_date datetime))) q  ) sn_sales on snyq.surgeon_name=sn_sales.surgeon_name , snyq.product_family=sn_sales.product_family , snyq.year=sn_sales.year , snyq.quarter=sn_sales.quarter  left join (select distinct rtrim(confirmed_to) 'surgeon_name' ,product_family ,cast(min(rev_ship_date) date) 'first_date'  vsoicl left join product_line_desc  on vsoicl.part_id = product_line_desc.part_id   vsoicl.product_line <> 'mirr' , product_family not null , product_family <> 'null' , (stores_code <> 'qu' , so_status <> 'x' , so_line_status <> 'x' , ship_type not in('o', 'c')) , (cust_po_id not '%demo%' , cust_po_id not '%expired%' , cust_po_id not '%literature%' , cust_po_id not '%toss%' , cust_po_id not '%replace%' , cust_po_id not '%swap%' , cust_po_id not '%exchange%' , cust_po_id not '%cancel%' , cust_po_id not '%duplicate%') , order_class <> 'r'  group confirmed_to, product_family) f on snyq.surgeon_name=f.surgeon_name , snyq.product_family=f.product_family  order surgeon_name,product_family,year,quarter 

i had read sql in haste, , revised thought be

create mycalendar table (myyyyy, myqq) years/quarters of interest. small table, 10 years having 40 rows

then use cross join "multiply" surgeon/product each yyyyqq

select     just.surgeon_name ,     just.product_family ,      myyyyy ,     myqq ,      first_date ,     [monthly average]   (     (select  distinct                 rtrim(confirmed_to) 'surgeon_name' ,                 product_family ,            vsoicl           product_line <> 'mirr'             , product_family not null             , product_family <> 'null'             , confirmed_to <> ''             , rev_ship_date >= '2013-01-01'       )    ,  mycalendar        )  left join     (the above sql) myv      on    just.surgeon_name = myv.surgeon_name      ,   just.product_family= myv.product_family      ,   myyyyy = year      ,   myqq = quarter 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -