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

MSACCESS query: not part of an aggregate function
pdi spoon ms-access concat
iff command issue in MS Access
How to display the maximum value from one column in a listbox, in a textbox on the same form
access 2010 writing SQL statements
Add Working Days to Start Date to Create End Date (including Bank Holiday)
ms access input mask MM/DD/YYYY
'Microsoft.ACE.OLEDB.12.0' provider is not registered -but it is
How do I run this query?
Sending Email with Late Binding Error Through VBA
Microsoft Access 2010 VB Script and Query
Delete query for multiple tables
write an Access query have more than 1 SELECT statement
MS Access - Rank on TEXT fields
Creating / Attaching module in Access database at runtime from VB6 application
Building a query in Access to work with time

Categories

HOME
testing
clips
hp-exstream
read-eval-print-loop
bookshelf.js
paw-app
jpeg
gps
windows-server
django-imagekit
ios-charts
facebook-messenger-bot
amazon-cloudformation
dacpac
offline
dax
qt-creator
timeout
iron-router
oracle-coherence
samoa
swingx
kryo
intentfilter
csrf-protection
bootstrap-tour
format-specifiers
php-openssl
apache-commons-io
user-interaction
primitive
unboundid
asset-pipeline
accelerate-framework
amazon-kinesis-kpl
hexo
wpf-controls
bower-install
overlap
stacked
serve
swisscomdev
occlusion
webdriver-manager
optix
nomethoderror
spring-security-kerberos
isbn
reactive-cocoa-5
sas-jmp
network-flow
chain-builder
veracode
gabor-filter
lowpass-filter
nativeapplication
namecoin
django-scheduler
smart-table
root-framework
android-textview
probability-density
bonobo
database-optimization
fody
trash
contact-list
markojs
suffix-tree
phpcas
metaclass
system32
unity5.2.3
javax.mail
xc16
knuth
titanium-modules
code-access-security
yorick
valuechangelistener
xceed-datagrid
dealloc
dbconnection
ftps
jplaton
cos
buster.js
mylyn
rabl
hungarian-algorithm
sharp-repository
simba
armcc
ohm
mercurial-server
doh
spyware
genshi
servicehost
gdlib
createwindow
mozilla-prism
modelstate
filtered-index
avatar

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