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 |
ulfber
Starting Member
3 Posts |
Posted - 2014-05-21 : 15:06:03
|
Hi All having a problem with no solution at hand:Reading data in a YYYYMM for which it has a data value (Forecast)so data in: 201201 10201202 5all up to 201212201301 11201302 6I would like to create a matrix to a screen that does : Month -> 01 02 03 2012 10 52013 11 6Tried group by, tried everything, i thinkThe final solution is to create another file with one record per year. Any help is appreciated |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2014-05-21 : 16:15:09
|
Not quite following. Please post DDL/DML* and the expected output. use code tags around any formatted text or code:*DDL/DML:executable statements like: [code]--DDLcreate table #table1 (col1 int, col2 varchar(10))--DMLinsert #table1 (col1, col2)values (1, '1'),(2,'2'),(3,'3')--expected output:col1 col2----------- ----------1 12 23 3[/code] Be One with the OptimizerTG |
 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-05-21 : 16:20:13
|
Is the format of the data for 2013 different from those of 2012? For 2012 you have six character that has year and month, for 2013, you have only year. Assuming 2013 also has month portion, use pivot operator, e.g., like this.SELECT * FROM(SELECT LEFT (YYYYMM,4) AS [Year], RIGHT(YYYYMM,2) AS [Month], DataValueFROM YourTable) sPIVOT (MAX(DataValue) FOR [Month] IN ([01],[02],[03]))P Not sure if your data types are char/varchar/int etc. or something else. This assumes varchar(nn) for the Period. Also, fill in other months that you need as columns |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2014-05-21 : 16:51:59
|
If data is int then this would work:select period/100 as [year] ,period-((period/100)*100) as [month] Be One with the OptimizerTG |
 |
|
ulfber
Starting Member
3 Posts |
Posted - 2014-05-21 : 18:06:02
|
James K I Made a FUPA and updtated the YYYYMM In my original Post. (No they are not different, and I have record for each month in the year)You say use pivot operator, could you elaborate???I do this SQL and it gives me a total per year, whereas I would like "One record per year"SELECT substr(char(ffyfpe),1,4),ffprdc ,sum( FFPDMD ) FROM sroffiWHERE ffprdc = 'BGL 97117' GROUP BY substr(char(ffyfpe),1,4), ffprdc ORDER BY substr(char(ffyfpe),1,4), ffprdTG I do not see anything you post after ":" |
 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-05-22 : 07:39:03
|
quote: Originally posted by ulfber James K I Made a FUPA and updtated the YYYYMM In my original Post. (No they are not different, and I have record for each month in the year)You say use pivot operator, could you elaborate???I do this SQL and it gives me a total per year, whereas I would like "One record per year"SELECT substr(char(ffyfpe),1,4),ffprdc ,sum( FFPDMD ) FROM sroffiWHERE ffprdc = 'BGL 97117' GROUP BY substr(char(ffyfpe),1,4), ffprdc ORDER BY substr(char(ffyfpe),1,4), ffprdTG I do not see anything you post after ":"
There must be something not quite right with your browser/reader because I am able to see TG's code, and I had posted sample code using the PIVOT operator. Here it is again, without using any code formatting blocks:SELECT * FROM(SELECT LEFT (YYYYMM,4) AS [Year], RIGHT(YYYYMM,2) AS [Month], DataValueFROM YourTable) sPIVOT (MAX(DataValue) FOR [Month] IN ([01],[02],[03]))P See more about the PIVOT operator here: http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx |
 |
|
ulfber
Starting Member
3 Posts |
Posted - 2014-05-22 : 11:52:16
|
Thanks James K I will give that a try, Still cant see TG's code. One would think that would be nice :-)HTML is OFFForum Code is ONIt says to the side in this message... Oh well I figure it out |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2014-05-22 : 13:04:52
|
Odd - never heard of someone not being able to view portions of a post. What device (and browser) are you using to view this forum?EDIT:this is all my post said:If data is int then this would work:select period/100 as [year], period-((period/100)*100) as [month]Be One with the OptimizerTG |
 |
|
|
|
|
|
|