Solved

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

Posted on 2007-11-21
7
725 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

'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 …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

21 Experts available now in Live!

Get 1:1 Help Now