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

What looks like a basic SQL query still not resolved

ARRRRRRRGGGGGHHHHH!!

Please can you help, I'm going round the bend with this.

I have a simple and small table called STOCKCATS, which I need to query to
get back a dataset in a particular order, but although it looks simple I
can't get it to work. My table schema plus sample data to see the problem
is as follows:

DROP TABLE IF EXISTS `STOCKCATS`;
CREATE TABLE `STOCKCATS` (
`CATID` varchar(30) NOT NULL default '',
`LEVEL` varchar(30) default NULL,

PRIMARY KEY (`CATID`),
KEY `indxCATEGORYID` (`CATID`)
);

INSERT INTO `STOCKCATS` (`CATID`,`LEVEL`) VALUES
('A001',''),
('A002','A001'),
('A003','A001'),
('A004','A001'),
('A005','A001'),
('PCHW01',''),
('MHW01',''),
('FD01',''),
('ELEC01',''),
('MHW02','MHW01');
INSERT INTO `STOCKCATS` (`CATID`,`LEVEL`) VALUES
('MHW03','MHW01'),
('MHW04','MHW01'),
('MHW05','MHW01'),
('PCHW02','PCHW01'),
('PCHW03','PCHW01'),
('PCHW04','PCHW01'),
('PCHW05','PCHW01'),
('PCSW01',''),
('MSW01',''),
('C001',''),
('C002','C001'),
('C003','C001'),
('MV',''),
('SUZ','MV'),
('ALF','MV'),
('PLASMA','ELEC01'),
('T01','ELEC01'),
('HEATING',''),
('RAD','HEATING'),
('P01',''),
('B01','P01'),
('BB','HEATING'),
('FS','HEATING'),
('WM','HEATING'),
('AEROSOL',''),
('SOLVENTS','AEROSOL'),
('DGC','');
INSERT INTO `STOCKCATS` (`CATID`,`LEVEL`) VALUES
('DGXWINDOWS','DGC'),
('DGXEXTRA','DGC'),
('DGXCON','DGC');

As you can see from the table structure, this table consists of 2 field
values. The 1st is the category code and the 2nd is the level is at. If a
catid has a level of nothing, eg '', then it means that it is a root level
category. If a catid has a another cat's catid in it's level, eg B01 has
P01, then it is a sub-category of this category, eg B01 is a sub-cat of P01.

All I want to do is query this table and bring back the data so that
alphabetically it goes root level cat A1, then all the sub-cats for this
root level, then root level A2, then all sub-cats for this root level and so
on. An example using the above would be as follows:

^ ^ A to G of root level cats plus their sub-cats....

HEATING << root level
BB << sub-cat of heating
FS << sub-cat of heating
WM << sub-cat of heating

\/ \/ I to Z of root level cats plus their sub-cats....

A few posters kindly gave me a solution of ORDER BY COALESCE(CATID,LEVEL),
CATID and I thought this had done it, but I was looking at the ('A001',''),
('A002','A001'), ('A003','A001'), ('A004','A001'), ('A005','A001')
entries as these naturally fell into place. If you use this order command
on the above you will see that ('P01','') and it's associated ('B01','P01')
sub-cat just don't come together.

Does any body have any ideas?

Thanks

Laphan

Nov 23 '05 #1
2 1300
It's not so bad. Question: Can a category belong to more than one level? And
thanks for posting the DDL. It makes it much easier.

"Laphan" <in**@SpamMeNot.co.uk> wrote in message
news:11*************@corp.supernews.com...
ARRRRRRRGGGGGHHHHH!!

Please can you help, I'm going round the bend with this.

I have a simple and small table called STOCKCATS, which I need to query to
get back a dataset in a particular order, but although it looks simple I
can't get it to work. My table schema plus sample data to see the problem
is as follows:

DROP TABLE IF EXISTS `STOCKCATS`;
CREATE TABLE `STOCKCATS` (
`CATID` varchar(30) NOT NULL default '',
`LEVEL` varchar(30) default NULL,

PRIMARY KEY (`CATID`),
KEY `indxCATEGORYID` (`CATID`)
);

INSERT INTO `STOCKCATS` (`CATID`,`LEVEL`) VALUES
('A001',''),
('A002','A001'),
('A003','A001'),
('A004','A001'),
('A005','A001'),
('PCHW01',''),
('MHW01',''),
('FD01',''),
('ELEC01',''),
('MHW02','MHW01');
INSERT INTO `STOCKCATS` (`CATID`,`LEVEL`) VALUES
('MHW03','MHW01'),
('MHW04','MHW01'),
('MHW05','MHW01'),
('PCHW02','PCHW01'),
('PCHW03','PCHW01'),
('PCHW04','PCHW01'),
('PCHW05','PCHW01'),
('PCSW01',''),
('MSW01',''),
('C001',''),
('C002','C001'),
('C003','C001'),
('MV',''),
('SUZ','MV'),
('ALF','MV'),
('PLASMA','ELEC01'),
('T01','ELEC01'),
('HEATING',''),
('RAD','HEATING'),
('P01',''),
('B01','P01'),
('BB','HEATING'),
('FS','HEATING'),
('WM','HEATING'),
('AEROSOL',''),
('SOLVENTS','AEROSOL'),
('DGC','');
INSERT INTO `STOCKCATS` (`CATID`,`LEVEL`) VALUES
('DGXWINDOWS','DGC'),
('DGXEXTRA','DGC'),
('DGXCON','DGC');

As you can see from the table structure, this table consists of 2 field
values. The 1st is the category code and the 2nd is the level is at. If a catid has a level of nothing, eg '', then it means that it is a root level
category. If a catid has a another cat's catid in it's level, eg B01 has
P01, then it is a sub-category of this category, eg B01 is a sub-cat of P01.
All I want to do is query this table and bring back the data so that
alphabetically it goes root level cat A1, then all the sub-cats for this
root level, then root level A2, then all sub-cats for this root level and so on. An example using the above would be as follows:

^ ^ A to G of root level cats plus their sub-cats....

HEATING << root level
BB << sub-cat of heating
FS << sub-cat of heating
WM << sub-cat of heating

\/ \/ I to Z of root level cats plus their sub-cats....

A few posters kindly gave me a solution of ORDER BY COALESCE(CATID,LEVEL),
CATID and I thought this had done it, but I was looking at the ('A001',''), ('A002','A001'), ('A003','A001'), ('A004','A001'), ('A005','A001')
entries as these naturally fell into place. If you use this order command
on the above you will see that ('P01','') and it's associated ('B01','P01') sub-cat just don't come together.

Does any body have any ideas?

Thanks

Laphan

Nov 23 '05 #2
Hi Guys

Many thanks for your replies. I must be honest, I've been a naughty poster
and posted this in an ASP ng as well. A genius called David helped me with
the answer, so just for your ref the solution is as follows:

SELECT catid, level
FROM stockcats
ORDER BY COALESCE(NULLIF(level,''),catid),catid;

Rgds Laphan

"Rich Ryan" <ry****@sbcglobal.net> wrote in message
news:p7***************@newssvr24.news.prodigy.net. ..
It's not so bad. Question: Can a category belong to more than one level? And
thanks for posting the DDL. It makes it much easier.

"Laphan" <in**@SpamMeNot.co.uk> wrote in message
news:11*************@corp.supernews.com...
ARRRRRRRGGGGGHHHHH!!

Please can you help, I'm going round the bend with this.

I have a simple and small table called STOCKCATS, which I need to query to
get back a dataset in a particular order, but although it looks simple I
can't get it to work. My table schema plus sample data to see the problem
is as follows:

DROP TABLE IF EXISTS `STOCKCATS`;
CREATE TABLE `STOCKCATS` (
`CATID` varchar(30) NOT NULL default '',
`LEVEL` varchar(30) default NULL,

PRIMARY KEY (`CATID`),
KEY `indxCATEGORYID` (`CATID`)
);

INSERT INTO `STOCKCATS` (`CATID`,`LEVEL`) VALUES
('A001',''),
('A002','A001'),
('A003','A001'),
('A004','A001'),
('A005','A001'),
('PCHW01',''),
('MHW01',''),
('FD01',''),
('ELEC01',''),
('MHW02','MHW01');
INSERT INTO `STOCKCATS` (`CATID`,`LEVEL`) VALUES
('MHW03','MHW01'),
('MHW04','MHW01'),
('MHW05','MHW01'),
('PCHW02','PCHW01'),
('PCHW03','PCHW01'),
('PCHW04','PCHW01'),
('PCHW05','PCHW01'),
('PCSW01',''),
('MSW01',''),
('C001',''),
('C002','C001'),
('C003','C001'),
('MV',''),
('SUZ','MV'),
('ALF','MV'),
('PLASMA','ELEC01'),
('T01','ELEC01'),
('HEATING',''),
('RAD','HEATING'),
('P01',''),
('B01','P01'),
('BB','HEATING'),
('FS','HEATING'),
('WM','HEATING'),
('AEROSOL',''),
('SOLVENTS','AEROSOL'),
('DGC','');
INSERT INTO `STOCKCATS` (`CATID`,`LEVEL`) VALUES
('DGXWINDOWS','DGC'),
('DGXEXTRA','DGC'),
('DGXCON','DGC');

As you can see from the table structure, this table consists of 2 field
values. The 1st is the category code and the 2nd is the level is at. If a catid has a level of nothing, eg '', then it means that it is a root level
category. If a catid has a another cat's catid in it's level, eg B01 has
P01, then it is a sub-category of this category, eg B01 is a sub-cat of P01.
All I want to do is query this table and bring back the data so that
alphabetically it goes root level cat A1, then all the sub-cats for this
root level, then root level A2, then all sub-cats for this root level and so on. An example using the above would be as follows:

^ ^ A to G of root level cats plus their sub-cats....

HEATING << root level
BB << sub-cat of heating
FS << sub-cat of heating
WM << sub-cat of heating

\/ \/ I to Z of root level cats plus their sub-cats....

A few posters kindly gave me a solution of ORDER BY COALESCE(CATID,LEVEL),
CATID and I thought this had done it, but I was looking at the ('A001',''), ('A002','A001'), ('A003','A001'), ('A004','A001'), ('A005','A001')
entries as these naturally fell into place. If you use this order command
on the above you will see that ('P01','') and it's associated ('B01','P01') sub-cat just don't come together.

Does any body have any ideas?

Thanks

Laphan


Nov 23 '05 #3

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

Similar topics

5
by: lkrubner | last post by:
I have a webserver through Rackspace. I create a domain. I create an FTP user. I upload some files. I create a database called testOfSetupScript and then I create a database user named setup. I...
20
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
3
by: sal cifone | last post by:
Hey, Is there any way to turn off the query parm prompts? I have a report that must be printed for 53 different cities and the vba code loops thru a table and passes the value to a select query....
1
by: nicole bissell | last post by:
Hello, I am working in a new form and still making changes to the visual basic code of the form. Not always, but very often when I try to save my changes in the visual basic window and press the...
6
by: Laphan | last post by:
ARRRRRRRGGGGGHHHHH!! Please can you help, I'm going round the bend with this. I have a simple and small table called STOCKCATS, which I need to query to get back a dataset in a particular...
39
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
6
by: Vince | last post by:
Hello all, I am using Visual Basic to open a saved query and then save information in the query to an array for later use. The problem is that the same query shows different results when opened...
6
by: lisacrowe | last post by:
I have a simple database recording complaints. A crosstab query is based on a query which returns resolved complaints only. The crosstab has the field Complaint Type as a row heading and Outcome as a...
7
by: SteveGod | last post by:
Hi, I'm trying to create a report that will produce automated sets of Committee Minutes for School Appeals, where there are a set range of different outcomes. The field involved are all from...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.