Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2007-09-24 : 17:02:57
|
| Hi,I would like to write a sql query in sql server 2005 to derive a table called tblResult from tblData as follows:I think I should use unpivot.tblDataNotice the dates are fieldssurname 26 Dec 27 Dec 28 Dec 02 Jan 04 Jan 05 JanBrown 100.2 12.65 43.76 3.54 98.12 56.76Jackson 32.21 98.34 45.54 2.65 65.11 78.86Peterson 32.23 65.34 88.22 8.34 12.22 87.55This is what I would like to end up withtblResultSurname Date price1 Price2Brown 26 Dec 100.2 100.2Jackson 26 Dec 32.21 32.21Peterson 26 Dec 32.23 32.23Brown 27 Dec 12.65 12.65Jackson 27 Dec 98.34 98.34Peterson 27 Dec 65.34 65.34Brown 28 Dec 43.76 43.76Jackson 28 Dec 45.54 45.54Peterson 28 Dec 88.22 88.22...Thanks |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-24 : 17:11:10
|
SELECT surname, theDate, theValue, theValue FROM (SELECT d.surname, d.theDate, d.theValue, ROW_NUMBER() OVER (PARTITION BY d.surname ORDER BY d.theValue) AS RecIDFROM (SELECT surname, '26 Dec' AS theDate, [26 Dec] FROM tblData UNION ALLSELECT surname, '27 Dec', [27 Dec] FROM tblData UNION ALLSELECT surname, '28 Dec', [28 Dec] FROM tblData UNION ALLSELECT surname, '02 Jan', [02 Jan] FROM tblData UNION ALLSELECT surname, '04 Jan', [04 Jan] FROM tblData UNION ALLSELECT surname, '05 Jan', [05 Jan] FROM tblData) AS d) AS e WHERE RecID = 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-25 : 04:20:48
|
| How about Normalization?MadhivananFailing to plan is Planning to fail |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2007-09-25 : 04:21:47
|
| Hi,I am not sure if this will be a good solution for me because the actual file that I am trying to do the pivot on is from an excel file.So the excel file has fields such as:surname 28-Dec 29-Dec 02-Jan 03-Jan ...Please note the date field goes all lthe way to 14-Sep 2007So as you can see the date fields are quite alot.Issues that I am facing:When importing the excel file data into sql server table, I get the dcolumns which are dates as F2, F3, F4... fields in sql server imported table.Other issue is:do I have to write all the date filed columns in the pivot query?Thanks |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2007-09-25 : 04:22:37
|
quote: Originally posted by madhivanan How about Normalization?MadhivananFailing to plan is Planning to fail
The source is from an excel file which is described in my previous post.Thanks |
 |
|
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2007-09-25 : 06:49:14
|
| Yes, I have been reading documentation but can not figure this out. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-25 : 07:06:22
|
quote: Originally posted by arkiboys I would like to write a sql query in sql server 2005 to derive a table called tblResult from tblData as follows:I think I should use unpivot.
Excel is not mentioned. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-25 : 07:27:59
|
quote: Originally posted by Peso
quote: Originally posted by arkiboys I would like to write a sql query in sql server 2005 to derive a table called tblResult from tblData as follows:I think I should use unpivot.
Excel is not mentioned. E 12°55'05.25"N 56°04'39.16"
by previous reply OP means the previous reply made before replying to my question MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|