| Author |
Topic |
|
SDK
Starting Member
3 Posts |
Posted - 2008-06-10 : 07:45:46
|
| Hi Everyone.Looong story short. I have a screen with search criteria. Right.I have table : tblCandidateLicenseintPkCandidateLicenseID (PK)intFkCandidateId intFkLicenseTypeIDAnd Another tabletblLicenseTypeDefColumns:intPkLicenseTypeID (Pk Int)strDescriptionstrLicenseCodeintRank (just for ranking)intRankParent (for grouping types of vehicles)tblLicenseTypeDef data contains:1 , MotorCycle < 125,0,1,02 , MotorCycle > 125,0,2,03 , Car 1 ,0,0,14 , Car 2 ,0,1,15 , Car 3 ,0,2,15 , Car 4 ,0,3,1tblCandidateLicense data contains1 , 57, 12 , 57, 33 , 58, 4I use the intRank to determine which vehicle is bigger and intRankParent to make groups (so motorbikes and cars searched don't bring back the wrong type)I want to be able to search on License Type .But now a person can search for car 4 and motorcyle > 125. Then my query needs to find all the people with car type 4 AND Smaller (car-1-2-3-4) (that is group 4), and motorcycle and smaller (1 and 2) (Group 0)If a person search for car type Car 3 and motorcycle < 125, then car 1-2-3 and MotorCycle 1 must be returned.I hope it makes sense. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-10 : 07:56:41
|
One method is by using a temp tableINSERT INTO #TempSELECT * FROm tblLicenseTypeDef tWHERE @Description LIKE t.strDescription + '%'INSERT INTO #TempSELECT t.* FROm tblLicenseTypeDef tINNER JOIN #Temp tmpON tmp.intRankParent=t.intRankParentAND tmp.intRank<t.intRank select * from #temp will give all the results |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-10 : 08:00:56
|
[code]-- Prepare sample dataDECLARE @LicenseTypeDef TABLE ( intPkLicenseTypeID INT, strDescription VARCHAR(200), strLicenseCode VARCHAR(200), intRank INT, intRankParent INT )INSERT @LicenseTypeDefSELECT 1, 'MotorCycle < 125', 0, 1, 0 UNION ALLSELECT 2, 'MotorCycle > 125', 0, 2, 0 UNION ALLSELECT 3, 'Car 1' , 0, 0, 1 UNION ALLSELECT 4, 'Car 2' , 0, 1, 1 UNION ALLSELECT 5, 'Car 3' , 0, 2, 1 UNION ALLSELECT 6, 'Car 4' , 0, 3, 1DECLARE @CandidateLicense TABLE ( intPkCandidateLicenseID INT, intFkCandidateID INT, intFkLicenseTypeID INT )INSERT @CandidateLicenseSELECT 1, 57, 1 UNION ALLSELECT 2, 57, 3 UNION ALLSELECT 3, 58, 4 UNION ALLSELECT 4, 99, 2 UNION ALLSELECT 5, 99, 5-- Show the expected outputSELECT cl.intFkCandidateID, ltd.strDescriptionFROM @CandidateLicense AS clINNER JOIN @LicenseTypeDef AS x ON x.intPkLicenseTypeID = cl.intFkLicenseTypeIDINNER JOIN @LicenseTypeDef AS ltd ON ltd.intRankParent = x.intRankParent AND ltd.intRank >= x.intRankORDER BY cl.intFkCandidateID, x.intRankParent, x.intRank[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SDK
Starting Member
3 Posts |
Posted - 2008-06-10 : 08:16:24
|
| thanks for the quick replies guys. I will give it a whirl! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-10 : 08:20:03
|
Or this:-DECLARE @LicenceDef table(intPkLicenseTypeID Int,strDescription varchar(20),strLicenseCode int,intRank int,intRankParent int)INSERT INTO @LicenceDefSELECT 1 , 'MotorCycle < 125',0,1,0UNION ALLSELECT 2 , 'MotorCycle > 125',0,2,0UNION ALLSELECT 3 , 'Car 1' ,0,0,1UNION ALLSELECT 4 , 'Car 2' ,0,1,1UNION ALLSELECT 5 , 'Car 3' ,0,2,1UNION ALLSELECT 5 , 'Car 4' ,0,3,1DECLARE @Search varchar(8000)--your serach stringSET @Search='Car 3,MotorCycle > 125'--test value;With CTE (ID,Descn,Code,[Rank],Parent) AS(SELECT * FROM @LicenceDef WHERE ','+@Search+',' LIKE '%,' + strDescription + ',%'UNION ALLSELECT l.* FROM @LicenceDef lINNER JOIN CTE c on c.Parent=l.intRankParentAND c.[Rank]-1=l.intRank)select * from CTE ORDER BY IDID Descn Code Rank Parent----------- -------------------- ----------- ----------- -----------1 MotorCycle < 125 0 1 02 MotorCycle > 125 0 2 03 Car 1 0 0 14 Car 2 0 1 15 Car 3 0 2 1 |
 |
|
|
SDK
Starting Member
3 Posts |
Posted - 2008-06-10 : 09:33:40
|
| Peso..Just a quick questionI want to pass the intPkLicenseTypeID in (example 2,5 [MotorCycle > 125 and Car 3] , I want everyone that has (4,3) and (2,1) back. (Each intRankParent group and intRank less than what was passed in) Does it make sense? |
 |
|
|
|
|
|