473,782 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query to select one row from each group ?

In a manufacturing document control situation, a procedure revision
table named [tblProcedures] has principal columns [Name] and
[Revision] and satellite data columns [Date], [Person], [Comments]. A
unique index [Name] and [Revision] to ensure each procedure name has
only unique revision numbers.

A form has a listbox for selecting a procedure and revision for
browsing, and a checkbox 'Show all'.
When ShowAll is true the listbox rowsource can be [tblProcedures].
When ShowAll is false the listbox should only show each procedures
highest revision.

foo replaces date in this sample data

name rev foo
A 1 U
A 2 V
A 3 W
B 1 X
B 2 Y
B 3 Z
B 4 K

want

A 3 W
B 4 K

tried
select * from procs group by name having rev=max(rev)
but access97 does not like.

Feb 4 '07 #1
4 5959
try using a Totals query; in query Design view, set the Total: row in the
Rev field to Max.

hth
"Richard" <ri************ @devenezia.comw rote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
In a manufacturing document control situation, a procedure revision
table named [tblProcedures] has principal columns [Name] and
[Revision] and satellite data columns [Date], [Person], [Comments]. A
unique index [Name] and [Revision] to ensure each procedure name has
only unique revision numbers.

A form has a listbox for selecting a procedure and revision for
browsing, and a checkbox 'Show all'.
When ShowAll is true the listbox rowsource can be [tblProcedures].
When ShowAll is false the listbox should only show each procedures
highest revision.

foo replaces date in this sample data

name rev foo
A 1 U
A 2 V
A 3 W
B 1 X
B 2 Y
B 3 Z
B 4 K

want

A 3 W
B 4 K

tried
select * from procs group by name having rev=max(rev)
but access97 does not like.

Feb 4 '07 #2
On Feb 4, 4:40 am, "Richard" <richard.goo... @devenezia.comw rote:
In a manufacturing document control situation, a procedure revision
table named [tblProcedures] has principal columns [Name] and
[Revision] and satellite data columns [Date], [Person], [Comments]. A
unique index [Name] and [Revision] to ensure each procedure name has
only unique revision numbers.

A form has a listbox for selecting a procedure and revision for
browsing, and a checkbox 'Show all'.
When ShowAll is true the listbox rowsource can be [tblProcedures].
When ShowAll is false the listbox should only show each procedures
highest revision.

foo replaces date in this sample data

name rev foo
A 1 U
A 2 V
A 3 W
B 1 X
B 2 Y
B 3 Z
B 4 K

want

A 3 W
B 4 K

tried
select * from procs group by name having rev=max(rev)
but access97 does not like.
Try:

SELECT * FROM procs AS p
WHERE NOT EXISTS (
SELECT *
FROM procs AS p2
WHERE name=p.name AND rev>p.rev
)

Feb 5 '07 #3
On Feb 4, 10:12 pm, "Kevin Chambers" <KBChamb...@gma il.comwrote:
Try:

SELECT * FROM procs AS p
WHERE NOT EXISTS (
SELECT *
FROM procs AS p2
WHERE name=p.name AND rev>p.rev
)
Thanks Kevin.

Is the where not exists fundamentally better than inner joining a
subselect, such as:

SELECT * FROM procs
INNER JOIN
[ SELECT name, max(rev) AS maxRev FROM procs GROUP BY name ]. AS
sqNavel
ON
procs.name = sqNavel.name
AND
procs.rev = sqNavel.maxRev

Feb 5 '07 #4
On Feb 4, 8:41 pm, "Richard" <richard.goo... @devenezia.comw rote:
On Feb 4, 10:12 pm, "Kevin Chambers" <KBChamb...@gma il.comwrote:
Try:
SELECT * FROM procs AS p
WHERE NOT EXISTS (
SELECT *
FROM procs AS p2
WHERE name=p.name AND rev>p.rev
)

Thanks Kevin.

Is the where not exists fundamentally better than inner joining a
subselect, such as:

SELECT * FROM procs
INNER JOIN
[ SELECT name, max(rev) AS maxRev FROM procs GROUP BY name ]. AS
sqNavel
ON
procs.name = sqNavel.name
AND
procs.rev = sqNavel.maxRev
Hmm, that's a good question. I guess it depends on what you're using
the query for. Since you're using it for a list box only, speed is
probably the most important consideration. Having the subselect in
the FROM clause as you do may be faster in Access since Access seems
to be able to optimize that kind of query structure much better, but
you'd want to test them both to be sure.

What I like about using NOT EXISTS in the FROM clause is that there's
no question about the recordset's updatability. There's only one
table in the FROM clause, so it's a legitimate updatable view in SQL
Server. And from a more stylistic standpoint, I find the SQL a wee
bit cleaner and easier to read. It's clear to me when I come back to
a query I've written with NOT EXISTS that my intention was filtering,
not grouping.

KC

Feb 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3137
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
4
1744
by: techquest | last post by:
got slightly similar table. #Temp Table: name,name1,name2,exam,score A,A1,A21, A,A1,A21,math100,88 A,A1,A21,math101,56 A,A1,A21, A,A1,A21,math102,67 A,A1,A21, A,A1,A21,math104,45
4
2659
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I need to be able to do a search for specific tickets withing price ranges, different locations within the theaters, etc. etc. My problem is in the search one of the criteria is to search for a group of seats together. For example let's say...
7
2056
by: jane | last post by:
HI, I was ask to do a query to get the member active condition I had table to show the member number and active status in three month. ACC A1 A2 A3 ---- ----------- ----------- ----------- c001 1 1 1
4
3398
by: New Guy | last post by:
I'm trying to work with a system that somebody else built and I am confounded by the following problem: There is a table of payments and a table of charges. Each client has charges and payments during the month. I'd like to get the totals of the payments and of the charges for each client. When I run the following query, I get huge numbers that appear as if the join is not working correctly.
0
2341
by: rdnews | last post by:
Dear group, I got excellent help here a while back in optimizing a slow query, so I thought I'd try again with another one... I have a small table, around 3000 rows with two columns. One is a timestamp and the other is an integer. Each day, a number of entries are created, with integers ranging from 1 to 5. I need to find how many entries with the integer of 3 are created per
7
3391
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
24
19915
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every renewal in the history of the policyholder. The information is in 2 tables, policy and customer, which share the custid data. The polno changes with every renewal Renewals in 2004 would be D, 2005 S, and 2006 L. polexpdates for a given customer...
3
1753
by: Parasyke | last post by:
I have a complicated query that is giving me duplicate records for some of the returned records. Underwriter name is what I don't want to duplicate. Any clues? Thanks for any help! SELECT qryUnderwriters.UWID, qryUnderwriters.UWName, qryUnderwriters.JobTitle, qryUnderwriters.BUName, qryUnderwriters.EmployHireDate, qryUnderwriters.ApptStartDate, qryUnderwriters.TermDate, qryUnderwriters.EmployTypeCode, qryUnderwriters.Active,...
0
7497
by: djflow | last post by:
Hi! II was wondering if you can help me with SQL query.. Below 7 separated select query works fine(only when they are retrieved separately) But I want to combined them together and so that i can make report from my database on the one sheet rather than querying each select statement ( I will use MS Query from the Excel) ========================================================= 1) select convert(varchar, create_date_time, 112) as Date,...
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.