jbound
asked on
SQL Sybase - Convert Rows to Columns
I have a numerical result for each month in the Current table. Each result is a row. I
I would like to be able to create a result set, so that the months are columns and all the relevant monthly values are in the relevant month column in one row see Target table structure (below)
Current Table Structure:
ID Month Result
1 02 9.7
2 03 6.5
3 01 3.2
Target Table Structure:
ID Jan Feb Mar
1 3.2 9.7 6.5
Can anyone give me complete answer that I can simply paste in and adapt? BTW, I am a novice at SQL.
Thanks
PS Not all the MS SQL commands like PIVOT are available in Sybase.
I would like to be able to create a result set, so that the months are columns and all the relevant monthly values are in the relevant month column in one row see Target table structure (below)
Current Table Structure:
ID Month Result
1 02 9.7
2 03 6.5
3 01 3.2
Target Table Structure:
ID Jan Feb Mar
1 3.2 9.7 6.5
Can anyone give me complete answer that I can simply paste in and adapt? BTW, I am a novice at SQL.
Thanks
PS Not all the MS SQL commands like PIVOT are available in Sybase.
ASKER
Sorry, but with my limited knowledge, I can't adapt this code as I am unsure what to substitute.
The real name of the current table is "dba"."vwResultInspTask_iD ashboards"
and the actual names of the current columns that are to be transposed are:
ID nMonth AnswerDesc
1 02 9.7
2 03 6.5
3 01 3.2
The results are not going to a table, but are simply the query result set. I hope this is clearer.
Rgds
The real name of the current table is "dba"."vwResultInspTask_iD
and the actual names of the current columns that are to be transposed are:
ID nMonth AnswerDesc
1 02 9.7
2 03 6.5
3 01 3.2
The results are not going to a table, but are simply the query result set. I hope this is clearer.
Rgds
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for new code - but a few glitches :
1. Kept stopping a 'dec', which I think is a reserved word ('decreasing'?), so changed all of the dec to 'december'.
Ran the query:
2. Next error is 'no such table as '"dba"."vwAnswerDescInspTa sk_iDashbo ards" - which is correct as the table name is "dba"."vwResultInspTask_iD ashboards" . So, changed that.
Ran it again:
3. Could not execute statement. Column 'ID' not found. Line 1, Column 1. This table is a view, so there is no ID column- my error, sorry. I have substituted this for the first column of the table/view, which is 'ClientID'.
So the modified query now looks like this:
select jan.ClientID, jan.AnswerDesc Jan, feb.AnswerDesc Feb, mar.AnswerDesc Mar, apr.AnswerDesc Apr, may.AnswerDesc May, jun.AnswerDesc Jun, jul.AnswerDesc Jul, aug.AnswerDesc Aug, sep.AnswerDesc Sep, oct.AnswerDesc Oct, nov.AnswerDesc Nov, december.AnswerDesc December
from "dba"."vwResultInspTask_iD ashboards" jan, "dba"."vwResultInspTask_iD ashboards" feb, "dba"."vwResultInspTask_iD ashboards" mar, "dba"."vwResultInspTask_iD ashboards" apr, "dba"."vwResultInspTask_iD ashboards" may, "dba"."vwResultInspTask_iD ashboards" jun, "dba"."vwResultInspTask_iD ashboards" jul, "dba"."vwResultInspTask_iD ashboards" aug, "dba"."vwResultInspTask_iD ashboards" sep, "dba"."vwResultInspTask_iD ashboards" oct, "dba"."vwResultInspTask_iD ashboards" nov, "dba"."vwResultInspTask_iD ashboards" december
where jan.ClientID *= feb.ClientID
and jan.ClientID *= mar.ClientID
and jan.ClientID *= apr.ClientID
and jan.ClientID *= may.ClientID
and jan.ClientID *= jun.ClientID
and jan.ClientID *= jul.ClientID
and jan.ClientID *= aug.ClientID
and jan.ClientID *= sep.ClientID
and jan.ClientID *= oct.ClientID
and jan.ClientID *= nov.ClientID
and jan.ClientID *= december.ClientID
and jan.nMonth = 01
and feb.nMonth = 02
and mar.nMonth = 03
and apr.nMonth = 04
and may.nMonth = 05
and jun.nMonth = 06
and jul.nMonth = 07
and aug.nMonth = 08
and sep.nMonth = 09
and oct.nMonth = 10
and nov.nMonth = 11
and december.nMonth = 12
and "dba"."vwResultInspTask_iD ashboards" ."SiteID" = 3771 (to limit the result set).
The error returned is: Correlation name "dba"."vwResultInspTask_iD ashboards" not found
So what I have I done wrong?
1. Kept stopping a 'dec', which I think is a reserved word ('decreasing'?), so changed all of the dec to 'december'.
Ran the query:
2. Next error is 'no such table as '"dba"."vwAnswerDescInspTa
Ran it again:
3. Could not execute statement. Column 'ID' not found. Line 1, Column 1. This table is a view, so there is no ID column- my error, sorry. I have substituted this for the first column of the table/view, which is 'ClientID'.
So the modified query now looks like this:
select jan.ClientID, jan.AnswerDesc Jan, feb.AnswerDesc Feb, mar.AnswerDesc Mar, apr.AnswerDesc Apr, may.AnswerDesc May, jun.AnswerDesc Jun, jul.AnswerDesc Jul, aug.AnswerDesc Aug, sep.AnswerDesc Sep, oct.AnswerDesc Oct, nov.AnswerDesc Nov, december.AnswerDesc December
from "dba"."vwResultInspTask_iD
where jan.ClientID *= feb.ClientID
and jan.ClientID *= mar.ClientID
and jan.ClientID *= apr.ClientID
and jan.ClientID *= may.ClientID
and jan.ClientID *= jun.ClientID
and jan.ClientID *= jul.ClientID
and jan.ClientID *= aug.ClientID
and jan.ClientID *= sep.ClientID
and jan.ClientID *= oct.ClientID
and jan.ClientID *= nov.ClientID
and jan.ClientID *= december.ClientID
and jan.nMonth = 01
and feb.nMonth = 02
and mar.nMonth = 03
and apr.nMonth = 04
and may.nMonth = 05
and jun.nMonth = 06
and jul.nMonth = 07
and aug.nMonth = 08
and sep.nMonth = 09
and oct.nMonth = 10
and nov.nMonth = 11
and december.nMonth = 12
and "dba"."vwResultInspTask_iD
The error returned is: Correlation name "dba"."vwResultInspTask_iD
So what I have I done wrong?
Sorry about the incorrect view name - global search and replace of strings in bad order (changed Result to AnswerDesc after changing table names).
The problem is your final statement. You should change it to:
The alias assigned to a table/view name is the correlation name. Since all of the tables have been aliased then it is expecting the correlation name (and not the actual table/view name).
The problem is your final statement. You should change it to:
and jan.SiteID = 3771
The alias assigned to a table/view name is the correlation name. Since all of the tables have been aliased then it is expecting the correlation name (and not the actual table/view name).
ASKER
Good Morning, seems like we're on the final lap...
I put in all of the code in my previous reply to you and then added : and jan.SiteID = 3771, to limit the result set to one site, ran the query and got this msg:
Transact: SQL Outer joins are currently disabled. SQL Code = 1099, ODBC 3 State = 'HY000'
Line 1, column 1
Is this a general setting in my database or related to this query?
I put in all of the code in my previous reply to you and then added : and jan.SiteID = 3771, to limit the result set to one site, ran the query and got this msg:
Transact: SQL Outer joins are currently disabled. SQL Code = 1099, ODBC 3 State = 'HY000'
Line 1, column 1
Is this a general setting in my database or related to this query?
I've never seen that before. Are you using ASE, SQL Anywhere, or another Sybase product/version?
I found this for SQL Anywhere (if they are deprecated, I have no idea what you'd use instead):
I found this for SQL Anywhere (if they are deprecated, I have no idea what you'd use instead):
If it is a connection-specific setting, you may be able to change it (how would depend on how you connect to Sybase). It may also be a server setting in which case sa would need to change it.
The ability to specify a Transact-SQL outer join in a query is currently disabled by the setting of the tsql_outer_joins connection option. Transact-SQL outer joins are deprecated in this release of SQL Anywhere.
ASKER
Hi, yes you are correct, it is SQL Anywhere.
I also checked the error msg in Sybase's manual, got the same definition, but I don't fully understand it.
I think this problem is now moving some way beyond my skill level so I'll check with my SA to see if outer joins can be done on our server. Either way, I'll get back to you to let you know the result - really appreciate your help so far, thank you.
I also checked the error msg in Sybase's manual, got the same definition, but I don't fully understand it.
I think this problem is now moving some way beyond my skill level so I'll check with my SA to see if outer joins can be done on our server. Either way, I'll get back to you to let you know the result - really appreciate your help so far, thank you.
Open in new window