Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - MS Access: Subselect, order by and such.
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

MS Access: Subselect, order by and such.

 Post Reply Post Reply
Author
Jyde View Drop Down
Newbie
Newbie


Joined: 03 February 2005
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote Jyde Quote  Post ReplyReply Direct Link To This Post Topic: MS Access: Subselect, order by and such.
    Posted: 03 February 2005 at 7:47pm
Hi all,

I have this small problem that is bugging my to quitsville...

I have two tables (simplyfied here):

Company:
ID
Description

CompanyName:
CompanyID (foreign to ID above, many-to-one)
Name
FromYear

The Company table is self-explanatory.
The CompanyName table holds the different names a particular company have had since it started out.

In order to generate a list of company names for a given year, I do something like this (again simplified here):

SELECT co.ID, co.Description,
(SELECT top 1 cn.Name from CompanyName cn where cn.CompanyID=co.ID) as Name
FROM Company co
WHERE cn.FromYear >= [YEAR]
ORDER BY co.ID

[YEAR] is a parameter I supply.

My problem is that I would ideally like to have this list sorted by the company names, but alas one cannot sort by a subquery.
I have tried with inner and outer joins, group by, but I keep hitting a wall.

Please bear in mind that this is the simplified version, and this ties in with other tables, so re-structuring the tables is not an option.

If anyone has any ideas how to get around this, I would be sleeping a lot better! I am fairly routined at SQL and I cannot believe I cannot solve this minor thing. I guess it is just one of those things...

All the best...
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post Posted: 03 February 2005 at 9:05pm
Why do you use a subquery? just use a join like
Select co.ID, co.Description, cn.CompanyName from Company co Join CompanyName cn on co.id=cn.CompanyID
order by cn.Companyname
Back to Top
Jyde View Drop Down
Newbie
Newbie


Joined: 03 February 2005
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote Jyde Quote  Post ReplyReply Direct Link To This Post Posted: 04 February 2005 at 6:47am
Michael, thanks for your reply, but you missed a point:

The CompanyName table is many-to-one and and I only want one (thus the top 1) name from that, ie. the name that is relevant to the year (FromYear).

A simple join would give me loads of dublicate company IDs and all the names a company has had over time.

Jyde


Edited by Jyde - 04 February 2005 at 6:49am
Back to Top
WebWiz-Bruce View Drop Down
Admin Group
Admin Group
Avatar
Web Wiz Developer

Joined: 03 September 2001
Location: Bournemouth
Status: Offline
Points: 9844
Post Options Post Options   Thanks (0) Thanks(0)   Quote WebWiz-Bruce Quote  Post ReplyReply Direct Link To This Post Posted: 04 February 2005 at 7:14am
Can you not use:-

SELECT DISTINCT

So you don't get redundencies?
Back to Top
Jyde View Drop Down
Newbie
Newbie


Joined: 03 February 2005
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote Jyde Quote  Post ReplyReply Direct Link To This Post Posted: 04 February 2005 at 7:43am
Problem with DISTINCT is that it works on all the fields in the select statement, thus if I select only DISTINCT ID, it would work, though if I did SELECT DISTINCT ID, Name, it would give me distinct combinations, ie. all of them.

But you did give me a good idea though. I think I have tried, but will have a look.

If anyone has any other ideas, please keep them coming.

Thanks.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.