473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SQL Query Question - how to - minimum # of steps (Access)


Hi. I have an 'Attendance' table like this:

PIN Year Category Days
1 2006 Authorized 1
1 2006 Available 2
1 2006 Personal 3
2 2006 Authorized 4
2 2006 Available 5
2 2006 Personal 6
3 2006 Authorized 7
3 2006 Available 8
3 2006 Personal 9
4 2006 Authorized 10
4 2006 Available 11
4 2006 Personal 12
1 2007 Authorized 13
1 2007 Available 14
1 2007 Personal 15
2 2007 Authorized 16
2 2007 Available 17
2 2007 Personal 18
3 2007 Authorized 19
3 2007 Available 20
3 2007 Personal 21
4 2007 Authorized 22
4 2007 Available 23
4 2007 Personal 24
I need to sum the days by PIN, Year and Category (that's easy...) AND
obtain a layout like this:

PIN Auth 2006 Avail 2006 Pers 2006 Auth 2007 Avail 2007 Pers 2007
1 1 2 3 13 14 15
2 4 5 6 16 17 18
3 7 8 9 19 20 21
4 10 11 12 22 23 24

How can I do this by queries without writing too many intermediate
steps ?
What I have done is this (5 queries, 2, 3, and 4 building on top of 1,
and 5 building on 2, 3, 4).

1 = Table1_Crosstab:

TRANSFORM Sum(Table1.Days) AS SumOfDays
SELECT Table1.PIN, Table1.Year
FROM Table1
GROUP BY Table1.PIN, Table1.Year
PIVOT Table1.Category;

Then, based on that,

2 = Authorized:

TRANSFORM First([1 = Table1_Crosstab].Authorized) AS FirstOfAuthorized
SELECT [1 = Table1_Crosstab].PIN
FROM [1 = Table1_Crosstab]
GROUP BY [1 = Table1_Crosstab].PIN
PIVOT [1 = Table1_Crosstab].Year;

3 = Available:

TRANSFORM First([1 = Table1_Crosstab].Available) AS FirstOfAvailable
SELECT [1 = Table1_Crosstab].PIN
FROM [1 = Table1_Crosstab]
GROUP BY [1 = Table1_Crosstab].PIN
PIVOT [1 = Table1_Crosstab].Year;

and

4 = Personal:

TRANSFORM First([1 = Table1_Crosstab].Personal) AS FirstOfPersonal
SELECT [1 = Table1_Crosstab].PIN
FROM [1 = Table1_Crosstab]
GROUP BY [1 = Table1_Crosstab].PIN
PIVOT [1 = Table1_Crosstab].Year;

and finally

5 = All

SELECT [2 = Authorized].PIN, [2 = Authorized].[2006] AS [Auth 2006],
[3 = Available].[2006] AS [Avail 2006], [4 = Personal].[2006] AS [Pers
2006], [2 = Authorized].[2007] AS [Auth 2007], [3 = Available].[2007]
AS [Avail 2007], [4 = Personal].[2007] AS [Pers 2007]
FROM ([2 = Authorized] INNER JOIN [3 = Available] ON [2 =
Authorized].PIN = [3 = Available].PIN) INNER JOIN [4 = Personal] ON [3
= Available].PIN = [4 = Personal].PIN;

It works, but... I am sure that this is an awkward way of doing it. Is
there any other, more elegant, way, please ? Besides, what if I had
not 3, but 15 categories, for example ????

Thanks a lot for your time reading this, Alex

Jun 20 '07 #1
2 1479
On Jun 20, 10:45 am, Radu <cuca_macaii2...@yahoo.comwrote:
Hi. I have an 'Attendance' table like this:

PIN Year Category Days
1 2006 Authorized 1
1 2006 Available 2
1 2006 Personal 3
2 2006 Authorized 4
2 2006 Available 5
2 2006 Personal 6
3 2006 Authorized 7
3 2006 Available 8
3 2006 Personal 9
4 2006 Authorized 10
4 2006 Available 11
4 2006 Personal 12
1 2007 Authorized 13
1 2007 Available 14
1 2007 Personal 15
2 2007 Authorized 16
2 2007 Available 17
2 2007 Personal 18
3 2007 Authorized 19
3 2007 Available 20
3 2007 Personal 21
4 2007 Authorized 22
4 2007 Available 23
4 2007 Personal 24

I need to sum the days by PIN, Year and Category (that's easy...) AND
obtain a layout like this:

PIN Auth 2006 Avail 2006 Pers 2006 Auth 2007 Avail 2007 Pers 2007
1 1 2 3 13 14 15
2 4 5 6 16 17 18
3 7 8 9 19 20 21
4 10 11 12 22 23 24

How can I do this by queries without writing too many intermediate
steps ?

What I have done is this (5 queries, 2, 3, and 4 building on top of 1,
and 5 building on 2, 3, 4).

1 = Table1_Crosstab:

TRANSFORM Sum(Table1.Days) AS SumOfDays
SELECT Table1.PIN, Table1.Year
FROM Table1
GROUP BY Table1.PIN, Table1.Year
PIVOT Table1.Category;

Then, based on that,

2 = Authorized:

TRANSFORM First([1 = Table1_Crosstab].Authorized) AS FirstOfAuthorized
SELECT [1 = Table1_Crosstab].PIN
FROM [1 = Table1_Crosstab]
GROUP BY [1 = Table1_Crosstab].PIN
PIVOT [1 = Table1_Crosstab].Year;

3 = Available:

TRANSFORM First([1 = Table1_Crosstab].Available) AS FirstOfAvailable
SELECT [1 = Table1_Crosstab].PIN
FROM [1 = Table1_Crosstab]
GROUP BY [1 = Table1_Crosstab].PIN
PIVOT [1 = Table1_Crosstab].Year;

and

4 = Personal:

TRANSFORM First([1 = Table1_Crosstab].Personal) AS FirstOfPersonal
SELECT [1 = Table1_Crosstab].PIN
FROM [1 = Table1_Crosstab]
GROUP BY [1 = Table1_Crosstab].PIN
PIVOT [1 = Table1_Crosstab].Year;

and finally

5 = All

SELECT [2 = Authorized].PIN, [2 = Authorized].[2006] AS [Auth 2006],
[3 = Available].[2006] AS [Avail 2006], [4 = Personal].[2006] AS [Pers
2006], [2 = Authorized].[2007] AS [Auth 2007], [3 = Available].[2007]
AS [Avail 2007], [4 = Personal].[2007] AS [Pers 2007]
FROM ([2 = Authorized] INNER JOIN [3 = Available] ON [2 =
Authorized].PIN = [3 = Available].PIN) INNER JOIN [4 = Personal] ON [3
= Available].PIN = [4 = Personal].PIN;

It works, but... I am sure that this is an awkward way of doing it. Is
there any other, more elegant, way, please ? Besides, what if I had
not 3, but 15 categories, for example ????

Thanks a lot for your time reading this, Alex

In Access SQL, this ought to do it, but it will fail if you have so
many categories and/or years in the data that you run up against the
limit of 255 columns:

TRANSFORM Sum(attendance.Days) AS SumOfDays
SELECT attendance.PIN
FROM attendance
GROUP BY attendance.PIN
PIVOT [Category] & " " & [year];
Jun 21 '07 #2
On Jun 21, 3:36 am, helenwhee...@yahoo.com.au wrote:
On Jun 20, 10:45 am, Radu <cuca_macaii2...@yahoo.comwrote:


Hi. I have an 'Attendance' table like this:
PIN Year Category Days
1 2006 Authorized 1
1 2006 Available 2
1 2006 Personal 3
2 2006 Authorized 4
2 2006 Available 5
2 2006 Personal 6
3 2006 Authorized 7
3 2006 Available 8
3 2006 Personal 9
4 2006 Authorized 10
4 2006 Available 11
4 2006 Personal 12
1 2007 Authorized 13
1 2007 Available 14
1 2007 Personal 15
2 2007 Authorized 16
2 2007 Available 17
2 2007 Personal 18
3 2007 Authorized 19
3 2007 Available 20
3 2007 Personal 21
4 2007 Authorized 22
4 2007 Available 23
4 2007 Personal 24
I need to sum the days by PIN, Year and Category (that's easy...) AND
obtain a layout like this:
PIN Auth 2006 Avail 2006 Pers 2006 Auth 2007 Avail 2007 Pers 2007
1 1 2 3 13 14 15
2 4 5 6 16 17 18
3 7 8 9 19 20 21
4 10 11 12 22 23 24
How can I do this by queries without writing too many intermediate
steps ?
What I have done is this (5 queries, 2, 3, and 4 building on top of 1,
and 5 building on 2, 3, 4).
1 = Table1_Crosstab:
TRANSFORM Sum(Table1.Days) AS SumOfDays
SELECT Table1.PIN, Table1.Year
FROM Table1
GROUP BY Table1.PIN, Table1.Year
PIVOT Table1.Category;
Then, based on that,
2 = Authorized:
TRANSFORM First([1 = Table1_Crosstab].Authorized) AS FirstOfAuthorized
SELECT [1 = Table1_Crosstab].PIN
FROM [1 = Table1_Crosstab]
GROUP BY [1 = Table1_Crosstab].PIN
PIVOT [1 = Table1_Crosstab].Year;
3 = Available:
TRANSFORM First([1 = Table1_Crosstab].Available) AS FirstOfAvailable
SELECT [1 = Table1_Crosstab].PIN
FROM [1 = Table1_Crosstab]
GROUP BY [1 = Table1_Crosstab].PIN
PIVOT [1 = Table1_Crosstab].Year;
and
4 = Personal:
TRANSFORM First([1 = Table1_Crosstab].Personal) AS FirstOfPersonal
SELECT [1 = Table1_Crosstab].PIN
FROM [1 = Table1_Crosstab]
GROUP BY [1 = Table1_Crosstab].PIN
PIVOT [1 = Table1_Crosstab].Year;
and finally
5 = All
SELECT [2 = Authorized].PIN, [2 = Authorized].[2006] AS [Auth 2006],
[3 = Available].[2006] AS [Avail 2006], [4 = Personal].[2006] AS [Pers
2006], [2 = Authorized].[2007] AS [Auth 2007], [3 = Available].[2007]
AS [Avail 2007], [4 = Personal].[2007] AS [Pers 2007]
FROM ([2 = Authorized] INNER JOIN [3 = Available] ON [2 =
Authorized].PIN = [3 = Available].PIN) INNER JOIN [4 = Personal] ON [3
= Available].PIN = [4 = Personal].PIN;
It works, but... I am sure that this is an awkward way of doing it. Is
there any other, more elegant, way, please ? Besides, what if I had
not 3, but 15 categories, for example ????
Thanks a lot for your time reading this, Alex

In Access SQL, this ought to do it, but it will fail if you have so
many categories and/or years in the data that you run up against the
limit of 255 columns:

TRANSFORM Sum(attendance.Days) AS SumOfDays
SELECT attendance.PIN
FROM attendance
GROUP BY attendance.PIN
PIVOT [Category] & " " & [year];- Hide quoted text -

- Show quoted text -

Thank you for your time..... I had a really difficult problem to solve
by queries, and this transposition thing was only part of it. It's
solved now, and your help was honestly appreciated.

Again, thanks a lot :-))
Alex

Jun 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Rolan | last post by:
There must be a way to enhance the performance of a query, or find a plausible workaround, but I seem to be hitting a wall. I have tried a few tweaks, however, there has been no improvement. ...
3
by: cect425mike | last post by:
I'm trying to design and write a little module (or macro even) for someone in my office, but I'm having some trouble. I have the following database structure (Note: just some sample data,...
3
by: c tom | last post by:
id name location 1 tom new york 2 jeny sicago 3 tom new york 4 luca sidney 5 luca sidney i want to make below table using query. id name location
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
3
by: tbone | last post by:
I'd like to have a way to add bidders who have won at least $1000 in the last auction to my mailing list. I'm having trouble with the update step. To find bidders who have won at least $1000 in...
5
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
2
by: SQL Server Questions | last post by:
Environment: Server1 (Local) OS Windows 2000 Server SQL Server 2000 Server2 (Remote) OS Windows 2003 Server SQL Server 2000 (Both with most recent service packs)
1
by: Vinod Sadanandan | last post by:
A Roadmap To Query Tuning ============================ For each SQL statement, there are different approaches that could be used to retrieve the required data. Optimization is the process of...
4
natalie99
by: natalie99 | last post by:
Hi All :) I am trying to stipulate rules for the return of a pricing value in Access. I know this should be very easy, I simply cannot seem to make my expressions work! The two tables have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.