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 |
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-01-12 : 03:40:54
|
| Hi I have a table (column) which holds the where clause. i have a condition in which i need to select only the rows which does not have duplicate conditions or the highest scope. Ex- assumation that these condition apply to customers. 1. Country ='US'2. Country ='US' and State='california'3. Country ='US' and State='california' and City ='san francisco'4. Country ='India' 5. Country ='India' and State='AP' 6. Country ='India' and State='AP' and City ='Hyderabad'i need to fetch only rows 1 and 4. as the rest will be subset of the same dataRegardsKhalik======================================Ask to your self before u ask someone |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-12 : 04:14:53
|
| Hi,Please post ur table structure , sample data and expected output... |
 |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-01-12 : 04:18:33
|
| thanks raky, here are the detailsTable Name : ApplyCondition<b>ID Condition</B>1. Country ='US'2. Country ='US' and State='california'3. Country ='US' and State='california' and City ='san francisco'4. Country ='India' 5. Country ='India' and State='AP' 6. Country ='India' and State='AP' and City ='Hyderabad'expect output is rows 1 and 4 only.======================================Ask to your self before u ask someone |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-12 : 04:23:13
|
| select id, condition from applyconditionwhere condition in ( 'us' ,'India') |
 |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-01-12 : 04:26:49
|
| its not as simple as it is Dear..i have many such rows..i dont know which all country will be there.. if i select all customer from us then i need not apply other such condition like "State='california'" or "City ='san francisco'"as its will only fetch subset of the first conditions.======================================Ask to your self before u ask someone |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-12 : 04:33:09
|
quote: Originally posted by khalik its not as simple as it is Dear..i have many such rows..i dont know which all country will be there.. if i select all customer from us then i need not apply other such condition like "State='california'" or "City ='san francisco'"as its will only fetch subset of the first conditions.======================================Ask to your self before u ask someone
try thisselect id, conditionfrom applyconditionwhere ( condition not like '%state%' and condition not like '%city%') |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-12 : 04:40:18
|
| create table test20(id int,condition varchar(2000))insert into test20select 1, 'Country =US' union allselect 2, 'Country =US and State=california' union allselect 3, 'Country =US and State=california and City =san francisco' union allselect 4, 'Country =India' union allselect 5, 'Country =India and State=AP' union allselect 6, 'Country =India and State=AP and City =Hyderabad' select * from test20 where condition not like '%and%'May Be like this,,,i ThinkThanks,, |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-12 : 04:44:03
|
quote: Originally posted by ashishashish create table test20(id int,condition varchar(2000))insert into test20select 1, 'Country =US' union allselect 2, 'Country =US and State=california' union allselect 3, 'Country =US and State=california and City =san francisco' union allselect 4, 'Country =India' union allselect 5, 'Country =India and State=AP' union allselect 6, 'Country =India and State=AP and City =Hyderabad' select * from test20 where condition not like '%and%'May Be like this,,,i ThinkThanks,,
If the country name is Southerland or newzealand or phinland or england etc. They won't be returned..isn't it.? |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-12 : 04:47:29
|
| Yes Sorry i donn think about that,,,,wait for one more shot in few minutes ,,,,,,,,,n Thanks For Guiding,,,,,Me,,,,,,,,, |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-12 : 04:57:12
|
quote: Originally posted by raky
quote: Originally posted by ashishashish create table test20(id int,condition varchar(2000))insert into test20select 1, 'Country =US' union allselect 2, 'Country =US and State=california' union allselect 3, 'Country =US and State=california and City =san francisco' union allselect 4, 'Country =India' union allselect 5, 'Country =India and State=AP' union allselect 6, 'Country =India and State=AP and City =Hyderabad' select * from test20 where condition not like '%and%'May Be like this,,,i ThinkThanks,,
If the country name is Southerland or newzealand or phinland or england etc. They won't be returned..isn't it.?
May be Like This..........insert into test21select 1, 'Country =US' union allselect 2, 'Country =US and State=california' union allselect 3, 'Country =US and State=california and City =san francisco' union allselect 4, 'Country =India' union allselect 5, 'Country =India and State=AP' union allselect 6, 'Country =India and State=AP and City =Hyderabad' union allselect 7, 'country=newzealand' union allselect 8, 'country=finland'select * from test21 where condition not like '% and %'Test it If there is any error comes up then lemme know,,,,Thanks,,,, |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-12 : 05:07:29
|
| This is the better solution which i had found. try thisdeclare @test table ( id int, condition varchar(100))insert into @test select 1, 'Country =''US''' union allselect 2, 'Country =''US'' and State=''california''' union allselect 3, 'Country =''US'' and State=''california'' and City =''san francisco''' union allselect 4, 'Country =''India''' union allselect 5, 'Country =''India'' and State=''AP''' union allselect 6, 'Country =''India'' and State=''AP'' and City =''Hyderabad''' select id, condition from @testwhere datalength(condition) -datalength(replace(condition,'=','')) = 1 |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-12 : 05:11:24
|
quote: Originally posted by ashishashish
quote: Originally posted by raky
quote: Originally posted by ashishashish create table test20(id int,condition varchar(2000))insert into test20select 1, 'Country =US' union allselect 2, 'Country =US and State=california' union allselect 3, 'Country =US and State=california and City =san francisco' union allselect 4, 'Country =India' union allselect 5, 'Country =India and State=AP' union allselect 6, 'Country =India and State=AP and City =Hyderabad' select * from test20 where condition not like '%and%'May Be like this,,,i ThinkThanks,,
If the country name is Southerland or newzealand or phinland or england etc. They won't be returned..isn't it.?
May be Like This..........insert into test21select 1, 'Country =US' union allselect 2, 'Country =US and State=california' union allselect 3, 'Country =US and State=california and City =san francisco' union allselect 4, 'Country =India' union allselect 5, 'Country =India and State=AP' union allselect 6, 'Country =India and State=AP and City =Hyderabad' union allselect 7, 'country=newzealand' union allselect 8, 'country=finland'select * from test21 where condition not like '% and %'Test it If there is any error comes up then lemme know,,,,Thanks,,,,
I think Your solution is ok to the maximum extent but it depends on the data. |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-12 : 05:12:30
|
| yea this One is More Reliable,,,,,,,,Thanks For the Solution... |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-12 : 05:16:21
|
| Welcome... |
 |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-01-12 : 05:28:15
|
| thanks ashishashish and rakyyou guy got the solution to the problem, let me add some thign to it.i may have a case where only the subset is avilable. 7. country 'AE' and state ='Dubai' (not sure abt other country...state)in this case its only with combination country and state.. so it should return it..consider the follow generic case also.1 Country =<@country> 2. Country =<@country> and State= <@state>3. Country =<@country> and State= <@state> and City =<@city>for US & India .. i have 1,2,3for UAE we have only 2for singapore we have 3in general if 1 is fetch then 2,3 shd not fetch.if 2 is fetch then 3 shd not be fetch. if 1,2 is not fetch then fetch 3RegardsKhalik======================================Ask to your self before u ask someone |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-12 : 05:32:34
|
| Donn get U sry for this.....Can U please..Clear it in more efficient way for us.Thanks,,, |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-12 : 06:42:04
|
quote: Originally posted by khalik thanks ashishashish and rakyyou guy got the solution to the problem, let me add some thign to it.i may have a case where only the subset is avilable. 7. country 'AE' and state ='Dubai' (not sure abt other country...state)in this case its only with combination country and state.. so it should return it..consider the follow generic case also.1 Country =<@country> 2. Country =<@country> and State= <@state>3. Country =<@country> and State= <@state> and City =<@city>for US & India .. i have 1,2,3for UAE we have only 2for singapore we have 3in general if 1 is fetch then 2,3 shd not fetch.if 2 is fetch then 3 shd not be fetch. if 1,2 is not fetch then fetch 3RegardsKhalik======================================Ask to your self before u ask someone
try thiscreate table #temp ( id int, condition varchar(1000),country varchar(100), priority int )insert into #temp ( id, condition ,country )select id,condition, substring(condition,charindex('''',condition)+1,charindex('''',condition,charindex('''',condition)+1)-charindex('''',condition)-1)from yourtable update tset t.priority = datalength(s.condition) -len(replace(s.condition,'=',''))from yourtable sinner join #temp t on t.id = s.idselect id,condition from ( select row_number() over ( partition by country order by priority ) as sno, id,condition from #temp ) t where t.sno = 1 order by t.id |
 |
|
|
khalik
Constraint Violating Yak Guru
443 Posts |
Posted - 2009-01-12 : 07:15:05
|
| Hi Rakycan you please explain this part of ur solution. in words. what will be output of substring..select id,condition, substring(condition,charindex('''',condition)+1,charindex('''',condition,charindex('''',condition)+1)-charindex('''',condition)-1)from yourtable ======================================Ask to your self before u ask someone |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-12 : 07:22:34
|
| declare @test table ( id int, condition varchar(100))insert into @test select 1, 'Country =''US''' union allselect 2, 'Country =''US'' and State=''california''' union allselect 3, 'Country =''US'' and State=''california'' and City =''san francisco''' union allselect 4, 'Country =''India''' union allselect 5, 'Country =''India'' and State=''AP''' union allselect 6, 'Country =''India'' and State=''AP'' and City =''Hyderabad''' union allselect 7, 'Country =''Australia'' and State=''Melbourne''' union all select 8, 'Country =''Australia'' and State=''AP'' and City =''zzz''' union allselect 9, 'Country =''New Zealand'' and State=''zzz'' and City =''zzz''' select t.id,t.condition from (select id,condition,row_number() over(partition by left(condition,case when patindex('% and%',condition) = 0 then len(condition) else patindex('% and%',condition)-1 end) order by id) as rowno from @test) twhere rowno = 1 order by idJai Krishna |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-01-12 : 07:30:21
|
quote: Originally posted by Jai Krishna declare @test table ( id int, condition varchar(100))insert into @test select 1, 'Country =''US''' union allselect 2, 'Country =''US'' and State=''california''' union allselect 3, 'Country =''US'' and State=''california'' and City =''san francisco''' union allselect 4, 'Country =''India''' union allselect 5, 'Country =''India'' and State=''AP''' union allselect 6, 'Country =''India'' and State=''AP'' and City =''Hyderabad''' union allselect 7, 'Country =''Australia'' and State=''Melbourne''' union all select 8, 'Country =''Australia'' and State=''AP'' and City =''zzz''' union allselect 9, 'Country =''New Zealand'' and State=''zzz'' and City =''zzz''' select t.id,t.condition from (select id,condition,row_number() over(partition by left(condition,case when patindex('% and%',condition) = 0 then len(condition) else patindex('% and%',condition)-1 end) order by id) as rowno from @test) twhere rowno = 1 order by idJai Krishna
I found that one interesting and a nice approach,,,,,because,,,today i learn one more thing in Sql.Thanks To u,,,,,,Jai Krishna |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-12 : 07:31:41
|
quote: Originally posted by khalik Hi Rakycan you please explain this part of ur solution. in words. what will be output of substring..select id,condition, substring(condition,charindex('''',condition)+1,charindex('''',condition,charindex('''',condition)+1)-charindex('''',condition)-1)from yourtable ======================================Ask to your self before u ask someone
Hi,it directly selects id,condition columns from yourtable and 3rd select stmt returns the string between first encountered ''( single quotes ) set in your condition column... |
 |
|
|
Next Page
|
|
|
|
|