Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Microsoft Access Query Problem: Order by mutiple values on a particular column

Posted on 2007-11-21
7
Medium Priority
?
730 Views
Last Modified: 2010-04-21
Hi I'm trying to write a query that will be ordered in two different ways. My table has a field called Category and in this field I have 6 values which are Conservation, Culture, Environment, Voluntary, Community, and disadvantaged.

I want my query to be ordered so that it firstly shows the records for community voluntary then disadvantaged  - in that order

then after that to show the records for conservation culture snd environment

At the moment as a work around I have 2 seperate queries. So the first one orders my records by Community, Disadvantaged & Voluntary, the second query orders my records by Conservation, Culture & Environment. But I would really like to be able to have all this in one query and to have it in the particular order so it's not alphabetical like in the first query. I have attached the SQL I have used for each query:

Many thanks in advance for your help.
       
SELECT [tbl Organisation].[Organisation ID], [tbl Organisation].Organisation, [tbl subcategory].subcategory, [tbl Organisation].[Main Purpose], [tbl subcategory].subcategoryid, [tbl Organisation].[Registration Form Received]
 
FROM [tbl Organisation] INNER JOIN [tbl subcategory] ON [tbl Organisation].SubCategoryID = [tbl subcategory].subcategoryid
 
WHERE ((([tbl subcategory].subcategory)="Voluntary" Or ([tbl subcategory].subcategory)="Community" Or ([tbl subcategory].subcategory)="disadvantaged") AND (([tbl Organisation].[Registration Form Received])=Yes))
 
ORDER BY [tbl subcategory].subcategory; 
 
 
 
SELECT [tbl Organisation].[Organisation ID], [tbl Organisation].Organisation, [tbl subcategory].subcategory, [tbl Organisation].[Main Purpose], [tbl subcategory].subcategoryid, [tbl Organisation].[Registration Form Received]
 
FROM [tbl Organisation] INNER JOIN [tbl subcategory] ON [tbl Organisation].SubCategoryID = [tbl subcategory].subcategoryid
 
WHERE ((([tbl subcategory].subcategory)="Conservation" Or ([tbl subcategory].subcategory)="Culture" Or ([tbl subcategory].subcategory)="Environment") AND (([tbl Organisation].[Registration Form Received])=Yes))
 
ORDER BY [tbl subcategory].subcategory;

Open in new window

0
Comment
Question by:GlobexCorp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 35

Accepted Solution

by:
David Todd earned 672 total points
ID: 20330939
Hi,

In the order by clause do something like this:

order by
      case [tbl subcategory].subcategory
            when 'Conservation' then 4
            when 'Culture' then 5
            when 'Environment' then 6
            when 'Voluntary' then 2
            when 'Community' then 1
            when 'disadvantaged' then 3
            else 7
      end

HTH
  David
0
 
LVL 44

Assisted Solution

by:GRayL
GRayL earned 664 total points
ID: 20331071
Save yourself a headache and create another field CatOrder containing the values 1 thru 6.  You have to fill in the correct number for the value in Category.  Then the query becomes:

SELECT * from myTable ORDER BY CatOrder;
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 672 total points
ID: 20331109
Hi,

I agree with GRayL - adding a sortOrder column to this table is most sensible.

I worked on a fashion retail stock system, where every garment is colour-sized.

How do you get the sizes xs s m l xl to sort, or shoe sizes junior 10, junior 12, senior 2 etc which are more often just 10 12 2 to sort? You add a sort order column to the table.

But for one-offs, or where for whatever reason you can't alter the table, then my technique will work best.

HTH
  David
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 664 total points
ID: 20331178
try the switch statement
SELECT [tbl Organisation].[Organisation ID], [tbl Organisation].Organisation, [tbl subcategory].subcategory, [tbl Organisation].[Main Purpose], [tbl subcategory].subcategoryid, [tbl Organisation].[Registration Form Received]
 
FROM [tbl Organisation] INNER JOIN [tbl subcategory] ON [tbl Organisation].SubCategoryID = [tbl subcategory].subcategoryid
 
WHERE ((([tbl subcategory].subcategory)="Voluntary" Or ([tbl subcategory].subcategory)="Community" Or ([tbl subcategory].subcategory)="disadvantaged" Or
([tbl subcategory].subcategory)="Conservation" Or ([tbl subcategory].subcategory)="Culture" Or ([tbl subcategory].subcategory)="Environment")
AND (([tbl Organisation].[Registration Form Received])=Yes))
 
ORDER BY Switch(
([tbl subcategory].subcategory)="Community", 1
,([tbl subcategory].subcategory)="Voluntary", 2
,([tbl subcategory].subcategory)="disadvantaged", 3
,([tbl subcategory].subcategory)="Conservation", 4
,([tbl subcategory].subcategory)="Culture", 5
,([tbl subcategory].subcategory)="Environment", 6
) 

Open in new window

0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 664 total points
ID: 20331210
not disagreeing with GrayL, but sometimes you need to report differently to different people based on different sort orders. you could technically have a CategorySortOrder table with multiple columns, one per report

CategorySortOrder, ForThisReport, ForOtherReport, ForReport3
Community, 1,5,..
Voluntary, 2, 1..
disadvantaged, 3, 2..
Conservation, 4, 4..
Culture, 5, 6..
Environment, 6, 8..
AnotherCategory1, 0, 7..
AnotherCategory2, 0, 3..

and join that into the many reports.  Ahh. but you ask, do you store by the subcategory "text" here, or is it by subcategoryid. what if there are multiple "voluntary"s in the subcategory table by different IDs?  hmm... more table design

to each his own
0
 
LVL 44

Assisted Solution

by:GRayL
GRayL earned 664 total points
ID: 20331419
It's a good thing Access and VBA shake hands so well together.  In other db's, one does not have use of functions like Switch(), so for a db solution....
0
 
LVL 2

Author Closing Comment

by:GlobexCorp
ID: 31410426
Thanks everybody for your help was much appreciated
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question