Solved

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

Posted on 2007-11-21
7
727 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
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 166 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 168 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 166 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 166 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 166 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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