473,397 Members | 2,099 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,397 software developers and data experts.

Hierarchy

Hello!

I have a table that looks like this:

Col1; Col2; Col3; Col4; Col5
38; 75; 233; 916; 2770
38; 75; 233; 916; 2771
38; 75; 233; 916; 2772
38; 75; 233; 923; 2654
38; 75; 233; 923; 2655
38; 75; 245; 913; 2454
38; 75; 245; 913; 2456
....

And I need a query (not a procedure) that shows me this:
38; NULL; NULL; NULL; NULL
NULL; 75; NULL; NULL; NULL
NULL; NULL; 233; NULL; NULL
NULL; NULL; NULL; 916; NULL
NULL; NULL; NULL; NULL; 2770
NULL; NULL; NULL; NULL; 2771
NULL; NULL; NULL; NULL; 2772
NULL; NULL; NULL; 923; NULL
NULL; NULL; NULL; NULL; 2654
NULL; NULL; NULL; NULL; 2655
NULL; NULL; 245; NULL; NULL
NULL; NULL; NULL; 913; NULL
NULL; NULL; NULL; NULL; 2454
NULL; NULL; NULL; NULL; 2456
....

Does anybody know how i can get this result? How?

Help! Thank you!
SQLNull

ps: SQL-Server 2000

Jul 10 '06 #1
3 1414
SQLNull (ra******@yahoo.com) writes:
And I need a query (not a procedure) that shows me this:
38; NULL; NULL; NULL; NULL
NULL; 75; NULL; NULL; NULL
NULL; NULL; 233; NULL; NULL
NULL; NULL; NULL; 916; NULL
NULL; NULL; NULL; NULL; 2770
NULL; NULL; NULL; NULL; 2771
NULL; NULL; NULL; NULL; 2772
NULL; NULL; NULL; 923; NULL
NULL; NULL; NULL; NULL; 2654
NULL; NULL; NULL; NULL; 2655
NULL; NULL; 245; NULL; NULL
NULL; NULL; NULL; 913; NULL
NULL; NULL; NULL; NULL; 2454
NULL; NULL; NULL; NULL; 2456
...

Does anybody know how i can get this result? How?
...
ps: SQL-Server 2000
You are going to regret this...

The query is below, and I encourage you to study it closely to see what
is going on. The query makes use of derived tables - a derived table is
a temp table within the query so to speak, but not necessarily
materialsed. All SELECTs are derived tables, execpt the two SELECT
COUNT - they are correlated subqueries.

Had you been on SQL 2005, it would have been possible to write the
query more compactly with help of a CTE - Common Table Expression.
Also the row_number() function would have come in handy.

The keystr that appears in the query is a simplifcation that I could
permit myself, when all columns where numeric. It may not work well,
if your actual table have different data types. But they query could
be written without keystr. (Which is left as an exercise to the reader.)

Performance is not likely to be good.

CREATE TABLE h (col1 int NOT NULL,
col2 int NOT NULL,
col3 int NOT NULL,
col4 int NOT NULL,
col5 int NOT NULL,
PRIMARY KEY (col1, col2, col3, col4, col5))
go
INSERT h (col1, col2, col3, col4, col5)
EXEC ('SELECT 38, 75, 233, 916, 2770
SELECT 38, 75, 233, 916, 2771
SELECT 38, 75, 233, 916, 2772
SELECT 38, 75, 233, 923, 2654
SELECT 38, 75, 233, 923, 2655
SELECT 38, 75, 245, 913, 2454
SELECT 38, 75, 245, 913, 2456')
go
SELECT keystr, col1, col2, col3, col4, col5
FROM (SELECT a.keystr,
col1 = CASE WHEN a.col1 <b.col1 OR b.col1 IS NULL
THEN a.col1
END,
col2 = CASE WHEN a.col2 <b.col2 OR b.col2 IS NULL
THEN a.col2
END,
col3 = CASE WHEN a.col3 <b.col3 OR b.col3 IS NULL
THEN a.col3
END,
col4 = CASE WHEN a.col4 <b.col4 OR b.col4 IS NULL
THEN a.col4
END,
col5 = CASE WHEN a.col5 <b.col5 OR b.col5 IS NULL
THEN a.col5
END
FROM (SELECT keystr,
rowno = (SELECT COUNT(*)
FROM h AS h1
WHERE str(h1.col1) + str(h1.col2) +
str(h1.col3) + str(h1.col4) +
str(h1.col5) <=
str(h.col1) + str(h.col2) +
str(h.col3) + str(h.col4) +
str(h.col5)),
col1 = CASE n WHEN 1 THEN h.col1 END,
col2 = CASE n WHEN 2 THEN h.col2 END,
col3 = CASE n WHEN 3 THEN h.col3 END,
col4 = CASE n WHEN 4 THEN h.col4 END,
col5 = CASE n WHEN 5 THEN h.col5 END,
v.n
FROM (SELECT keystr = str(h.col1) + str(h.col2) +
str(h.col3) + str(h.col4) +
str(h.col5),
col1, col2, col3, col4, col5
FROM h) AS h
CROSS JOIN (SELECT n = 1 UNION
SELECT 2 UNION
SELECT 3 UNION
SELECT 4 UNION
SELECT 5) AS v) AS a
LEFT JOIN
(SELECT keystr = str(h.col1) + str(h.col2) + str(h.col3) +
str(h.col4) + str(h.col5),
rowno = (SELECT COUNT(*)
FROM h AS h1
WHERE str(h1.col1) + str(h1.col2) +
str(h1.col3) + str(h1.col4) +
str(h1.col5) <=
str(h.col1) + str(h.col2) +
str(h.col3) + str(h.col4) +
str(h.col5)),
col1 = CASE n WHEN 1 THEN h.col1 END,
col2 = CASE n WHEN 2 THEN h.col2 END,
col3 = CASE n WHEN 3 THEN h.col3 END,
col4 = CASE n WHEN 4 THEN h.col4 END,
col5 = CASE n WHEN 5 THEN h.col5 END,
v.n
FROM h
CROSS JOIN (SELECT n = 1 UNION
SELECT 2 UNION
SELECT 3 UNION
SELECT 4 UNION
SELECT 5) AS v) AS b
ON a.rowno = b.rowno + 1
AND a.n = b.n) AS final
WHERE col1 IS NOT NULL OR
col2 IS NOT NULL OR
col3 IS NOT NULL OR
col4 IS NOT NULL OR
col5 IS NOT NULL
ORDER BY keystr
go
DROP TABLE h


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 10 '06 #2
You are right, I'm going to regret your query... :-) Thank you!
This one ist very simple n fast!
The difference is that it will be sorted. But it doesn't matter, I just
need the hierarchy.
Thank you!
SQLNULL

CREATE TABLE h (col1 int NOT NULL,
col2 int NOT NULL,
col3 int NOT NULL,
col4 int NOT NULL,
col5 int NOT NULL,
PRIMARY KEY (col1, col2, col3, col4, col5))
go
INSERT h (col1, col2, col3, col4, col5)
EXEC ('SELECT 38, 75, 233, 916, 2770
SELECT 38, 75, 233, 916, 2771
SELECT 38, 75, 233, 916, 2772
SELECT 38, 75, 233, 923, 2654
SELECT 38, 75, 233, 923, 2655
SELECT 38, 75, 245, 913, 2454
SELECT 38, 75, 245, 913, 2456')
go
SELECT c1 as col1, c2 as col2, c3 as col3, c4 as col4, c5 as col5
FROM (
SELECT DISTINCT col1 AS c1, c2=NULL, c3=NULL, c4=NULL, c5=NULL,
col1, col2=NULL, col3=NULL, col4=NULL,
col5=NULL
FROM h
UNION
SELECT DISTINCT NULL, col2, NULL, NULL, NULL,
col1, col2, NULL, NULL, NULL
FROM h
UNION
SELECT DISTINCT NULL, NULL, col3, NULL, NULL,
col1, col2, col3, NULL, NULL
FROM h
UNION
SELECT DISTINCT NULL, NULL, NULL, col4, NULL,
col1, col2, col3, col4, NULL
FROM h
UNION
SELECT NULL, NULL, NULL, NULL, col5,
col1, col2, col3, col4, col5
FROM h
) BIGUNION
ORDER BY col1, col2, col3, col4, col5
go
DROP TABLE h

Jul 12 '06 #3
Let me do what I always do and suggest that you get a copy of TREES &
HIERARCHIES IN SQL for several better approachs for modeling this kind
of structure. Right now, you are destroying information with false
NULLs. What were you trying to do?

Jul 15 '06 #4

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

Similar topics

8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
1
by: Chris Lasher | last post by:
Hello, I'm trying to write a tool to scrape through some of the Ribosomal Database Project II's (http://rdp.cme.msu.edu/) pages, specifically, through the Hierarchy Browser....
0
by: archway | last post by:
Hi, I would like to build a hierarchy of ProductNode objects like so ProductNode ---Product Node ---ProductNode ------ProductNode ------ProductNode ---ProductNode
2
by: Matt | last post by:
Hello, I would like to generate what I call an "overall class hierarchy" in UML automatically derived from my C++ code source. An example of what I seek: ...
2
by: Yaro | last post by:
Hi All (UDB 8.1 FP8, Win) In my simple test database, I have two tables CREATE TABLE "DB2ADMIN"."AAA" ( "F1" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, "F2" VARCHAR(10), PRIMARY KEY...
4
by: Dave Veeneman | last post by:
I have an object hierarchy that's several layers deep, and I'm puzzling over how to access an object at any point in the hierarchy, solely by its key. The hierarchy is similar to an...
21
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new...
2
by: Do | last post by:
Hi, I have a database table with the following fields: id, name, parentid. These fields are supposed to create a hierarchy for a list box, an infinite hierarchy Child fields of parent fields...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
3
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.