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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 VERY VERY URGENT - Intersect

Author  Topic 

Vatsan
Starting Member

7 Posts

Posted - 2006-01-30 : 03:20:30
VERY URGENT!!!


There is a table with the following columns

network_group_id | segment_id | age_key | gender_key | count |
---------------------------------------------------------------
0 1 1 1 10
0 2 1 2 12
1 1 2 2 20
and so on...

Now i have to find the segment_id, age_key, gender_key combination that is in network_group_id = 0 and not in network_group_id = 1
and vice versa... and insert the same with zero as dummy values...

Is there any operator to perform an intersect/minus kind of function.

Please excuse me if this looks kiddish to u experts... but i need a solution ASAP...


elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2006-01-30 : 03:28:12
Hello Vatsan, welcome to SQL Team. If you give an example of the resutlts you expect I'm sure someone will be able to help


steve

-----------

Facts are meaningless. You could use facts to prove anything that's even remotely true!
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2006-01-30 : 03:32:44
see if this helps..
to fetch records which exist for group_id 0 and not in 1
select network_group_id,0,0,0,0 from table where segment_id+age_key+gender_key not in (select segment_id+age_key+gender_key from table where network_group_id =1)
where network_group_id =0
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2006-01-30 : 03:44:30
oops
..
there was an error
try this..
select * from [tablename] where cast(segment_id as varchar)+ cast(age_key as varchar)+cast( gender_key as varchar)not in (select cast(segment_id as varchar)+ cast (age_key as varchar)+cast(gender_key as varchar) from [tablename] where network_group_id =1)
and network_group_id = 0
Go to Top of Page

Vatsan
Starting Member

7 Posts

Posted - 2006-01-30 : 06:14:55
Thanks Guys...

Hi gupta,

It was gr8... u made it work... hats off 2 u...

I tried a diff method... please look into the query i used & tell me y i should not use this. The query u sent runs a bit faster. but when i view the execution plan... it takes a lot more steps & cost than what i have below... It would be gr8 if u cld explain me when & where shld i use a join.

Thanks 4 u help guys... The code is working fine...


select network_group_id, segment_id, age_key, gender_key, 0,0,0,0,0,0,0,0,0,0,0,0 from
(select network_group_id,segment_id,age_key,gender_key,T1.segment_id as seg_id from
(select distinct network_group_id,segment_id,age_key,gender_key from seg_norms
where network_group_id = 1)T0 left outer join
(select distinct network_group_id,segment_id,age_key,gender_key from seg_norms
where network_group_id = 0)T1
on T0.segment_id = T1.segment_id
and T0.age_key = T1.age_key
and T0.gender_key = T1.gender_key )T2
where T2.seg_id is null
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-01-30 : 09:33:42
quote:
Originally posted by Vatsan

It was gr8... u made it work... hats off 2 u...




oye....inter-speak



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page
   

- Advertisement -