SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


problem in merging 2 sql queries into a single query to distinguish old and new.plz help!!


problem in merging 2 sql queries into a single query to distinguish old and new.plz help!!

Author
Message
suvo.kundu
suvo.kundu
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 80
in my table i have the records like

Id Record1 Record2 Level gid
1 testRec1 testRec2 0 1
2 testRec11 testRec22 0 2
3 testRec111 testRec222 0 3
4 testRec111 testRec2222 0 4
5 newtestRec newtestRec2 1 1

I want to show records like
Id OldRecord1 NewRecord1 OldRecord2 NewRecord2
1 testRec1 newtestRec1 testRec2 newtestRec2
2 testRec11 NoNewRecord testRec22 NoNewRecord
3 testRec22 NoNewRecord testRec33 NoNewRecord
4 testRec33 NoNewRecord testRec33 NoNewRecord

actually i have 2 querys
this is for old record
select * from tablename where level=0
this is for New record
select * from tablename where level=1

I want to merge this into a single record set as old and new..plz help
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32160 Visits: 18551
suvo.kundu (1/12/2013)
in my table i have the records like

Id Record1 Record2 Level gid
1 testRec1 testRec2 0 1
2 testRec11 testRec22 0 2
3 testRec111 testRec222 0 3
4 testRec111 testRec2222 0 4
5 newtestRec newtestRec2 1 1

I want to show records like
Id OldRecord1 NewRecord1 OldRecord2 NewRecord2
1 testRec1 newtestRec1 testRec2 newtestRec2
2 testRec11 NoNewRecord testRec22 NoNewRecord
3 testRec22 NoNewRecord testRec33 NoNewRecord
4 testRec33 NoNewRecord testRec33 NoNewRecord

actually i have 2 querys
this is for old record
select * from tablename where level=0
this is for New record
select * from tablename where level=1

I want to merge this into a single record set as old and new..plz help



SELECT *
FROM tablename old
INNER JOIN tablename new
ON old.id = new.id
WHERE old.LEVEL = 0
AND new.LEVEL = 1;





Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

suvo.kundu
suvo.kundu
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 80
ya i could get my solution as
select
ZZ.gid,
ZZ.Record1 as 'OldRecord1',
CASE WHEN XX.Record1 IS NULL THEN 'No New Record' ELSE XX.Record1 END as 'NewRecord1' ,
ZZ.Record2 as 'OldRecord2',
CASE WHEN XX.Record2 IS NULL THEN 'No New Record' ELSE XX.Record2 END as 'NewRecord2'
--XX.Record1 as 'NewRecord1',
--XX.Record2 as 'NewRecord2'
from
(
SELECT
ROW_NUMBER ( ) OVER ( order by id )as RN,gid,Record1,Record2 from tblName
where Level=0
)ZZ left outer join
(
SELECT
ROW_NUMBER ( ) OVER ( order by id )as RNN,
Record1,
Record2
from tblName
where Level=1
)XX on xx.rnn=zz.rn
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32160 Visits: 18551
Good to hear



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search