473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Select Columns using nested select statement.

1
Hi all,

I am populating data from a table which contain parent id which reference to itself
E.g.: TableA - ID, Name, Desc, ParentID
ParentID reference to ID

I would need the result to return:
ID, Name, Desc, ParentID, ParentName, ParentDesc

Select statement:
SELECT ID, Name, Desc, ParentID,
(SELECT Name from TableA WHERE ID=ParentID) AS ParentName,
(SELECT Desc from TableA WHERE ID=ParentID) AS ParentDesc
FROM TableA;

Is there any performance issue using this approach?
Is there a better way of doing this?
Jan 21 '10 #1
3 9348
code green
1,726 Expert 1GB
It looks a little unconventional, and the sub-query would throw an error if there was more than one ID returned. What about
Expand|Select|Wrap|Line Numbers
  1. SELECT child.ID, child.Name, child.Desc, child.ParentID, 
  2. parent.Name AS ParentName,
  3. parent.Desc AS ParentDesc
  4. FROM TableA child
  5. JOIN TableA parent ON child.ID = parent.ParentID;
  6.  
Pleas adjust to correct parent-child field required
Jan 21 '10 #2
ck9663
2,878 Expert 2GB
Try using a recursive CTE instead.

Happy Coding!!!

~~ CK
Jan 21 '10 #3
nbiswas
149 100+
Recursive CTE

Simple Example of Recursive CTE
Mar 24 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
15
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to...
10
by: AC Slater | last post by:
I have 1 table (out of many) that has very poor performance when performing a select into on. The select statement is called multiple times. We've found each call to take almost 1 second... we...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
23
by: Brian | last post by:
Hello All - I am wondering if anyone has any thoughts on which is better from a performance perspective: a nested Select statement or an Inner Join. For example, I could do either of the...
29
by: pb648174 | last post by:
I have the following basic statements being executed: Create a temp table, #TempPaging Insert Into #TempPaging (Col1, Col2) Select Col1, Col2 From SomeOtherTable Order By Col2, Col1 Select...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
4
by: Matt Ratliff | last post by:
Hello, I would appreciate any assistance you have with the following problem: I have (as an example) an array of values as follows: arrayvalues=new Array("0001","0003","0005") where each is the...
6
by: Jon Bilbao | last post by:
I´m trying a select clause in two steps because it´s too complex. First: SELECT Reference, Results.idEnsayo, Results.Num_taladro, min(Results.dTime) + 500 AS tIni, max(Results.dTime) - 500 AS...
9
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.