Solved

How would modify a query to group by a field and within each grouping, have an order by clause?

Posted on 2014-01-10
5
312 Views
Last Modified: 2014-01-10
I am working with a query using Access 2003.

How would you modify the following query to order the records by
 [Age(Days)] DESC within tblBanks.[Senior Management

In other words I want to group records by the field tblBanks.[Senior Management
and withinin each grouping, the records should be sorted in [Age(Days)] DESC.


SELECT tblOpenItems.[Process Date], tblOpenItems.[Trans Date], tblBanks.[Bank Code] AS [BRS#],
tblBanks.[GLAcct#] As [Taps/Margin],
tblBanks.[ACCOUNT#] As [Bank Account Number],  tblOpenItems.T As [_Type], tblOpenItems.Type As [Trans Code],
tblOpenItems.Description,
tblOpenItems.[Office] & ' ' & [CheckNum] As [Check/Reference#],
tblOpenItems.Amount,  tblOpenItems.AgeDays As [Age(Days)], tblOpenItems.footnote As Comments,
tblBanks.[Report Name] As Responsibility,tblBanks.Currency,tblBanks.[Senior Management Tab]
FROM tblDates, tblBanks INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
Where tblBanks.[RISK REPORT] = 'YES' AND tblOpenItems.t In ("A","D","E");
UNION ALL
SELECT tblOpenItems.[Process Date], tblOpenItems.[Trans Date], tblBanks.[Bank Code] AS [BRS#],
tblBanks.[GLAcct#] As [Taps/Margin],
tblBanks.[ACCOUNT#] As [Bank Account Number],  tblOpenItems.T As [_Type], tblOpenItems.Type As [Trans Code],
tblOpenItems.Description,
tblOpenItems.[Office] & ' ' & [CheckNum] As [Check/Reference#],
tblOpenItems.Amount,  tblOpenItems.AgeDays As [Age(Days)], tblOpenItems.footnote As Comments,
tblBanks.[Report Name] As Responsibility,tblBanks.Currency,tblBanks.[Senior Management Tab]
FROM tblDates, tblBanks INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
Where tblBanks.[RISK REPORT] = 'YES' AND tblOpenItems.t In ("B","C")
ORDER BY [Age(Days)] DESC;
0
Comment
Question by:zimmer9
  • 3
5 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 39772206
try this


select x.*
From
(SELECT tblOpenItems.[Process Date], tblOpenItems.[Trans Date], tblBanks.[Bank Code] AS [BRS#],
tblBanks.[GLAcct#] As [Taps/Margin],
tblBanks.[ACCOUNT#] As [Bank Account Number],  tblOpenItems.T As [_Type], tblOpenItems.Type As [Trans Code],
tblOpenItems.Description,
tblOpenItems.[Office] & ' ' & [CheckNum] As [Check/Reference#],
tblOpenItems.Amount,  tblOpenItems.AgeDays As [Age(Days)], tblOpenItems.footnote As Comments,
tblBanks.[Report Name] As Responsibility,tblBanks.Currency,tblBanks.[Senior Management Tab]
FROM tblDates, tblBanks INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
Where tblBanks.[RISK REPORT] = 'YES' AND tblOpenItems.t In ("A","D","E");
UNION ALL
SELECT tblOpenItems.[Process Date], tblOpenItems.[Trans Date], tblBanks.[Bank Code] AS [BRS#],
tblBanks.[GLAcct#] As [Taps/Margin],
tblBanks.[ACCOUNT#] As [Bank Account Number],  tblOpenItems.T As [_Type], tblOpenItems.Type As [Trans Code],
tblOpenItems.Description,
tblOpenItems.[Office] & ' ' & [CheckNum] As [Check/Reference#],
tblOpenItems.Amount,  tblOpenItems.AgeDays As [Age(Days)], tblOpenItems.footnote As Comments,
tblBanks.[Report Name] As Responsibility,tblBanks.Currency,tblBanks.[Senior Management Tab]
FROM tblDates, tblBanks INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
Where tblBanks.[RISK REPORT] = 'YES' AND tblOpenItems.t In ("B","C")
) As X
Order By x.[Age(Days)] Desc


or use use this Order bY

Order By 11 DESC
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39772531
ORDER BY [Senior Management Tab], [Age(Days)] DESC

OR

ORDER BY [Senior Management Tab] ASC, [Age(Days)] DESC
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 39772565
oops sorry, forgot about the Management group


select x.*
From
(SELECT tblOpenItems.[Process Date], tblOpenItems.[Trans Date], tblBanks.[Bank Code] AS [BRS#],
tblBanks.[GLAcct#] As [Taps/Margin],
tblBanks.[ACCOUNT#] As [Bank Account Number],  tblOpenItems.T As [_Type], tblOpenItems.Type As [Trans Code],
tblOpenItems.Description,
tblOpenItems.[Office] & ' ' & [CheckNum] As [Check/Reference#],
tblOpenItems.Amount,  tblOpenItems.AgeDays As [Age(Days)], tblOpenItems.footnote As Comments,
tblBanks.[Report Name] As Responsibility,tblBanks.Currency,tblBanks.[Senior Management Tab]
FROM tblDates, tblBanks INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
Where tblBanks.[RISK REPORT] = 'YES' AND tblOpenItems.t In ("A","D","E");
UNION ALL
SELECT tblOpenItems.[Process Date], tblOpenItems.[Trans Date], tblBanks.[Bank Code] AS [BRS#],
tblBanks.[GLAcct#] As [Taps/Margin],
tblBanks.[ACCOUNT#] As [Bank Account Number],  tblOpenItems.T As [_Type], tblOpenItems.Type As [Trans Code],
tblOpenItems.Description,
tblOpenItems.[Office] & ' ' & [CheckNum] As [Check/Reference#],
tblOpenItems.Amount,  tblOpenItems.AgeDays As [Age(Days)], tblOpenItems.footnote As Comments,
tblBanks.[Report Name] As Responsibility,tblBanks.Currency,tblBanks.[Senior Management Tab]
FROM tblDates, tblBanks INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
Where tblBanks.[RISK REPORT] = 'YES' AND tblOpenItems.t In ("B","C")
) As X
Order By X.[Senior Management Tab], x.[Age(Days)] Desc




.
0
 

Author Comment

by:zimmer9
ID: 39772593
Please see attached doc. Thank you.
Synax.doc
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 39772633
remove the semi colon  from this line

Where tblBanks.[RISK REPORT] = 'YES' AND tblOpenItems.t In ("A","D","E"); '<<<<
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

919 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

16 Experts available now in Live!

Get 1:1 Help Now