count from multiple tables

  • Thread starter Thread starter Kay
  • Start date Start date
K

Kay

Hi
Im creating a query for my chart to lookup a count of records from 3
different tables using the Driver ID field. I have got the following
code but it comes up with the following error 'You tried to execute a
query that does not include the specified expression 'Driver ID' as
part of an aggregate funtion'. My code is a follows:

SELECT Count(*) AS Expr1
FROM (SELECT Count(*) AS Expr1, [Driver ID]
FROM [Current Jobs]


UNION ALL
SELECT COUNT(*) AS Expr1, [Driver ID]
FROM [Bookings]


UNION ALL
SELECT COUNT(*) AS Expr1, [Driver ID]
FROM [Contract Jobs]) AS query1


WHERE ((([Date]) Between [From Date] And [To Date]))
GROUP BY [Driver ID]
ORDER BY [Driver ID];


Any help will be much appreciated
 
First, Driver id is not selected in your top query to group by.
Second Driver Id I believe needs to have a group by in all of your sub
queries
Third, I think you actually want to sum the top level, not count

SELECT Sum(Expr1) AS Expr1, [Driver ID]
FROM
(
SELECT Count(*) AS Expr1, [Driver ID]
FROM [Current Jobs]
WHERE ((([Date]) Between [From Date] And [To Date]))
GROUP BY [Driver ID]

UNION ALL
SELECT COUNT(*) AS Expr1, [Driver ID]
FROM [Bookings]
WHERE ((([Date]) Between [From Date] And [To Date]))
GROUP BY [Driver ID]

UNION ALL
SELECT COUNT(*) AS Expr1, [Driver ID]
FROM [Contract Jobs]
WHERE ((([Date]) Between [From Date] And [To Date]))
GROUP BY [Driver ID]
)
GROUP BY [Driver ID]
ORDER BY [Driver ID];
 
Hi
Doesnt the sum option give you a total. The driver ID refers to many
drivers. So I need to display each driver ID and a number next to it
showing the no of records booked under that ID

Any Suggestions
 
The sub queries have already done the counting, the top query should just
add these up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

charts query 1
How can I count records from many tables 3
Select query records between dates 10
Dates 2
trying to create a sample table with 31,000,000 rows 4
input date in query 1
Query Input 1
Counting records 1

Back
Top