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

Avoid duplication without using Indexed fields and 'no duplication'
how do i build a website using access 2010? [closed]
Is there a NotIn(“A”,“B”) function in VBA?
Access VBA: If Form Value <> NULL then run query else, end if. - not doing anything
ms-access: file already in use ERROR
Data from different records in one record using Access 2003
forcing EDIT of access backend database
How to collect input via a form and pass to report query in Access
sql select - order by but partial part of the field
SQL to ACCESS 2010
Select a value from a table in current DB and use a variable in Access VBA
maximum number of connections to network accessed Access database
access - properly hosting backend of access database
Generating consecutive invoice number in MS Access VBA, # restarting every year
Use Value in Word calculated with VBA in Access
Compile Error in VBA code in MS Access

Categories

HOME
client
magnific-popup
push-notification
nullpointerexception
relative-path
stock
objectgears
react-router
alfresco
read-eval-print-loop
q
framework7
sqlite-net-extensions
react-redux
infragistics
jxls
survey
sql-server-2016
wamp
azure-media-services
cross-validation
windows-azure-storage
uitypeeditor
firefox-webextensions
alpine
grails3
reverse-proxy
captiveportal
php-openssl
shopware
wpfdatagrid
entitlements
spring-mybatis
jaxb2
nouislider
xquery-3.0
pim
phpfreechat
normal-distribution
automake
streamreader
graphenedb
texmaker
wptoolkit
drupal-6
csound
android-cursor
angular-strap
removechild
ado.net-entity-data-model
trash
qpid
prettytensor
actionbardrawertoggle
ios4
wapiti
hsv
iad
atk4
graph-drawing
system32
npapi
kineticjs
system.reflection
phalanger
applescript-objc
googlemock
mesa
phpthumb
wp-query
yorick
braille
algebraic-data-types
openlaszlo
sitemesh
runtime.exec
tws
dataadapter
django-nonrel
hungarian-algorithm
factory-method
buildr
bigcouch
cufon
php-parser
subgurim-maps
self-extracting
yui-datatable
html-input
google-friend-connect
coda-slider
nhibernate.search
ext3
meego
firefox4
pascal-fc
zend-decorators
post-build
geneva-server
grid-system

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