| Author |
Topic |
|
uholbrx
Starting Member
4 Posts |
Posted - 2008-02-28 : 12:06:22
|
| I have a form that has many checkboxes (more than 40) that provide information about companies. For example. Company ABC Checkbox 1 (Windows XP)Checkbox 2 (Windows Vista)Checkbox 3 (Windows NT4)etc.I save these in a SQL table like the followingRow 1 - Col 1 (CompanyID), col2 (checkbox value)Row 2 - Col 1 (CompanyID), col2 (checkbox value)etc.. so a comapany can have multiple checkbox selected..I am currently developing a report where the end user can select one or many of the checkbox to see if the company exist. This is where I get stuck - I only want to return the results of the companies that meet the report selections. So if I want to see all the companies that current have Windows NT and Windows Vista...how would I build that querry based on the data model above. I have tried using the IN clause - however that use the OR connector and doesnt show ONLY the compaines that meet. I have tried using a UNION and INTERSECT, and running the select statement many times.. (SELECT CompanyID from xx WHERE CheckboxVal = xx)INTERSECT(SELECT CompanyID from xx WHERE CheckboxVal = xx)etc.. howevert this creates a very large quesry that is unable to be handled by SQL 2005. I get a error message asking to minimize the query. Any suggestions on this please...this is my 4th day working on this.. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-28 : 12:18:04
|
| [code]SELECT t.CompanyIDFROM(SELECT CompanyID from Table WHERE CheckboxVal = xxUNION ALLSELECT CompanyID from Table WHERE CheckboxVal = yy)tGROUP BY t.CompanyIDHAVING COUNT(*) =2[/code] |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-02-28 : 12:43:29
|
Visakh's method is probably the fastest. But, if you need a method to handle a variable number of check boxes here is a sample (note you would need to populate the "ToFind" table in some way, possible a separated list that you would need to split or something)DECLARE @CompanyOS TABLE (CompanyID INT, CheckID INT)INSERT @CompanyOSSELECT 1, 1UNION ALL SELECT 1, 2UNION ALL SELECT 1, 3UNION ALL SELECT 2, 2UNION ALL SELECT 2, 3UNION ALL SELECT 3, 1UNION ALL SELECT 3, 3DECLARE @ToFind TABLE (CheckID INT)INSERT @ToFindSELECT 2UNION ALL SELECT 3-- Dynamic methodSELECT C.CompanyIDFROM @CompanyOS AS CINNER JOIN @ToFind AS T ON C.CheckID = T.CheckIDGROUP BY C.CompanyIDHAVING COUNT(*) = (SELECT COUNT(*) FROM @ToFind) |
 |
|
|
uholbrx
Starting Member
4 Posts |
Posted - 2008-02-28 : 12:55:04
|
| I not sure this approach would work - I have tried a similar approach using the following:BEGINCREATE TABLE #MyTempTable3 (inTEFirmID int PRIMARY KEY)INSERT #MyTempTable3 EXEC(@vChkBoxes)ENDWhere @vCheckboxes was a srting I parsed within ASP.NET code. A sample is like: 'SELECT nCompanyID FROM Company WHERE (inChkVal = 1) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 3) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 5) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 10) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 15) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 22) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 54) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 55) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 56) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 58) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 59) INTERSECTSELECT nCompanyID FROM Company WHERE (inChkVal = 63)' I run perfect when the query is only about 5-10 options selected - however the potentail exist the end user could select many checkboxes - which could be over 50... I get the following message when this happens.."The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information."I have reported this to our DBA - however they said this issues was fixed via a SP2 (SQL 2005 BTW) update and that the single statement im trying to execute is too large to parse... But im not sure of any other route to accomplish this.. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-02-28 : 13:16:34
|
If you are building a dynamic sql query in ASP you could modify the sample I provided to be soemthing like:SELECT C.nCompanyIDFROM @Company AS CINNER JOIN ( SELECT 1 AS inChkVal UNION ALL SELECT 2 UNION ALL SELECT 10 UNION ALL SELECT 59 -- ETC.. ) AS T ON C.inChkVal = T.inChkValGROUP BY C.nCompanyIDHAVING COUNT(*) = -- Whatever the count should be. |
 |
|
|
uholbrx
Starting Member
4 Posts |
Posted - 2008-02-28 : 14:36:24
|
| WOW! - I just implemented this code into my .net code - and WOW does it run fast. I am still trying to piece together and fully understand it myself - but MANY THANKS for this and you've saved my sanity - at least for now.. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-28 : 16:02:30
|
Or something like thisSELECT nCompanyIDFROM CompanyWHERE inChkVal IN (1, 3, 5, 10, 15, 22, 54, 55, 56, 58, 59, 63)GROUP BY CompanyHAVING COUNT(DISTINCT inChkVal) = 12 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
uholbrx
Starting Member
4 Posts |
Posted - 2008-02-28 : 16:35:39
|
| This just keeps getting better and better... This is even easier than before - Thanks Again. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-28 : 16:47:50
|
I missed an "n" character in the GROUP BY clause.GROUP BY nCompany E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|