| Author |
Topic |
|
raju2647
Starting Member
22 Posts |
Posted - 2004-09-01 : 00:14:34
|
| hi frdswould anyone please tell me out to get output from two tables listed below.the contents of the table are given below.name location id step1 london 12 step1 france 12 step2 newyork 14 step3 france 15 table 2name location id step1 london 102 step1 norway 102 step2 newyork 104 step3 france 105 i want to get the output asstep2& step 3please help |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-09-01 : 00:24:50
|
| what do you mean by step2& step3?mk_garg |
 |
|
|
raju2647
Starting Member
22 Posts |
Posted - 2004-09-01 : 01:06:41
|
quote: Originally posted by mk_garg20 what do you mean by step2& step3?mk_garg
haitake the table 1 as new and the other as oldname==system nameso system can have any location with same id or different id.if both tables contain a system with same location & count of location for an id same as count of location for id in the other table then it should take the system name and id from new tablewaiting 4 ur reply |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-09-01 : 01:43:32
|
| you mean final result will be step1 london 12 step2 newyork 14 step3 france 15 right?mk_garg |
 |
|
|
raju2647
Starting Member
22 Posts |
Posted - 2004-09-01 : 01:52:03
|
quote: Originally posted by mk_garg20 you mean final result will be step1 london 12 step2 newyork 14 step3 france 15 right?mk_garg
NO london wont come as the count of id from both sides for step1 would be same but the locations are different so it will not come..only step 2 ,step 3 will comewaiting for reply |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-09-01 : 01:52:42
|
| check this outcreate table #table1(name varchar(10), location varchar(20), id int)insert into #table1select 'step1','london',12 union allselect 'step1','france',12 union allselect 'step2','newyork',14 union allselect 'step3','france',15 create table #table2(name varchar(10), location varchar(20), id int)insert into #table2select 'step1','london',102 union allselect 'step1','norway',102 union allselect 'step2','newyork',104 union allselect 'step3','france',105 select #table1.name,#table1.location,#table1.idfrom #table1 inner join #table2 on (#table1.name=#table2.name and #table1.location=#table2.location)drop table #table1drop table #table2mk_garg |
 |
|
|
raju2647
Starting Member
22 Posts |
Posted - 2004-09-01 : 02:28:51
|
quote: Originally posted by mk_garg20 check this outcreate table #table1(name varchar(10), location varchar(20), id int)insert into #table1select 'step1','london',12 union allselect 'step1','france',12 union allselect 'step2','newyork',14 union allselect 'step3','france',15 create table #table2(name varchar(10), location varchar(20), id int)insert into #table2select 'step1','london',102 union allselect 'step1','norway',102 union allselect 'step2','newyork',104 union allselect 'step3','france',105 select #table1.name,#table1.location,#table1.idfrom #table1 inner join #table2 on (#table1.name=#table2.name and #table1.location=#table2.location)drop table #table1drop table #table2mk_garg
hibut london will still come the problem is how to remove london from the list.should i use cursors for doing that please helpwaiting for ur replyregards raju |
 |
|
|
raju2647
Starting Member
22 Posts |
Posted - 2004-09-01 : 02:36:32
|
| from the table u can get the o/p for the table variable and i have written a script with cursors .i am not much familiar with cursors .------declare @new_count table(sys_name varchar(50),sys_loc varchar(50),sys_id int, sys_count int)insert @new_count select name1,location,id,count(location) from @new group by name1,id,locationselect * from @new_count declare @old_count table(sys_name varchar(50),sys_loc varchar(50),sys_id int, sys_count int)insert @old_count select name1,location,id,count(location) from @old group by name1,id,locationselect * from @old_countdeclare @mig table(name varchar(50),id int)declare @row1 varchar(100),@row2 varchar(100),@row3 int,@row4 int,@acc1 varchar(100),@acc2 varchar(100),@o_loc varchar(100),@o_cnt intdeclare @cnt intset @cnt=0declare new_cursor CURSORoptimisticforselect sys_name,sys_loc,sys_id,sys_count from @new_count open new_cursorfetch next from new_cursor into @row1,@row2,@row3,@row4while @@fetch_status=0begindeclare old_cursor CURSORoptimisticforselect sys_loc,sys_count from @old_count where sys_name=@row1open old_cursor fetch next from old_cursor into @o_loc,@o_cntwhile @@fetch_status=0begin if @o_cnt=@row4 begin while (@row2) is not null begin if @o_loc=@row2 begin set @cnt=@cnt+1print @cnt if @cnt=@o_cnt begin insert into @Mig values(@row1,@row3) end end fetch next from new_cursor into @row2 end end else begin break end fetch next from old_cursor into @o_locendclose old_cursordeallocate old_cursorfetch next from new_cursor into @row3endclose new_cursordeallocate new_cursor------ |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-09-01 : 02:39:00
|
| what are you doing here?If you are creating new tables then you should define some Primary keys in the tables.mk_garg |
 |
|
|
raju2647
Starting Member
22 Posts |
Posted - 2004-09-01 : 03:05:40
|
| hithis are two tables from different DB.during migration data in new was corrupted ie unwanted data.so i am trying to get the orginal migrated data for updation.this query is only for executing once.ie i am using table variable.could u please c weather the script is correct |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-09-01 : 18:18:06
|
| I have not worked alot on cursors. I will see if can help you. otherwise someone else will help you here.mk_garg |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-09-01 : 18:57:28
|
| I am trying to wrap my head around what this confusing thread is about...So you are saying that you want to query on these two tables but not include london...Select * From table1 as AInner Join table2 as BOn A.name = B.nameand A.location = B.locationWhere location <> 'London'If this isn't it, try posting some sample data as it would be before your query ... and the desired result. Then I am sure we can straighten this out. No need for cursors that I see yet.Corey |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-09-02 : 09:54:39
|
quote: I am trying to wrap my head around what this confusing thread is about...
LOL -- the most frustrating thing about these forums is we spend more time trying to figure out the QUESTION as opposed to the ANSWER.- Jeff |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2004-09-02 : 10:24:03
|
I think he wants all the steps where the locations for those steps are the same. Maybe -------Moo. :) |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-09-02 : 11:04:28
|
| In other words: determine which steps have only one location.Select * from table1 as t1Inner Join (Select name, location From table1 Group By name, location having count(*)=1) as Aon t1.name = a.nameand t1.location = a.locationbut i still don't get what this has to do with table2... [sigh]I give up until the question is phrased clearly...Corey |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-02 : 11:06:55
|
well after turning my ESP server on i'm thinking this is what he wants... based on mr_mist's previous solution:select t1.name, t1.location, t1.idfrom #table1 t1 inner join #table2 t2 on (t1.name=t2.name and t1.location=t2.location)where t1.name not in(select distinct t3.namefrom #table1 t3 inner join #table2 t4 on (t3.name=t4.name)where t3.location <> t4.location) Go with the flow & have fun! Else fight the flow :) |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-09-02 : 18:21:28
|
| After looking your responses, i am sure he got answer to his question.mk_garg |
 |
|
|
raju2647
Starting Member
22 Posts |
Posted - 2004-09-10 : 03:15:36
|
| thanks for ur help i got the o/p without using cursors |
 |
|
|
|