Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 1st, 2003, 04:32 AM
Authorized User
 
Join Date: Jun 2003
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default i don't get values from 4 tables in right order

hi,

i have 4 tables which i need to create a list of values. it's easier for the user if they are alphabetisized so he can find them easier...but i can't figure out exactly how to do that.

my 4 tables:
LOCATION ([u]locationId</u>, name)
DEPARTMENT ([u]departmentId</u>, locationId, name)
SUBDEPARTMENT ([u]subdepartmentId</u>, departmentid, name)
COST ([u]costId</u>, locationId, departmentId, subdepartmentId, name)

a location has 1 to many costs
a department has 1 to many costs
a department has 1 to many subdepartments

What i need is a list in this order:
===========================
location
    department
        subdepartment
            cost
===========================

somehow i can only get this list for one location and not for all of them

if some sql whizzkid could write a statement for this problem i would be very grateful :D

thanks in advance,

Harold
 
Old August 10th, 2003, 08:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You aren't very clear on what your desired output is, but how about:
Code:
SELECT LOCATION.name as LocName, DEPARTMENT.name as DeptName, SUBDEPARTMENT.name as SubName, COST.name as CostName
FROM COST
    INNER JOIN LOCATION ON COST.LocationId=LOCATION.LocationId
    INNER JOIN DEPARTMENT ON COST.departmentId=DEPARTMENT.departmentId
    INNER JOIN SUBDEPARTMENT ON COST.subdepartmentId=SUBDEPARTMENT.subdepartmentId
ORDER BY LocName, DeptName, SubName, CostName;

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 19th, 2003, 02:48 AM
Authorized User
 
Join Date: May 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT LOCATION.name as LocName, DEPARTMENT.name as DeptName, SUBDEPARTMENT.name as SubName, COST.name as CostName
FROM LOCATION, DEPARTMENT, SUBDEPARTMENT, COST
WHERE
COST.LocationId=LOCATION.LocationId AND COST.departmentId=DEPARTMENT.departmentId AND
COST.subdepartmentId=SUBDEPARTMENT.subdepartmentId
ORDER BY LocName, DeptName, SubName, CostName;

Deepesh Jain
VB,VBA & .NET Specialist
Wiley Support Team
 
Old September 19th, 2003, 06:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by Deepesh_Jain
 SELECT LOCATION.name as LocName, DEPARTMENT.name as DeptName, SUBDEPARTMENT.name as SubName, COST.name as CostName
FROM LOCATION, DEPARTMENT, SUBDEPARTMENT, COST
WHERE
COST.LocationId=LOCATION.LocationId AND COST.departmentId=DEPARTMENT.departmentId AND
COST.subdepartmentId=SUBDEPARTMENT.subdepartmentId
ORDER BY LocName, DeptName, SubName, CostName;

Deepesh Jain
VB,VBA & .NET Specialist
Wiley Support Team
This is exactly equivalent to the query I posted above, but it uses the deprecated "old style join" notation. I don't recommend that people use this style any longer. It's OK for INNER JOINs and the two forms of query will result in identical execution plans, but ambiguities can result when using the "old style" for OUTER JOINs and the associated *= operator in the WHERE clause.

This is why the ANSI SQL 92 standard refers to the construction as "old style join" syntax, and introduces the new syntax.

Besides, YMMV, but I think the newer style is easier to read, and refrains from "cluttering up" the WHERE clause leaving it for true row selection operations.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting values from other tables TheAndruu SQL Language 7 May 27th, 2007 10:47 AM
passing values between two tables bright_mulenga Access 1 January 24th, 2007 11:27 AM
Order by based on two columns, each in two tables? LockesRabb PHP Databases 2 December 17th, 2006 12:19 PM
Order by based on two columns, each in two tables? LockesRabb SQL Language 4 December 14th, 2006 02:42 AM
Order a recordset by specific values SoC SQL Language 2 August 28th, 2005 08:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.