473,387 Members | 1,766 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,387 software developers and data experts.

SQL Query to transpose data from rows into columns

Hi all,

I have a table with two columns, labeled Year and Loss. In the Year
field, I have the numbers 1 to 10,000, each which can or cannot
repeat. In the Loss column, i have numbers corresponding to the
Years...for example:

Year, Loss
1, 568
1, 621
1, 358
1, 7888
2, 2689
2, 6563
2, 15
3, 983
3, 146
3, 258
3, 852
4, 96
5, 87
5, 32

So, you see, Year 1 can have four losses, Year 2 can have three
losses, etc.

Now, here's where I need help as I'm not sure what to do given that I
am a beginner at SQl and learning: I want to have just one row for
each Year. So, for Year 1, I would like to have four extra columns,
and for Year 2, I would like to have three columns created to hold
loss numbers so they display in a row - please see below for example:

Year, Loss1, Loss2, Loss3, Loss4, Loss5, etc.
1, 568, 621, 358, 7888
2, 2689, 6563, 15
3, 983, 146, 258, 852
4, 96
5, 87, 32

I hope that the examples helped in clarifying what kind of Query I
need. I believe I can have up to 30 losses for each year. Please help
if you can - I truly appreciate it and thank you in advance!
Aug 28 '08 #1
4 23025
Hello Haas,

A crosstab query will do what you want. Go to queries and click New. One of
your choices will be a crosstab query. Choose it and follow the wizard.

Steve
"Haas C" <ha*****@yahoo.comwrote in message
news:a3**********************************@26g2000h sk.googlegroups.com...
Hi all,

I have a table with two columns, labeled Year and Loss. In the Year
field, I have the numbers 1 to 10,000, each which can or cannot
repeat. In the Loss column, i have numbers corresponding to the
Years...for example:

Year, Loss
1, 568
1, 621
1, 358
1, 7888
2, 2689
2, 6563
2, 15
3, 983
3, 146
3, 258
3, 852
4, 96
5, 87
5, 32

So, you see, Year 1 can have four losses, Year 2 can have three
losses, etc.

Now, here's where I need help as I'm not sure what to do given that I
am a beginner at SQl and learning: I want to have just one row for
each Year. So, for Year 1, I would like to have four extra columns,
and for Year 2, I would like to have three columns created to hold
loss numbers so they display in a row - please see below for example:

Year, Loss1, Loss2, Loss3, Loss4, Loss5, etc.
1, 568, 621, 358, 7888
2, 2689, 6563, 15
3, 983, 146, 258, 852
4, 96
5, 87, 32

I hope that the examples helped in clarifying what kind of Query I
need. I believe I can have up to 30 losses for each year. Please help
if you can - I truly appreciate it and thank you in advance!

Aug 28 '08 #2
On Aug 28, 2:57*pm, "Steve" <nonse...@nomsense.comwrote:
Hello Haas,

A crosstab query will do what you want. Go to queries and click New. One of
your choices will be a crosstab query. Choose it and follow the wizard.

Steve

"Haas C" <haas...@yahoo.comwrote in message

news:a3**********************************@26g2000h sk.googlegroups.com...
Hi all,
I have a table with two columns, labeled Year and Loss. In the Year
field, I have the numbers 1 to 10,000, each which can or cannot
repeat. In the Loss column, i have numbers corresponding to the
Years...for example:
Year, Loss
1, 568
1, 621
1, 358
1, 7888
2, 2689
2, 6563
2, 15
3, 983
3, 146
3, 258
3, 852
4, 96
5, 87
5, 32
So, you see, Year 1 can have four losses, Year 2 can have three
losses, etc.
Now, here's where I need help as I'm not sure what to do given that I
am a beginner at SQl and learning: I want to have just one row for
each Year. So, for Year 1, I would like to have four extra columns,
and for Year 2, I would like to have three columns created to hold
loss numbers so they display in a row - please see below for example:
Year, Loss1, Loss2, Loss3, Loss4, Loss5, etc.
1, 568, 621, 358, 7888
2, 2689, 6563, 15
3, 983, 146, 258, 852
4, 96
5, 87, 32
I hope that the examples helped in clarifying what kind of Query I
need. I believe I can have up to 30 losses for each year. Please help
if you can - I truly appreciate it and thank you in advance!- Hide quoted text -

- Show quoted text -
That actually doesn't work as I get a "too many headers" error. Can
someone please actually show me how to do this rather than tell me to
use a wizard? I appreciate the help Steve but am against a time crunch
and can't figure this out.

Thanks!
Aug 28 '08 #3
Haas,

I created a table named TblLossYer with fields MyYear, Loss and ColTitle. I
put your data in the MyYear and Loss fields. I manually put Loss 1, Loss
2,etc in ColTitle field. Here is the crosstab SQL:

TRANSFORM First(TblYearLoss.Loss) AS FirstOfLoss
SELECT TblYearLoss.MyYear
FROM TblYearLoss
GROUP BY TblYearLoss.MyYear
PIVOT TblYearLoss.ColTitle;

You will need to figure out a function to automatically create the value in
ColTitle and then use a calculated field in a query to automatically set the
value for ColTitle. When you do this, base your crosstab query on that query
and delete the ColTitle field in your table.

Steve
rl****@penn.com


"Haas C" <ha*****@yahoo.comwrote in message
news:9f**********************************@34g2000h sh.googlegroups.com...
On Aug 28, 2:57 pm, "Steve" <nonse...@nomsense.comwrote:
Hello Haas,

A crosstab query will do what you want. Go to queries and click New. One
of
your choices will be a crosstab query. Choose it and follow the wizard.

Steve

"Haas C" <haas...@yahoo.comwrote in message

news:a3**********************************@26g2000h sk.googlegroups.com...
Hi all,
I have a table with two columns, labeled Year and Loss. In the Year
field, I have the numbers 1 to 10,000, each which can or cannot
repeat. In the Loss column, i have numbers corresponding to the
Years...for example:
Year, Loss
1, 568
1, 621
1, 358
1, 7888
2, 2689
2, 6563
2, 15
3, 983
3, 146
3, 258
3, 852
4, 96
5, 87
5, 32
So, you see, Year 1 can have four losses, Year 2 can have three
losses, etc.
Now, here's where I need help as I'm not sure what to do given that I
am a beginner at SQl and learning: I want to have just one row for
each Year. So, for Year 1, I would like to have four extra columns,
and for Year 2, I would like to have three columns created to hold
loss numbers so they display in a row - please see below for example:
Year, Loss1, Loss2, Loss3, Loss4, Loss5, etc.
1, 568, 621, 358, 7888
2, 2689, 6563, 15
3, 983, 146, 258, 852
4, 96
5, 87, 32
I hope that the examples helped in clarifying what kind of Query I
need. I believe I can have up to 30 losses for each year. Please help
if you can - I truly appreciate it and thank you in advance!- Hide
quoted text -

- Show quoted text -
That actually doesn't work as I get a "too many headers" error. Can
someone please actually show me how to do this rather than tell me to
use a wizard? I appreciate the help Steve but am against a time crunch
and can't figure this out.

Thanks!
Aug 29 '08 #4
On Thu, 28 Aug 2008 13:44:05 -0700 (PDT), Haas C <ha*****@yahoo.com>
wrote:
>On Aug 28, 2:57*pm, "Steve" <nonse...@nomsense.comwrote:
>Hello Haas,

A crosstab query will do what you want. Go to queries and click New. One of
your choices will be a crosstab query. Choose it and follow the wizard.

Steve

"Haas C" <haas...@yahoo.comwrote in message

news:a3**********************************@26g2000 hsk.googlegroups.com...
Hi all,
I have a table with two columns, labeled Year and Loss. In the Year
field, I have the numbers 1 to 10,000, each which can or cannot
repeat. In the Loss column, i have numbers corresponding to the
Years...for example:
Year, Loss
1, 568
1, 621
1, 358
1, 7888
2, 2689
2, 6563
2, 15
3, 983
3, 146
3, 258
3, 852
4, 96
5, 87
5, 32
So, you see, Year 1 can have four losses, Year 2 can have three
losses, etc.
Now, here's where I need help as I'm not sure what to do given that I
am a beginner at SQl and learning: I want to have just one row for
each Year. So, for Year 1, I would like to have four extra columns,
and for Year 2, I would like to have three columns created to hold
loss numbers so they display in a row - please see below for example:
Year, Loss1, Loss2, Loss3, Loss4, Loss5, etc.
1, 568, 621, 358, 7888
2, 2689, 6563, 15
3, 983, 146, 258, 852
4, 96
5, 87, 32
I hope that the examples helped in clarifying what kind of Query I
need. I believe I can have up to 30 losses for each year. Please help
if you can - I truly appreciate it and thank you in advance!- Hide quoted text -

- Show quoted text -

That actually doesn't work as I get a "too many headers" error. Can
someone please actually show me how to do this rather than tell me to
use a wizard? I appreciate the help Steve but am against a time crunch
and can't figure this out.

Thanks!
Like so many tasks in this area, there are lots of approaches. I
suspect someone will provide a solution that is substantially
"slicker" than this one, but in case not - this one works exactly as
you've requested.

It uses a small function and then calls that function from a query.
We're assuming the table is named 'years' and the attributes are
'yearnum' and 'loss'.

' -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Public Function Cram(Y As Long)
Dim SQL As String
Dim L

SQL = "Select Loss from years where Yearnum=" & Y
' watch out for line wrap here-
L = CurrentProject.Connection.Execute(SQL).GetString(a dClipString, ,
, ",", "")
Cram = Left(L, Len(L) - 1)

End Function
' -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Then use the function in a query:

' -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
SELECT [yearnum] & ", " & Cram([yearnum]) AS Haas
FROM years
GROUP BY [yearnum] & ", " & Cram([yearnum]);
' -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

Sidenote - Year is a reserved word in Access, it shouldn't be used for
an attribute name.

HTH,
Arch
Aug 29 '08 #5

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

Similar topics

4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
1
by: Chris Smith | last post by:
Experience Posters, Sorry if this is not the right group to post this question. He is my issue; Is there a way without the use of 3rd party controls, to transpose the rows of a datatable to...
7
by: Leszek Gruszka | last post by:
I wrote an aplication that write something into tableA in sql2000. I want to write the same, but transposed into tableB. Someone can help me? Any example? *** Sent via Developersdex...
8
by: Leszek Gruszka | last post by:
I wrote code, that fill TableA with records by executenonquery. But i want to have second TableB, that will be transposed TableA. My code: Public Sub SQL_Wpis() Dim sSQL As String sSQL =...
1
by: Radu | last post by:
Hi. I have a table with the following *COLUMNS*: PIN# # of weeks when Overtime 8 Overtime Week 1 Overtime Week 2 Overtime Week 3 .. ..
8
by: m.wanstall | last post by:
Hi All, This is similar to a question I asked earlier however this is following a more "correct" way of doing things. I have normalised and summarised an Exchange addressbook (a few thousand...
2
by: erbrose | last post by:
Hello All! Hoping some folks could help me optimize and or choose the best route to do this process. First off, here is what I am trying to achieve. I have a (fairly large) table of ~34million rows...
5
by: jenniferhelen | last post by:
I have been searching threads for a while and found the instructions listed below many times, however when I get to step 6 and select to save, I always receive the following error, "The information...
12
by: jenniferhelen | last post by:
I am working with a query that has 6 columns and 101 rows; I would like to transpose the rows and columns. I have tried using a crosstab query but Access limits the row "fields" to 3 and this was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.