Solved

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

Posted on 2007-11-21
7
724 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 35

Accepted Solution

by:
David Todd earned 168 total points
Comment Utility
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 166 total points
Comment Utility
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 168 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 166 total points
Comment Utility
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 166 total points
Comment Utility
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 166 total points
Comment Utility
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
Comment Utility
Thanks everybody for your help was much appreciated
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now