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 |
lakeoffm
Starting Member
2 Posts |
Posted - 2012-09-21 : 06:46:26
|
Hi! I have a problem building an SQL script to select from a db.The data is hyerarcical and is stored in one table. Generaly, we use java to operate a select like this, but here he need to use just the SQL.So, here is the table:number branch code1000 center XXCO1001 xyz XXER1002 zyx XXRE2000 center2 YYCO2001 xyz YYER2002 zyx YYREAll the items that have "CO" in the end of their code are parents to the items that have the same first two symbols in the code.The task is to get a list of all the branches with their respective parents, like this:center xyz XXERcenter zyx XXREcenter2 xyz YYERcenter2 zyx YYRECan anybody please give a hand? |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-09-21 : 07:05:42
|
[code]CREATE TABLE #tmp(n INT, branch VARCHAR(32), code VARCHAR(32));insert into #tmp values ('1000','center','XXCO');insert into #tmp values ('1001','xyz','XXER');insert into #tmp values ('1002','zyx','XXRE');insert into #tmp values ('2000','center2','YYCO');insert into #tmp values ('2001','xyz','YYER');insert into #tmp values ('2002','zyx','YYRE');SELECT a.branch, b.branch, a.CODEFROM #tmp a INNER JOIN #tmp b ON LEFT(a.code,2) = LEFT(b.code,2) AND a.code NOT LIKE '%CO' AND b.code LIKE '%CO';DROP TABLE #tmp;[/code] |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-09-21 : 07:13:42
|
[code]declare @sample table(number int, branch varchar(30), code varchar(30))insert @sampleselect 1000, 'center', 'XXCO' union allselect 1001, 'xyz', 'XXER' union allselect 1002, 'zyx', 'XXRE' union allselect 2000, 'center2', 'YYCO' union allselect 2001, 'xyz', 'YYER' union allselect 2002, 'zyx', 'YYRE' --All the items that have "CO" in the end of their code are parents to the items that have the same first two symbols in the code.--The task is to get a list of all the branches with their respective parents, like this:--center xyz XXER--center zyx XXRE--center2 xyz YYER--center2 zyx YYREselect * from @sampleselect t1.branch,t2.branch,t2.codefrom @sample t1join @sample t2 on left(t2.code,2) = left(t1.code,2) and right(t2.code,2) <> 'CO'where right(t1.code,2) = 'CO'[/code] Too old to Rock'n'Roll too young to die. |
|
|
lakeoffm
Starting Member
2 Posts |
Posted - 2012-09-21 : 07:25:39
|
I mplemented the code, but it gives me an "ORA-00904 Invalid Identifier" error, pointing al the position between the equals sign and the second left..SELECT b.branch, a.branch, a.codeFROM tmp a JOIN tmp b ONleft(a.code,2) = left(b.code,2)AND a.code NOT LIKE '%CO' AND b.code LIKE '%CO'; |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-09-21 : 07:34:26
|
The code both Fred and I posted are for SQL Server. This forum is a Microsoft SQL Server forum, so there may be very few people who can answer Oracle questions, if any at all.You would get better and faster responses at another forum such as dbforums.com. |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-09-21 : 07:35:52
|
quote: Originally posted by webfred Too old to Rock'n'Roll too young to die.
Today I am doing a very good job of annoying you, aren't I? |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-09-21 : 08:31:46
|
quote: Originally posted by sunitabeck
quote: Originally posted by webfred Too old to Rock'n'Roll too young to die.
Today I am doing a very good job of annoying you, aren't I?
You are trying hard but you can't Too old to Rock'n'Roll too young to die. |
|
|
|
|
|
|
|