Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
liveloveshare
Starting Member
1 Post |
Posted - 2011-11-14 : 12:10:40
|
Hey all.. First time poster. Did a little searching under 'combining records' but didn't find what I was after.I work for a company that sells crap on the internet. Pretty neat and unique right?We have multiple tier product tables to represent various products, styles, and options. So the hierarchy goes: PRODUCTS with the primary key being PR_ID >>>NEWSTYLES with the primary key being ST_ID linking back to the PRODUCTS table with ST_PRODUCTID >>>NEWSTYLES_OPTIONS with the primary key being NEWSTYLES_OPTION_ID linkin back to the NEWSTYLES table with Style_ID >>>here is my query:select pr_manufactid,name,sum(oneforone) as OneForOne,sum(oneforone_combo) as OneForOne_Combo,sum(multi) as Multi,sum(multi_combo) as Multi_Combo from(select b.*,case when num_styles = 1 and num_options = 1 then 1 else 0 end as OneForOne,case when num_styles = 1 and num_options > 1 then 1 else 0 end as OneForOne_Combo,case when num_styles > 1 and num_options = num_styles then 1 else 0 end as Multi,case when num_styles > 1 and num_options > num_styles then 1 else 0 end as Multi_Combo from(select pr_manufactid,name,pr_partnum,pr_title,pr_id as ProductID,count(pr_id) as Num_Styles,sum(Options) as Num_Options from(select pr_manufactid,name,pr_partnum,pr_title,pr_id,style_id,count(style_id) as Optionsfrom newstyles_option no inner join newstyles ns on ns.st_id = no.style_idinner join products p on p.pr_id = ns.st_productidinner join manufacturer m on m.categoryid = p.pr_manufactidwhere/* pr_manufactid = 73 and*/ st_status=0 and pr_status=0 and isnull(op_status,0)=0 and st_productid in ( select pr_id from products where pr_status = 0 and pr_subcategoryid in( select subcategoryid from subcategory where categoryid in ( select categoryid from category where type in( select categoryid from categorytype where categoryid = 15 ))))group by pr_id,style_id,pr_manufactid,name,pr_partnum,pr_title)agroup by pr_id,Options,pr_manufactid,name,pr_partnum,pr_title)b)cgroup by pr_manufactid,name,oneforone,oneforone_combo,multi,multi_comboorder by nameSo my results might look something like this:pr_manufactid name OneForOne OneForOne_Combo Multi Multi_Combo4 Access Lighting 0 0 12 04 Access Lighting 21 0 0 0OneForOne, OneForOne_Combo, Multi, and Multi_Combo represent the four different scenarios that can come into play.So I'm trying to effectively consolidate these records. I have designed the case statements such that there will never be more than one scenario occurring for a manufacturer. How can I merge these records efficiently..?Also if you have any suggestions on how to build my query better in general I'm definitely open to suggestions on that as well. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-14 : 13:22:55
|
sounds like belowselect pr_manufactid,name,sum(oneforone) as OneForOne,sum(oneforone_combo) as OneForOne_Combo,sum(multi) as Multi,sum(multi_combo) as Multi_Combo from(select b.*,case when num_styles = 1 and num_options = 1 then 1 else 0 end as OneForOne,case when num_styles = 1 and num_options > 1 then 1 else 0 end as OneForOne_Combo,case when num_styles > 1 and num_options = num_styles then 1 else 0 end as Multi,case when num_styles > 1 and num_options > num_styles then 1 else 0 end as Multi_Combo from(select pr_manufactid,name,pr_partnum,pr_title,pr_id as ProductID,count(pr_id) as Num_Styles,sum(Options) as Num_Options from(select pr_manufactid,name,pr_partnum,pr_title,pr_id,style_id,count(style_id) as Optionsfrom newstyles_option no inner join newstyles ns on ns.st_id = no.style_idinner join products p on p.pr_id = ns.st_productidinner join manufacturer m on m.categoryid = p.pr_manufactidwhere/* pr_manufactid = 73 and*/ st_status=0 and pr_status=0 and isnull(op_status,0)=0 and st_productid in ( select pr_id from products where pr_status = 0 and pr_subcategoryid in( select subcategoryid from subcategory where categoryid in ( select categoryid from category where type in( select categoryid from categorytype where categoryid = 15 ))))group by pr_id,style_id,pr_manufactid,name,pr_partnum,pr_title)agroup by pr_id,Options,pr_manufactid,name,pr_partnum,pr_title)b)cgroup by pr_manufactid,nameorder by name ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|