If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > Select Statement Help Required

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-29-10, 05:01
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
Select Statement Help Required

Hi all....
Can anyone help me out with a select statement?

I have a 'categories' table and need to select all records where the ParentID equals a given value, eg. 137, but in the same statement i also need to select all records where the ParentID equals the CategoryID of the first part of the select statement.
So the first part would give me records like this:

CategoryID ParentID Category
1 137 17th Edition Testers
2 137 Clampmeters
3 137 General Electricals

The second part would give me records based on ParentID = CateogryID from the first part of the statement like.

CategoryID ParentID Category
9 1 Calibration Checkboxes
10 1 RCD Testers
11 1 Loop Testers
19 2 Fluke Clampmeters

etc....

But i need all of these records from one select statement!
Is this possible?

Thanks
Andy
Reply With Quote
  #2 (permalink)  
Old 04-29-10, 07:17
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
SELECT CategoryID 
     , ParentID 
     , Category
  FROM categories
 WHERE ParentID = 137
UNION ALL
SELECT two.CategoryID 
     , two.ParentID 
     , two.Category
  FROM categories AS one
INNER
  JOIN categories AS two
    ON two.ParentID = one.CategoryID
 WHERE one.ParentID = 137
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-29-10, 10:20
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
r937 - Thanks that works perfectly :-)

Very much appreciated
Andy
Reply With Quote
  #4 (permalink)  
Old 05-21-10, 07:25
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
A bit more help required please

My requirements have changed slightly from my original post and i hope i can get some help on my 'new headache' :-)

I need to pull the three levels (top, sub, sub sub) from my database for the menu system im making.
At present im getting two levels.
I've had a few attempts at at but so far haven't succeeded

At present i'm using the following SQL :
SELECT one.ParentID, one.Category AS Parent, one.CatID, one.CategoryID, two.Category, two.CategoryID
FROM categories AS one INNER JOIN categories AS two ON two.ParentID=one.CategoryID
WHERE one.ParentID=0 And one.CatID='e'
ORDER BY one.Category;


I have attached the categories table and SQL Query Result screengrabs to illustrate.

Thanks in advance - fingers crossed

Andy
Attached Thumbnails
Select Statement Help Required-categories.gif   Select Statement Help Required-query_result.gif  
Reply With Quote
  #5 (permalink)  
Old 05-21-10, 09:13
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
try this --
Code:
SELECT one.ParentID
     , one.Category AS Parent
     , one.CatID
     , one.CategoryID
     , two.Category
     , two.CategoryID
     , three.Category
     , three.CategoryID
  FROM (
       categories AS one 
INNER 
  JOIN categories AS two 
    ON two.ParentID = one.CategoryID
       )
INNER 
  JOIN categories AS three 
    ON three.ParentID = two.CategoryID
 WHERE one.ParentID = 0 
   AND one.CatID = 'e'
ORDER 
    BY one.Category
     , two.Category
     , three.Category
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 05-21-10 at 10:03.
Reply With Quote
  #6 (permalink)  
Old 05-21-10, 09:31
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
Thanks r937

I'm getting an error when i run the query - please see attached screen grab
Looks like you know what you're doing though.
Hope you can see where the problem is

Andy
Attached Thumbnails
Select Statement Help Required-query_result2.gif  
Reply With Quote
  #7 (permalink)  
Old 05-21-10, 10:03
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by AndyJay View Post
I'm getting an error when i run the query
you forgot to mention that you're using access

i've edited my query to include the necessary parentheses that access requires when more than 2 tables are involved in a join
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 05-21-10, 10:24
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
r937 - you are a star :-)
How the hell did you work it out?
I need to read up more on SQL

My local machine is using Access, but the live site is using MS SQL so both examples will be very useful.

Thanks again - fantastic
Andy
Reply With Quote
  #9 (permalink)  
Old 05-21-10, 10:59
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by AndyJay View Post
My local machine is using Access, but the live site is using MS SQL so both examples will be very useful.
i urge you most sincerely to install SQL Server Express on your local machine so that you can test your queries properly

there are numerous differences between Access SQL and SQL Server SQL

save yourself a lot of future frustration and heartache
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 05-26-10, 05:40
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
Hi r937
I took your advice and now have SQL Server 2008 Express on my local machine :-)
I ran the Query in SQL Server without the extra parentheses and it gives the correct results for the subsub categories.

Is there a way of getting all of the categories displaying in the three tables (parent, category, category)?
This would allow me to create just one recordset instead of two.
I've attached a screengrab of the subsub category results and another showing the desired results (subsub category needs to be in the correct order)
Sorry to be such a pain.....
How do you create your SQL queries?

Thanks
Andy
Attached Thumbnails
Select Statement Help Required-query_sg.jpg   Select Statement Help Required-required_query_sg.jpg  
Reply With Quote
  #11 (permalink)  
Old 05-26-10, 06:54
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by AndyJay View Post
Is there a way of getting all of the categories displaying in the three tables (parent, category, category)?
This would allow me to create just one recordset instead of two.
sorry, i don't know what you're asking here

Quote:
Originally Posted by AndyJay View Post
How do you create your SQL queries?
in UltraEdit (a text editor)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 05-26-10, 09:38
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
Thanks for the reply again r937 - sorry to take up your time. It's greatly appreciated.
I've attached another screengrab. Hope it helps
Thanks
Andy
Attached Thumbnails
Select Statement Help Required-subsub.jpg  
Reply With Quote
  #13 (permalink)  
Old 05-26-10, 10:25
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by AndyJay View Post
I've attached another screengrab.
well, i can see that you're displaying a heirarchy to the 3rd level

what was the question again?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 05-26-10, 11:21
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
Hi r937

That screen grab is just two images stuck together, showing what i want to get out of the database :-)
I would like to get this result using one statement and one recordset instead of two.
The first SQL below displays the top and sub categories
The second only displays the subsub categories

Thanks again - owe you a beer



SELECT one.ParentID
, one.Category AS Parent
, one.CatID
, one.CategoryID
, two.Category
, two.CategoryID
FROM categories AS one
INNER
JOIN categories AS two
ON two.ParentID=one.CategoryID
WHERE one.ParentID=0 And one.CatID='e'
ORDER BY one.Category;


---------------------


SELECT one.ParentID
, one.Category AS Parent
, one.CatID
, one.CategoryID
, two.Category
, two.CategoryID
, three.Category
, three.CategoryID
FROM (
categories AS one
INNER
JOIN categories AS two
ON two.ParentID = one.CategoryID
)
INNER
JOIN categories AS three
ON three.ParentID = two.CategoryID
WHERE one.ParentID = 0
AND one.CatID = 'e'
ORDER
BY one.Category
, two.Category
, three.Category
Reply With Quote
  #15 (permalink)  
Old 05-26-10, 11:44
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
use the second query and change the INNER JOINs to LEFT OUTER JOINs

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On