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 |
|
sfortner
Yak Posting Veteran
63 Posts |
Posted - 2010-09-05 : 23:56:52
|
| Hey Gurus,I've got an interesting real-life problem I thought I'd check with you on. I've got one table called SurveyAnswers with a SurveyID (int), SurveyQuestion (int) and a answer column (tinyint) with a 1, 2 or 3 in it (SeverityID). Table 2 is named SurveyKey with SurveyQuestion (int) and a comma-delimited list of ProductIDs which is a varchar(50). Table 3 is a Products table with ProductID (int) and Name (varchar(50)). It's a survey which lets a user indicate a particular severity, 1 being mild, 2 moderate and 3 severe. The SurveyAnswers might look like this for a SurveyID of 1 (one particular survey): 1 | 1 | 21 | 2 | 11 | 3 | 11 | 4 | 3The SurveyKey table might look like this:1 | 3,5,62 | 1,33 | 44 | 2,7And the Products table looks like this:1 | Product12 | Product2etc.The goal is to determine how many products are associated with the questions based on the severity. If severity is 1, one instance of the product is needed. If severity is 2, two instances of the product is needed, and if severity is 3, three instances. In our example, for question 1 with severity 2 we haveProduct3 x 2Product5 x 2Product6 x 2For question 2 we haveProduct1 x 1Product3 x 1For question 3 we haveProduct4 x 1And for question 4 we have Product2 x 3Product7 x 3To sum it, we'd haveProduct1 - 1Product2 - 3Product3 - 3Product4 - 1Product5 - 2Product6 - 2Product7 - 3I'm using Peso's csv split fcn here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648 (with Kristen's fix) as a UDF. The problem is that I'm having trouble combining the ProductIDs along with the complexity. I thought about an inner derived table which would return a table with just the ProductIDs each in a column by themselves, but multiple rows, but that doesn't seem to work since they are coupled with severity and are also themselves comma-separated and on individual rows within the SurveyKey.I thought about converting the SurveyKey table to have one SurveyQuestion and ProductID for each row, but that's no fun. I set this up the way I did because I thought it was going to be easy to parse the comma-delimited list and couple that with the severity. It's throwing me - so I'm asking for help! Please, no cursors or while loops - I'd convert the SurveyKey to contain individual ProductID before I'd use that! TIA, Steve--Steve |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-09-06 : 00:25:22
|
[code]-- Sample Tabledeclare @SurveyAnswers table( SurveyID int, SurveyQuestion int, Answer int)declare @SurveyKey table( SurveyQuestion int, ProductID varchar(10))declare @Products table( ProductId int, [Name] varchar(10))-- Sample Datainsert into @SurveyAnswersselect 1, 1, 2 union allselect 1, 2, 1 union allselect 1, 3, 1 union allselect 1, 4, 3insert into @SurveyKeyselect 1, '3,5,6' union allselect 2, '1,3' union allselect 3, '4' union allselect 4, '2,7'insert into @Productsselect 1, 'Product1' union allselect 2, 'Product2' union allselect 3, 'Product3' union allselect 4, 'Product4' union allselect 5, 'Product5' union allselect 6, 'Product6' union allselect 7, 'Product7'-- The Queryselect p.[Name], Answer = sum(a.Answer)from @SurveyAnswers a inner join @SurveyKey k on a.SurveyQuestion = k.SurveyQuestion cross apply dbo.CSVTable(k.ProductID) csv inner join @Products p on csv.numberval = p.ProductIdgroup by p.[Name][/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sfortner
Yak Posting Veteran
63 Posts |
Posted - 2010-09-06 : 21:29:42
|
| Fantastic khtan - that cross apply was just what I needed! This worked like a dream. Thanks again!--Steve |
 |
|
|
|
|
|
|
|