ms-access


Access Row Data to Columns Based Off ID


I have a MS Access database for employee training and there is a class that each employee is required to have twice a year.
The table for that class looks like this:
EmployeeID ClassDate ClassHours
1 1/1/2011 8
1 7/31/2011 7
2 2/1/2011 8
2 8/31/2011 7
3 3/1/2011 8
3 9/30/2011 7
I want the table to be formated like this:
EmployeeID ClassDate_1 ClassHours_1 ClassDate_2 ClassHours_2
1 1/1/2011 8 7/31/2011 7
2 2/1/2011 8 8/31/2011 7
3 3/1/2011 8 9/30/2011 7
How do I write a query to move the second class date and hour fields to the same row based off the EmployeeID?
I have researched through this site and all the possible solutions appear to be overly complicated for what I am trying to acheive.
Your help is greatly appreciated.
Thanks
You will have to create a query with a sub-select
SELECT
X.EmployeeID,
X.d1 AS ClassDate_1, ec1.ClassHours AS ClassHours_1,
X.d2 AS ClassDate_2, ec2.ClassHours AS ClassHours_2
FROM
( (SELECT e.EmployeeID, Min(e.ClassDate) AS d1, Max(e.ClassDate) AS d2
FROM employee_classes AS e
GROUP BY e.EmployeeID) AS X
INNER JOIN employee_classes AS ec1
ON X.EmployeeID = ec1.EmployeeID AND X.d1 = ec1.ClassDate
)
INNER JOIN employee_classes AS ec2
ON X.EmployeeID = ec2.EmployeeID AND X.d2 = ec2.ClassDate;
Alternatively you could store the nested select as a query (let's call it query1):
SELECT e.EmployeeID, Min(e.ClassDate) AS d1, Max(e.ClassDate) AS d2
FROM employee_classes AS e
GROUP BY e.EmployeeID
and then use it in a second query
SELECT
X.EmployeeID,
X.d1 AS ClassDate_1, ec1.ClassHours AS ClassHours_1,
X.d2 AS ClassDate_2, ec2.ClassHours AS ClassHours_2
FROM
( query1 AS X
INNER JOIN employee_classes AS ec1
ON X.EmployeeID = ec1.EmployeeID AND X.d1 = ec1.ClassDate
)
INNER JOIN employee_classes AS ec2
ON X.EmployeeID = ec2.EmployeeID AND X.d2 = ec2.ClassDate;
It would be much easier if the hours were not displayed
SELECT e.EmployeeID, Min(e.ClassDate) AS ClassDate_1, Max(e.ClassDate) AS ClassDate_2
FROM employee_classes AS e
GROUP BY e.EmployeeID
Indeed there is a simpler solution, however it assumes that the table is sorted by EmployeeID and ClassDate. This assumption is not safe, as no natural sort order is guaranteed. Access can "decide" to reorganize the records in a different way at any time.
SELECT
EmployeeID,
First(ClassDate) AS ClassDate_1, First(ClassHours) AS ClassHours_1,
Last(ClassDate) AS ClassDate_2, Last(ClassHours) AS ClassHours_2
FROM
employee_classes
GROUP BY
EmployeeID
ORDER BY
EmployeeID;
Here again a sub-select can help
SELECT
EmployeeID,
First(ClassDate) AS ClassDate_1, First(ClassHours) AS ClassHours_1,
Last(ClassDate) AS ClassDate_2, Last(ClassHours) AS ClassHours_2
FROM
(SELECT * FROM employee_classes ORDER BY EmployeeID, ClassDate)
GROUP BY
EmployeeID
ORDER BY
EmployeeID;
Howsoever, either a sub-query or a second query is required.
It depends what you plan to do with the results if they are just for display here is a crosstab query that includes the ClassDate and Hours in the value cell (with the hours in brackets after the date)
TRANSFORM Last([ClassDate] & " (" & [ClassHours] & ")") AS Details
SELECT Classes.EmployeeId
FROM Classes
GROUP BY Classes.EmployeeId
PIVOT "Class " & (DCount("[ClassNumber]","[Classes]","[ClassDate]<#" & Format$([ClassDate],"dd-mmm-yyyy") & "# AND [EmployeeId]=" & [EmployeeId])+1);
this will allow for any number of classes, not just two per teacher.
output from crosstab http://www.solsup.com.au/images/classes.jpg

Related Links

Access query to find who didn't test over a range of dates
Error 6 Overflow using Access 2.0 and Access Basic
Runntime error 2115
Is there a way to protect a field so that only certain users can edit?
Format Text Field To Date In Access Query
Changing date format in access table
Sum daily totals plus grand total from multiple tables in MSSQL
Access ComboBox to TextBox
Pulling text from another text field in MS-access
Null indexed values in Access ComboBox
Access DoCmd.TransferText Specification Argument Location
Access how to see the database mapping
MS-Access users can not access linked text file
Access populate form from another table
currency with comma as a seperator not dot in access
MS Access not getting up-to-date records in multiple users environment

Categories

HOME
mockito
drivers
pycharm
q
jsp-tags
rsync
tizen-web-app
leon
infragistics
rubygems
youtube-api-v3
python-unittest
reveal.js
resize
apache-cayenne
opentracing
libtiff
connection-string
ab-testing
jqwidget
openedx
claims-based-identity
qhull
spark-jobserver
chromebook
url-scheme
wpfdatagrid
phpfox
xacml
replaceall
opentype
quote
directx-10
preg-match-all
google-qpx-express-api
noraui
protovis
avcapturesession
android-kernel
sequential
serve
serverside-rendering
jna
streamreader
greenrobot-eventbus
graphenedb
midl
sas-jmp
apple-news
sage-one
domain-model
carrot
worker-thread
directory-structure
font-size
sybase-asa
boost-preprocessor
python-cryptography
sqldf
mediaelement
parallel-data-warehouse
wdf
adxstudio-portals
underscore.js-templating
trash
fuzzy-search
websitepanel
react-native-listview
sdf
pickadate
tcpserver
spim
qdialog
spidermonkey
sysinternals
createprocessasuser
dd
zend-route
fluentautomation
mysql-error-1062
ccss
enterprisedb
specification-pattern
quickdialog
gdata-api
dice
whoosh
blackberry-playbook
joyent
disclosure
revisions
meego
photoshop-cs4
data-driven
sustainable-pace
geneva-server
rtml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App