Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 tables within an MS Access database:

A VEHICLES table with columns –
Make
Registration_No
Driver
Insurance
Depreciation


A VEHICLE_ALLOCATION table with columns –
Date
Registration_No
Project

The VEHICLES table is a master vehicle listing.
The VEHICLE_ALLOCATION table is a table containing the date that a given vehicle as used on a given project.

I need to produce a report showing how many days a vehicle was used on a given project over a period of a month.
I need to select DISTINCT Registration_No from VEHICLE_ALLOCATION (to get a list of all the vehicles that were used) and with an INNER JOIN all the details about the vehicle from VEHICLES WHERE the VEHICLE_ALLOCATION.DATE is between the 1st and last days of a given month.
There may be, say, 50 vehicles listed in the VEHICLES table but only 10 could have been allocated to projects over a period of a month.
I have been trying for hours to construct a single SQL statement that will return a list of all the VEHICLE details based on a DISTINCT Registration_No obtained from VEHICLE_ALLOCATION.

I can't post any sample code because I have tried so many variations (with zero success) and I wouldn't know which one to post.

Is such a statement constructable? Help anyone?

Thanks in advance.
Posted
Comments
nilesh sawardekar 17-Jul-14 10:10am    
which are the columns your looking in report?
Darrell de Wet 17-Jul-14 10:14am    
Hi Nilesh. All the columns in VEHICLES

Well i have tried like this, i used temp tables for sql server, i am not sure would it gonna be same in ms access

SQL
CREATE TABLE #VEHICLES(
	Make VARCHAR(100),
	Registration_No VARCHAR(100),
	Driver VARCHAR(100),
	Depreciation VARCHAR(100)
)
CREATE TABLE #VEHICLE_ALLOCATION(
	Date DATE,
	Registration_No VARCHAR(100),
	Project VARCHAR(100)
)

SELECT *
	FROM #VEHICLES
SELECT *
	FROM #VEHICLE_ALLOCATION

DROP TABLE #VEHICLES
DROP TABLE #VEHICLE_ALLOCATION

SELECT MONTH(GETDATE())
SELECT DAY(GETDATE())


/*Main query*/
SELECT *
	FROM #VEHICLES AS Vehicales
	INNER JOIN 
	(
		SELECT Registration_No, Project, COUNT(*) AS TotalDays
			FROM #VEHICLE_ALLOCATION
			/*Add the conditons*/
			WHERE MONTH(Date) = 1
			AND (DAY(Date) <= 30 AND DAY(Date) >= 1)
			GROUP BY Registration_No, Project
			
	) AS Allocations
	ON Vehicales.Registration_No = Allocations.Registration_No


remove the # signs from the Main query.
if you want to add allocation date also that a different story.
 
Share this answer
 
v2
Comments
Darrell de Wet 20-Jul-14 7:46am    
Brilliant - thanks very much.
You need to properly define join rules. Please, read this article to understand the difference between different types of joins: Visual Representation of SQL Joins[^].
 
Share this answer
 
Comments
Darrell de Wet 18-Jul-14 1:53am    
A very good article - thanks for the pointer.
Maciej Los 18-Jul-14 16:07pm    
You're welcome ;)
Can you accept my answer - formally - to remove the question from unanswered list?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900