473,394 Members | 1,893 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,394 software developers and data experts.

ordering problem

Hi everyone, I have a database (not designed by me) in sql server, I am
having trouble ordering it correctly, and I would appreciate any help.

Here is my structure, it's sort of hierarchical:

TABLE_A
ID_COL
SUB_ID_COL

TABLE_B
ID_COL'
NAME

This could be thought of as an employee and supervisor structure. We
have employees (TABLE_B) and their relationship (TABLE_A).

I know I need to use a group and oder by, but I am drawing blanks
today. I need the result to be (if using the employee example):

Bob
-Ryan
-Sue

Mark

Nick
-George
-Paul
--Ringo
--Star

Here is my half-hearted stab at it:

SELECT *
FROM TABLE_A A
INNER JOIN TABLE_B B
GROUP BY A.SUB_ID_COL
ORDER BY NAME;

Thank you very much for any help you can offer.

Have a great night!
Ryan

Jul 23 '05 #1
4 1054
Wow, my stab is way off. Hmm, this one is closer:

SELECT B1.NAME_COL, B2.NAME_COL
FROM TABLE_A A INNER JOIN
TABLE_B B1 ON A.ID_COL = B1.ID_COL INNER JOIN
TABLE_B B2 ON A.SUB_ID_COL = B2.ID_COL
GROUP BY A.SUB_ID_COL, A.ID_COL, B1.NAME_COL, B2.NAME_COL;

There must a be a better way of doing this, I just can't think right
now :)

Thanks again,
Ryan

Jul 23 '05 #2
ry*********@gmail.com wrote:
Wow, my stab is way off. Hmm, this one is closer:

SELECT B1.NAME_COL, B2.NAME_COL
FROM TABLE_A A INNER JOIN
TABLE_B B1 ON A.ID_COL = B1.ID_COL INNER JOIN
TABLE_B B2 ON A.SUB_ID_COL = B2.ID_COL
GROUP BY A.SUB_ID_COL, A.ID_COL, B1.NAME_COL, B2.NAME_COL;

There must a be a better way of doing this, I just can't think right
now :)


Problem is that you need some form of recursion. This can't be done with
standard SQL and CONNECT BY PRIOR exists just in Oracle.

The alternative (that works with SQL Server) is to store the tree
differently. Basically you do an in order tree traversal and store the
counts on node entry and exit. You get something like

create table tree (
no_left int,
no_right int,
node_id int
)

Insert and delete operations will have to properly update these. It's not
overly complicated and not very inefficient.

Kind regards

robert
Jul 23 '05 #3
Look up the nested sets model in this newsgroup or get a copy of my
book TREES & HIERARCHIES IN SQL. You can move teh parts of the tree
strucutre about as you wish and get any sort4ed order without recursion
or proprietary nightmares like CONNECT BY.

Jul 23 '05 #4
Thank you for the replys!

Have a great day!

Ryan

--CELKO-- wrote:
Look up the nested sets model in this newsgroup or get a copy of my
book TREES & HIERARCHIES IN SQL. You can move teh parts of the tree
strucutre about as you wish and get any sort4ed order without recursion
or proprietary nightmares like CONNECT BY.


Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ken Fine | last post by:
(originally posted to one of macromedia's groups; no help, so hopefully someone here can help me out. I'm using VBScript ASP.) When designing administrative interfaces to websites, we often need...
15
by: Kay Schluehr | last post by:
Here might be an interesting puzzle for people who like sorting algorithms ( and no I'm not a student anymore and the problem is not a students 'homework' but a particular question associated with...
2
by: masood.iqbal | last post by:
What is the standard C/C++ lexicograhic ordering of punctuation characters with respect to each other and the alphanumeric characters? --Masood
6
by: Brendan.Collins | last post by:
Hi I have a javascript problem that has been annoying me for two days now and thought that a javascript expert might have the magic solution. I am populating a table dynamically from the...
2
by: D. Dante Lorenso | last post by:
First I created a function that selected the next available pin code from a table of pre-defined pin codes: CREATE FUNCTION "public"."get_next_pin_code" () RETURNS varchar AS' DECLARE...
9
by: Project2501a | last post by:
hey guys, Question about the internal workings of Access. How are controls added in the Form.Controls collection? I mean, in which order? the order place them on the form? is there a way to...
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
23
by: illegal.prime | last post by:
Hi all, is there a key value collection in .Net that maintains the ordering in which I add items. I'm finding that I'm creating a number of classes that contain a name and then some object. I...
5
by: =?Utf-8?B?VG9t?= | last post by:
Cannot not seem to make any sense of the order that my key/values end up in when added to the Hashtable...ideally, I would like to be able to sort the keys/values...but not thinking it is possible....
4
ChrisWang
by: ChrisWang | last post by:
Dear all, I am reading the book "Core Python Programming". In the chapter talking about modules, it says the modules should follow this ordering: import Python Standard Library modules ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.