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 MDB: do access MDB files have an upper size limit?
Problem evaluating NULL in an IIF statement (Access)
WM_SETFOCUS Event not fired Microsoft Access 2000
Access VBA sub with form as parameter doesn't alter the form
Where can I find MSAccess Control in Database
MS Access 2003 - Really simple query
Query to add missing rows using values from prior period
How can I know which item in a mult-select Microsoft Access listbox was clicked?
Is it possible to save year 1 to 99 as date-value in Access database?
How to bottom-align string in Access report textbox?
table joins in Access 2003 on date-time field
Tagging Records in a Continuous Form
Drilldown using unbound comboboxes with Access “Continuous Form”
vba access: each Ctl in report.controls
MS Access : Read form control value in a module's function
Access 2007 Encrypted BackEnd Slow Only With New ACCDB

Categories

HOME
java
wso2-am
google-play
getelementsbytagname
spring-jdbc
binary-tree
blueprintjs
fingerprint
networkx
medical
slick-slider
fancybox-3
nstableview
leiningen
quickfix
systemc
invantive-sql
reactcsstransitiongroup
database-replication
firefox-webextensions
custom-wordpress-pages
conemu
one-hot-encoding
microsoft-r
p-value
buildbot
centos6.5
maquette
msys2
gitignore
preg-match-all
uisplitview
commit
vsts-build-task
lxd
uiswipegesturerecognizer
wpf-controls
wixsharp
bitbucket-pipelines
hockeyapp
tasker
dism
ws-security
eclipse-scout
fedex
http-live-streaming
node-sass
scrollable
typescript1.8
integrity
yii2-extension
cudafy.net
spring-mongodb
titanium-android
specrun
objective-c-swift-bridge
messenger
ableton-live
log4c
lttng
hover-over
fadeout
pagerank
vhd
muse
mmc
geonetwork
csquery
facebook-graph-api-v2.4
markers
valueconverter
valuechangelistener
marmalade
balanced-payments
cloud-connect
gnu-smalltalk
pyhdf
c18
tinn-r
browser-detection
sublist
regsvr32
qtkit
enter
buildr
nsdatecomponents
coercion
locationlistener
recent-documents
file-comparison
nerddinner
django-notification

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