473,698 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 FirstOfAuthoriz ed
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 FirstOfAvailabl e
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 1489
On Jun 20, 10:45 am, Radu <cuca_macaii2.. .@yahoo.comwrot e:
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 FirstOfAuthoriz ed
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 FirstOfAvailabl e
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...@ya hoo.com.au wrote:
On Jun 20, 10:45 am, Radu <cuca_macaii2.. .@yahoo.comwrot e:


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 FirstOfAuthoriz ed
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 FirstOfAvailabl e
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
5645
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. Simply, I'm including one calcualtion from a separate table/query. It sums the total units sold to date by ProductID number and is used in other select queries to perform various calculations. Perhaps there is an advantage in working with a maximum...
3
1419
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, obviously): SystemNumber SystemNumberMAX SystemName SystemKey 9007404 Windows Test 1 A 9007405 Windows Test 1 A 9007406 Windows Test 1 A
3
376
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
4843
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 SalesManName AT Alan Time
3
1308
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 the last auction, I created this query: SELECT Sum(Lots.HighBidAmount) AS SumOfHighBidAmount, Lots.HighBidderNumber FROM Lots,
5
9425
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 query it without making a table out of query 1. I can't find a median function in the "Total" field, so is there so way to make an expression to calculate the median of the orignial data from query 1 in my new query? Also, what does name by...
2
3191
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
6231
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 choosing the most efficient way to retrieve this data based upon the evaluation of a number of different criteria. The CBO bases optimization choices on pre-gathered table and index statistics while the RBO makes it's decisions based on a set of ...
4
1446
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 the following (ignoring all irrelevant fields) TABLE 1
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.