|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Wierd query
I'm writing a report for a survey application. There is a subscriber table, a question table, and an answer table. The subscriber table holds info like the name, address, etc of the publication's subscribers. The Question table holds the questions on the survey (ie, How many widgets do you own? What is your age? How many do you think you will purchase in the coming year?). The Answer table holds answers to each question (18-21, 25-50%, 20+, etc). I am forced to work within a very non-standard database application running on MS SQL Server 2000 that also uses a separate Relationships table, so to determine which answers go with which questions the relationships table will have a record linking a question to an answer (and vice versa). To determine how each subscriber answered the question the subscibers have a record in the relationships table linking them with specific answers (and vice versa). Both types of entries are in the same table. There are several questions that will include a note to "check all that apply" (you may get several widgets from the company). The report runs through each question, shows how many subscribers answered the question, and how many selected each answer as a percentage of the total for that question. It is nearly finished, I can run it on the test data for any date, but there is one special case I'm having trouble with. For one question, they want to know additionally how many people chose one possible answer, but no others. I can do it using simple SQL to get the list of the subscribers that chose the answer in question, and then loop through each one in vb and run another query that will tell me how many answers related to that question they selected. This works okay for the test data, but in production there are more than 100,000 subscribers, which means running 100.000+ individual querys. I know it's possible to do this in one (all I want is the final count), but the SQL for this is horrid. Any help appreciated.
__________________
Primary Forums: .Net Development, MS-SQL, C Programming VB.Net: It's not your father's Visual Basic. [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers] Last edited by f'lar : December 6th, 2004 at 04:13 PM. |
|
#2
|
|||
|
|||
|
quick proposition of solution
-- MY QUICK SOLUTION:
-- -- tables: -- SUBSCRIBERS: -- - ID INT -- - NAME VARCHAR -- ANSWERS: -- - ID INT -- - ANSWER VARCHAR -- QUESTIONS: -- - ID INT -- - QUESTION VARCHAR -- RELATIONSHIPS: -- - ID1 INT -- - ID2 INT -- - TYPE CHAR(3) - TYPE OF RELATION QUESTION 2 ANSWER 'q2a' OR SUBSCIBER 2 ANSWER 's2a' select ( select count(*) from ( select CASE WHEN GROUPING(name)= 0 then name else name end as name_alias, case when grouping(r1.id2)=0 then r1.id2 else r1.id2 end as number_alias, sum(1) as sum_alias from relationships r1 join relationships r2 on r1.type = 'q2a' and r2.type = 's2a' and r1.id2 = r2.id2 join subscibers s1 on s1.id = r2.id1 where r1.id1=q.id group by s1.name,r1.id2 with rollup ) o where o.name_alias is not null and o.number_alias is null and o.sum_alias = 1 ) [number of subscribers with 1 question] from questions q It works for the db structure which picture I got from post, hope it's ok. |
|
#3
|
||||
|
||||
|
I finally finished a solution of my own this morning, but it's dog slow because I had to build it by trial and error. I'll adapt yours to the system (it's even a little more complicated than I said, I tried to simplify as much as possible) and let you know how it works, plus post the final sql.
|
|
#4
|
|||
|
|||
|
I agree that my solution is not the only one possible and if you have a better one I'd like to see it.
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Wierd query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|