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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Identify the rows..

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 data

Regards
Khalik


======================================
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...
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-01-12 : 04:18:33
thanks raky, here are the details

Table 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
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-12 : 04:23:13
select id, condition
from applycondition
where condition in ( 'us' ,'India')
Go to Top of Page

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
Go to Top of Page

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 this

select id, condition
from applycondition
where ( condition not like '%state%' and condition not like '%city%')
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-01-12 : 04:40:18
create table test20(id int,condition varchar(2000))

insert into test20
select 1, 'Country =US' union all
select 2, 'Country =US and State=california' union all
select 3, 'Country =US and State=california and City =san francisco' union all
select 4, 'Country =India' union all
select 5, 'Country =India and State=AP' union all
select 6, 'Country =India and State=AP and City =Hyderabad'

select * from test20 where condition not like '%and%'

May Be like this,,,i Think

Thanks,,
Go to Top of Page

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 test20
select 1, 'Country =US' union all
select 2, 'Country =US and State=california' union all
select 3, 'Country =US and State=california and City =san francisco' union all
select 4, 'Country =India' union all
select 5, 'Country =India and State=AP' union all
select 6, 'Country =India and State=AP and City =Hyderabad'

select * from test20 where condition not like '%and%'

May Be like this,,,i Think

Thanks,,



If the country name is Southerland or newzealand or phinland or england etc. They won't be returned..isn't it.?
Go to Top of Page

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,,,,,,,,,
Go to Top of Page

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 test20
select 1, 'Country =US' union all
select 2, 'Country =US and State=california' union all
select 3, 'Country =US and State=california and City =san francisco' union all
select 4, 'Country =India' union all
select 5, 'Country =India and State=AP' union all
select 6, 'Country =India and State=AP and City =Hyderabad'

select * from test20 where condition not like '%and%'

May Be like this,,,i Think

Thanks,,



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 test21
select 1, 'Country =US' union all
select 2, 'Country =US and State=california' union all
select 3, 'Country =US and State=california and City =san francisco' union all
select 4, 'Country =India' union all
select 5, 'Country =India and State=AP' union all
select 6, 'Country =India and State=AP and City =Hyderabad' union all
select 7, 'country=newzealand' union all
select 8, 'country=finland'
select * from test21 where condition not like '% and %'

Test it If there is any error comes up then lemme know,,,,

Thanks,,,,

Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-12 : 05:07:29
This is the better solution which i had found. try this

declare @test table ( id int, condition varchar(100))
insert into @test
select 1, 'Country =''US''' union all
select 2, 'Country =''US'' and State=''california''' union all
select 3, 'Country =''US'' and State=''california'' and City =''san francisco''' union all
select 4, 'Country =''India''' union all
select 5, 'Country =''India'' and State=''AP''' union all
select 6, 'Country =''India'' and State=''AP'' and City =''Hyderabad'''



select id, condition
from @test
where datalength(condition) -datalength(replace(condition,'=','')) = 1
Go to Top of Page

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 test20
select 1, 'Country =US' union all
select 2, 'Country =US and State=california' union all
select 3, 'Country =US and State=california and City =san francisco' union all
select 4, 'Country =India' union all
select 5, 'Country =India and State=AP' union all
select 6, 'Country =India and State=AP and City =Hyderabad'

select * from test20 where condition not like '%and%'

May Be like this,,,i Think

Thanks,,



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 test21
select 1, 'Country =US' union all
select 2, 'Country =US and State=california' union all
select 3, 'Country =US and State=california and City =san francisco' union all
select 4, 'Country =India' union all
select 5, 'Country =India and State=AP' union all
select 6, 'Country =India and State=AP and City =Hyderabad' union all
select 7, 'country=newzealand' union all
select 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.
Go to Top of Page

ashishashish
Constraint Violating Yak Guru

408 Posts

Posted - 2009-01-12 : 05:12:30
yea this One is More Reliable,,,,,,,,
Thanks For the Solution...
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-12 : 05:16:21
Welcome...
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-01-12 : 05:28:15
thanks ashishashish and raky

you 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,3
for UAE we have only 2
for singapore we have 3

in 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 3

Regards
Khalik

======================================
Ask to your self before u ask someone
Go to Top of Page

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,,,
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-12 : 06:42:04
quote:
Originally posted by khalik

thanks ashishashish and raky

you 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,3
for UAE we have only 2
for singapore we have 3

in 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 3

Regards
Khalik

======================================
Ask to your self before u ask someone



try this

create 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 t
set t.priority = datalength(s.condition) -len(replace(s.condition,'=',''))
from yourtable s
inner join #temp t on t.id = s.id

select 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
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-01-12 : 07:15:05
Hi Raky

can 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
Go to Top of Page

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 all
select 2, 'Country =''US'' and State=''california''' union all
select 3, 'Country =''US'' and State=''california'' and City =''san francisco''' union all
select 4, 'Country =''India''' union all
select 5, 'Country =''India'' and State=''AP''' union all
select 6, 'Country =''India'' and State=''AP'' and City =''Hyderabad''' union all
select 7, 'Country =''Australia'' and State=''Melbourne''' union all
select 8, 'Country =''Australia'' and State=''AP'' and City =''zzz''' union all
select 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) t
where rowno = 1
order by id

Jai Krishna
Go to Top of Page

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 all
select 2, 'Country =''US'' and State=''california''' union all
select 3, 'Country =''US'' and State=''california'' and City =''san francisco''' union all
select 4, 'Country =''India''' union all
select 5, 'Country =''India'' and State=''AP''' union all
select 6, 'Country =''India'' and State=''AP'' and City =''Hyderabad''' union all
select 7, 'Country =''Australia'' and State=''Melbourne''' union all
select 8, 'Country =''Australia'' and State=''AP'' and City =''zzz''' union all
select 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) t
where rowno = 1
order by id

Jai Krishna



I found that one interesting and a nice approach,,,,,

because,,,today i learn one more thing in Sql.
Thanks To u,,,,,,
Jai Krishna
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-12 : 07:31:41
quote:
Originally posted by khalik

Hi Raky

can 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...
Go to Top of Page
    Next Page

- Advertisement -