473,662 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL query to select latest crime for each criminal

7 New Member
Hi,

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

Crime
CrimeCommitted
Criminal
CriminalOrganiz ation
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 2929
ADezii
8,834 Recognized Expert Expert
Hi,

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

Crime
CrimeCommitted
Criminal
CriminalOrganiz ation
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
penny111
7 New Member
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 Recognized Expert Expert
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
3123
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...
2
18693
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 first query returns a number of data items, consisting, among other fields, of a Company_ID and a Rating_Date. For most companies, the latest value of Rating_Date is equal to a value in a seperate table (tblVariables) which logs the last time a...
6
2293
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 table named {Table of Revisions}. It naturally matches them up by linking the autoid in {Doc} with the related field in {Rev}. It then looks for a field in {Rev} called "revision number" and looks for the last one for the given Doc (linked by the...
7
6344
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 | date --------+------------ red | 2004-01-19 blue | 2004-05-24
7
3383
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
19900
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...
4
416
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 one-many relationship with a JobParts table(primary key PartID, foreign key JobID) I want to list just the next part of each particular job which is due to be completed so from my JobParts table below: PartID CompleteBy JobID
1
2661
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
8432
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
8857
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
8764
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
8546
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
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
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
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
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.