Solved

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

Posted on 2007-11-21
7
729 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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 …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

624 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