Solved

SQL Sybase - Convert Rows to Columns

Posted on 2013-12-02
10
343 Views
Last Modified: 2016-05-27
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.
0
Comment
Question by:jbound
  • 4
  • 4
10 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39691061
This will work if you always have values for January.  If not, you'll need to do something a little more complex.  You also may need quotes around the Month values if they are strings.
select jan.ID, jan.Result, feb.Result, mar.Result, apr.Result, may.Result, jun.Result,
    jul.Result, aug.Result, sep.Result, oct.Result, nov.Result, dec.Result
from Current jan, Current feb, Current mar, Current apr, Current may, Current jun,
    Current jul, Current aug, Current sep, Current oct, Current nov, Current dec
where jan.ID *= feb.ID
and jan.ID *= mar.ID
and jan.ID *= apr.ID
and jan.ID *= may.ID
and jan.ID *= jun.ID
and jan.ID *= jul.ID
and jan.ID *= aug.ID
and jan.ID *= sep.ID
and jan.ID *= oct.ID
and jan.ID *= nov.ID
and jan.ID *= dec.ID
and jan.Month = 01
and feb.Month = 02
and mar.Month = 03
and apr.Month = 04
and may.Month = 05
and jun.Month = 06
and jul.Month = 07
and aug.Month = 08
and sep.Month = 09
and oct.Month = 10
and nov.Month = 11
and dec.Month = 12

Open in new window

0
 

Author Comment

by:jbound
ID: 39691164
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_iDashboards"

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
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 350 total points
ID: 39691292
Simple name substitution is all that's needed...
select jan.ID, 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, dec.AnswerDesc Dec
from "dba"."vwAnswerDescInspTask_iDashboards" jan, "dba"."vwAnswerDescInspTask_iDashboards" feb, "dba"."vwAnswerDescInspTask_iDashboards" mar, "dba"."vwAnswerDescInspTask_iDashboards" apr, "dba"."vwAnswerDescInspTask_iDashboards" may, "dba"."vwAnswerDescInspTask_iDashboards" jun, "dba"."vwAnswerDescInspTask_iDashboards" jul, "dba"."vwAnswerDescInspTask_iDashboards" aug, "dba"."vwAnswerDescInspTask_iDashboards" sep, "dba"."vwAnswerDescInspTask_iDashboards" oct, "dba"."vwAnswerDescInspTask_iDashboards" nov, "dba"."vwAnswerDescInspTask_iDashboards" dec
where jan.ID *= feb.ID
and jan.ID *= mar.ID
and jan.ID *= apr.ID
and jan.ID *= may.ID
and jan.ID *= jun.ID
and jan.ID *= jul.ID
and jan.ID *= aug.ID
and jan.ID *= sep.ID
and jan.ID *= oct.ID
and jan.ID *= nov.ID
and jan.ID *= dec.ID
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 dec.nMonth = 12

Open in new window

0
 

Author Comment

by:jbound
ID: 39691394
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"."vwAnswerDescInspTask_iDashboards" - which is correct as the table name is "dba"."vwResultInspTask_iDashboards". 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_iDashboards" jan, "dba"."vwResultInspTask_iDashboards" feb, "dba"."vwResultInspTask_iDashboards" mar, "dba"."vwResultInspTask_iDashboards" apr, "dba"."vwResultInspTask_iDashboards" may, "dba"."vwResultInspTask_iDashboards" jun, "dba"."vwResultInspTask_iDashboards" jul, "dba"."vwResultInspTask_iDashboards" aug, "dba"."vwResultInspTask_iDashboards" sep, "dba"."vwResultInspTask_iDashboards" oct, "dba"."vwResultInspTask_iDashboards" nov, "dba"."vwResultInspTask_iDashboards" 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_iDashboards"."SiteID" = 3771
(to limit the result set).

The error returned is: Correlation name "dba"."vwResultInspTask_iDashboards" not found

So what I have I done wrong?
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 26

Expert Comment

by:wilcoxon
ID: 39691413
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:
and jan.SiteID = 3771

Open in new window


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).
0
 

Author Comment

by:jbound
ID: 39691847
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?
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39694230
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):

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.
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.
0
 

Author Comment

by:jbound
ID: 39694258
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now