473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Access Query Sorting Problem

Hi Everybody
I have a problem with a small database which l am designing. I've created a simple query using 4 fields:

Sales_ID Pack_Number SOP_Number SOP_Line

When displayed the data looks like this:
1 3 20462 1
2 2 20465 1
3 5 20470 2
4 7 20470 2
5 4 20470 5

As you can see Sales_ID 3 and 4 contain the same data in the SOP_Number and SOP_Line. I would like to display the data but ignore any duplicates within the SOP_Number and SOP_Line fields so that in the above records only 4 records would be displayed 1,2,3,5.

Any help would be most appreciated.
Nov 15 '08 #1
18 1612
ADezii
8,834 Expert 8TB
Assuming your Table Name is tblSales, all Fields, namely: [Sales_ID], [Pack_Number], [SOP_Number], and [SOP_Line] are Numeric (LONG), and [Sales_ID] is the Primary Key, this solution will work. Be advised that SQL is not my strong point, and a better, and shorter, solution will probably be forthcoming.
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT DLast("[Sales_ID]","tblSales","[SOP_Number] = " & [SOP_Number] & _
  2. " And [SOP_Line] = " & [SOP_Line]) AS SalesID, DLookUp("[Pack_Number]","tblSales","[Sales_ID] = " &_
  3.  [SalesID] & " And [SOP_Number] = " & [SOP_Number] & " And [SOP_Line] = " & _
  4. [SOP_Line]) AS PackNum, tblSales.SOP_Number, tblSales.SOP_Line
  5. FROM tblSales;
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. SalesID    PackNum    SOP_Number      SOP_Line
  2. 1              3        20462             1
  3. 2              2        20465             1
  4. 4              7        20470             2
  5. 5              4        20470             5
Nov 15 '08 #2
Hi ADezii
Thanks for replying so promptly. As this is my first forum question l was really impressed. I've tried your reply but am having a problem with an error being reported:

The syntax of the subquery in this expression is incorrect.
Check the subquery's syntax and enclose the subquery in parentheses.

Not being very experienced in queries l've tried to track down the error but not having any luck. Any suggestions most appreciated.
Regards
Nov 16 '08 #3
ADezii
8,834 Expert 8TB
Hi ADezii
Thanks for replying so promptly. As this is my first forum question l was really impressed. I've tried your reply but am having a problem with an error being reported:

The syntax of the subquery in this expression is incorrect.
Check the subquery's syntax and enclose the subquery in parentheses.

Not being very experienced in queries l've tried to track down the error but not having any luck. Any suggestions most appreciated.
Regards
A picture is worth a thousand words, so I made the Test Database available to you as an Attachment. When the DB opens, the Test Query (qrySales) will be selected in the Database Window.
Nov 16 '08 #4
ADezii your a star.
Its now works a treat - l was trying to enter the whole of your code in the criteria of the Sales_ID.

Thanks again for your help.
Regards
Keith
Nov 16 '08 #5
ADezii
8,834 Expert 8TB
ADezii your a star.
Its now works a treat - l was trying to enter the whole of your code in the criteria of the Sales_ID.

Thanks again for your help.
Regards
Keith
Any time Keith, as previously stated, there may be a better solution. I'll ask a couple of the other Members who are more 'SQL Savy' than I to take a look.
Nov 16 '08 #6
FishVal
2,653 Expert 2GB
Hello, all.

Well ... Actually the same could be done with SQL for less money. ;)

Expand|Select|Wrap|Line Numbers
  1. SELECT tblSales.SOP_Number, tblSales.SOP_Line, First(tblSales.Pack_Number) AS FirstOfPack_Number, First(tblSales.Sales_ID) AS FirstOfSales_ID
  2. FROM tblSales
  3. GROUP BY tblSales.SOP_Number, tblSales.SOP_Line;
  4.  
or

Expand|Select|Wrap|Line Numbers
  1. SELECT tblSales.SOP_Number, tblSales.SOP_Line, Last(tblSales.Pack_Number) AS LastOfPack_Number, Last(tblSales.Sales_ID) AS LastOfSales_ID
  2. FROM tblSales
  3. GROUP BY tblSales.SOP_Number, tblSales.SOP_Line;
  4.  
whatever you like.

@ADezii

Oh ... Now I see. You are really a hardened and shameless coder. :D

Regards,
Fish
Nov 16 '08 #7
Stewart Ross
2,545 Expert Mod 2GB
Hi. In addition to ADezii's solution here's a subquery-based SQL solution for you to try which may be more efficient in operation as it does not use domain aggregate functions.

To enter the SQL into the Access Query Editor, select the Queries tab, New, Design View, and instead of adding tables to the grid select Close in the table add dialogue. With the empty grid visible select View from the main menu, SQL View, then copy and paste the SQL below in to the SQL window. Substitute the correct name of your table for tblSales in the two places it occurs in the SQL.

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tblSALES as A 
  2. WHERE  A.Sales_ID in 
  3.   (SELECT Min(B.Sales_ID) AS MinSales_ID
  4.    FROM tblSALES as B
  5.    GROUP BY B.SOP_Number, B.SOP_Line);
Results
Expand|Select|Wrap|Line Numbers
  1. Sales_ID Pack_Number SOP_Number SOP_Line
  2.      1       3         20462       1
  3.      2       2         20465       1
  4.      3       5         20470       2
  5.      5       4         20470       5
-Stewart
Nov 16 '08 #8
ADezii
8,834 Expert 8TB
@ FishVal & Stewart Ross Inverness, thanks guys at least my hunch was right in that either of you as well as NeoPa or missinglinq would come up with a better, more efficient, solution. Always a pleasure to be in your presence!
Nov 16 '08 #9
ADezii
8,834 Expert 8TB
Kanga, I seriously hope that you decided to check back on this Thread as I had previously suggested. Two of my 'famous' colleagues, namely FishVal and Stewart Ross Inverness, have come up with better and more efficient solutions (Posts #7 and #8 respectively). If you did not check back in, well, it's your loss. Take care.
Nov 17 '08 #10
NeoPa
32,556 Expert Mod 16PB
Hello, all.

Well ... Actually the same could be done with SQL for less money. ;)

Expand|Select|Wrap|Line Numbers
  1. SELECT tblSales.SOP_Number, tblSales.SOP_Line, First(tblSales.Pack_Number) AS FirstOfPack_Number, First(tblSales.Sales_ID) AS FirstOfSales_ID
  2. FROM tblSales
  3. GROUP BY tblSales.SOP_Number, tblSales.SOP_Line;
  4.  
or

Expand|Select|Wrap|Line Numbers
  1. SELECT tblSales.SOP_Number, tblSales.SOP_Line, Last(tblSales.Pack_Number) AS LastOfPack_Number, Last(tblSales.Sales_ID) AS LastOfSales_ID
  2. FROM tblSales
  3. GROUP BY tblSales.SOP_Number, tblSales.SOP_Line;
  4.  
whatever you like.
Technically, as far as the question was asked, the first solution here is the exact match.
@ADezii

Oh ... Now I see. You are really a hardened and shameless coder. :D

Regards,
Fish
It's taken you all this time to realise that... :D

@Stewart,
There are no Domain Aggregate functions in this SQL. Aggregate functions (as here), as they are entirely handled within the SQL engine itself, within the normal flow of the data, do not suffer from the degradation of performance that can often effect the Domain Aggregate functions.
Nov 17 '08 #11
NeoPa
32,556 Expert Mod 16PB
I suppose I should add that, if the first of the duplicate records is not specifically required, but the one matching the lowest value of a particular field, then Stewart's SQL using Min() would actually be the more correct. We can only really go on the question as asked though of course.
Nov 17 '08 #12
Just like to thank everybody for the help you've given me. As this is my first time on this forum l.m greatly impressed. I'll try out the other suggestions but once again thanks.
Regards
Keith
Nov 17 '08 #13
ADezii
8,834 Expert 8TB
Technically, as far as the question was asked, the first solution here is the exact match.

It's taken you all this time to realise that... :D

@Stewart,
There are no Domain Aggregate functions in this SQL. Aggregate functions (as here), as they are entirely handled within the SQL engine itself, within the normal flow of the data, do not suffer from the degradation of performance that can often effect the Domain Aggregate functions.
Thanks for the help, NeoPa, just for the Record you are also considered a 'famous' colleague! (LOL)!
Nov 17 '08 #14
Stewart Ross
2,545 Expert Mod 2GB
@Stewart,
There are no Domain Aggregate functions in this SQL
Hi NeoPa. I was referring to the DLast and DLookup functions in ADezii's original response (post #2), not to Fish's SQL (posted as mine was being prepared) which I had not seen at the time I posted.

I agree entirely that there are no domain aggregate functions in Fish's SQL - but I did not actually claim there were.

-Stewart
Nov 17 '08 #15
NeoPa
32,556 Expert Mod 16PB
I'm sorry I misunderstood Stewart. I was a little confused I must admit. I didn't expect such a mistake from you. Your explanation makes far more sense and, once again, sorry for doubting you.

I hope my post didn't sound critical. I didn't intend it to anyway. Just wanted to clarify the position.
Nov 17 '08 #16
Stewart Ross
2,545 Expert Mod 2GB
No worries NeoPa - I was I must admit somewhat taken aback that you thought I had confused First with one of the Dxxxxx range of domain aggregates (DLookup, DCount, DSum et al). It would have been a really bad reflection on me had I not known the difference!

Anyways, no harm done at all.

I'm just glad that ADezii, Fish and I have all supplied viable solutions that provide choice for the original poster to play with and see what works best in his application.

@ADezii - Whilst my SQL skills are certainly useful I am nowhere near as accomplished a programmer as you are. I consider it a privilege to be able to assist you in some small way with a thread like this.

Regards

Stewart
Nov 17 '08 #17
ADezii
8,834 Expert 8TB
No worries NeoPa - I was I must admit somewhat taken aback that you thought I had confused First with one of the Dxxxxx range of domain aggregates (DLookup, DCount, DSum et al). It would have been a really bad reflection on me had I not known the difference!

Anyways, no harm done at all.

I'm just glad that ADezii, Fish and I have all supplied viable solutions that provide choice for the original poster to play with and see what works best in his application.

@ADezii - Whilst my SQL skills are certainly useful I am nowhere near as accomplished a programmer as you are. I consider it a privilege to be able to assist you in some small way with a thread like this.

Regards

Stewart
Thanks again Stewart, and the rest of you guys. When I am truly stuck in the mud, I know that I can always turn to the 'Fab Four'! (LOL).
Nov 17 '08 #18
NeoPa
32,556 Expert Mod 16PB
I'll be the stretchy one :D
Nov 17 '08 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: Andante.in.Blue | last post by:
I just browsed through some of my Access links when I came across the Ten Commandments of Access (http://www.mvps.org/access/tencommandments.htm). Some of the points I heartily agree with (and...
11
by: James P. | last post by:
Hello, I have a report with the Priority field is used as sort order and grouping. The problem is the data in this Priority field if sorted in ascending order is: High, Low, and Medium. How...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
7
by: manning_news | last post by:
I've got a report that's not sorting correctly. I build a SQL statement and assign it to the recordsource in the Open event, sorting the data the way the user chooses. The user can choose up to 3...
3
by: Neil Hindry | last post by:
I wonder if you can help me. I have setup an address-book database in Access XP. I have the first name & surname as separate fields. As I wanted to sort my database by surname and then by first...
21
by: KK | last post by:
How can I find out the maximum or minimum values out of many values in an array. I have an array in Access that loads upto 20 values depending on a sql query. I would like to find out the min and...
6
by: Chris Zoper | last post by:
Hello, I have a form that shows a lot of records based on a passthrough query to a SQL Server database. I noticed that the Filter and the Sort property of the form do not properly work, often...
1
by: jjjoic | last post by:
Hi, I use Access 2003 to generate the back-end data for a ColdFusion report at work. The report is sorted by a column and based on the sorting, rankings are assigned to each row(i.e. the biggest...
6
by: jsacrey | last post by:
Hello everybody, I've got a bit of a situation that I could use some guidance with if possible. I work for an auditing firm where my users audit electronic shipping data for customers to see if...
2
by: Bob Laubla | last post by:
Hello I have a very complex maketable query with many records and involving multiple VB functions which call other functions. I need this table to be sorted by the first field. But no matter what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.