473,320 Members | 2,122 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,320 software developers and data experts.

SQL query to select latest crime for each criminal

Hi,

I have created an MS Access database containing the following tables:

Crime
CrimeCommitted
Criminal
CriminalOrganization
CriminalType
HideoutType

The Criminal table is the parent table that contains a one-to-many relationship with the CrimeCommitted Table (a criminal can commit many crimes). I've sorted the CrimeCommitted table in descending Date (that the crime was committed) so that the latest crime is at the top of the table.

What i need to do is to write a query to select only the lastest crime committed by a criminal. Then i will be retrieving this database from a Crystal Report to view the lastest crime for each criminal. I've tried using the SELECT DISTINCT statement

SELECT DISTINCT CriminalID FROM CrimeCommitted

and it returns each CriminalID once. But how can i write a query to retrieve only the lastest crime for each criminal?
Apr 9 '07 #1
3 2904
ADezii
8,834 Expert 8TB
Hi,

I have created an MS Access database containing the following tables:

Crime
CrimeCommitted
Criminal
CriminalOrganization
CriminalType
HideoutType

The Criminal table is the parent table that contains a one-to-many relationship with the CrimeCommitted Table (a criminal can commit many crimes). I've sorted the CrimeCommitted table in descending Date (that the crime was committed) so that the latest crime is at the top of the table.

What i need to do is to write a query to select only the lastest crime committed by a criminal. Then i will be retrieving this database from a Crystal Report to view the lastest crime for each criminal. I've tried using the SELECT DISTINCT statement

SELECT DISTINCT CriminalID FROM CrimeCommitted

and it returns each CriminalID once. But how can i write a query to retrieve only the lastest crime for each criminal?
Expand|Select|Wrap|Line Numbers
  1. SELECT Criminal.CriminalID, Criminal.Name, Max(CrimeCommitted.Date) AS Last_Crime_Committed
  2. FROM Criminal INNER JOIN CrimeCommitted ON Criminal.CriminalID = CrimeCommitted.CriminalID
  3. GROUP BY Criminal.CriminalID, Criminal.Name;
Apr 9 '07 #2
Expand|Select|Wrap|Line Numbers
  1. SELECT Criminal.CriminalID, Criminal.Name, Max(CrimeCommitted.Date) AS Last_Crime_Committed
  2. FROM Criminal INNER JOIN CrimeCommitted ON Criminal.CriminalID = CrimeCommitted.CriminalID
  3. GROUP BY Criminal.CriminalID, Criminal.Name;

Thanks for your reply ADezii!
Apr 9 '07 #3
ADezii
8,834 Expert 8TB
Thanks for your reply ADezii!
Glad I could help you, Penny111.
Apr 9 '07 #4

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

Similar topics

9
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...
2
by: Dom | last post by:
I need to run a query based on a query in Access. The second query has a number of conditions which all work well, but there is one more contition I need to set to make it run properly. the...
6
by: Larry R Harrison Jr | last post by:
I have a database I'm designing in Access 97. I have a custom field in a query which looks in {Table of Documents} and shows them all. It then needs a "latest revision number," stored in another...
7
by: Scott Frankel | last post by:
Still too new to SQL to have run across this yet ... How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color...
7
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...
24
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...
4
by: jim | last post by:
I am working on a database to save information about jobs i have to complete and which parts of jobs must be completed by what dates I have two tables Jobs(primary key JobID) which is linked via a...
1
by: Jordan M. | last post by:
Hi, Hoping to get some help modifying the following query that I have... TABLE: NAMES ID, FirstName, LastName TABLE: EMAILS ID,LinkID,Email,LastUpdateDate
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.