INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to pivot columns to rows

How to pivot columns to rows

(OP)
I need the results below to "pivot" so there is a record for each _BAL column
for each dept.
==
COMPANY FISCAL_YEAR DEPT ACCOUNT SUB_ACCOUNT 00_BAL 01_BAL
100 2017 90417 5100 0 0.00 88887.85
100 2017 90417 5103 0 0.00 0.00
100 2017 90417 5200 0 0.00 173650.74
100 2017 90417 5204 0 0.00 3413.60
100 2017 90417 5210 0 0.00 315.90
100 2017 90417 5214 0 0.00 1874.20

So for dept '90417' and account '5100' the result set would look like'

COMPANY FISCAL_YEAR DEPT ACCOUNT SUB_ACCOUNT PERIOD BALANCE
100 2017 90417 5100 0 0 0.00
100 2017 90417 5100 0 1 88887.85

Below is the query that created the original results;

SELECT COMPANY,FISCAL_YEAR,ACCT_UNIT AS DEPT,ACCOUNT,SUB_ACCOUNT,DB_BEG_BAL-CR_BEG_BAL AS [00_BAL],DB_AMOUNT_01-CR_AMOUNT_01 AS [01_BAL]
FROM GLAMOUNTS
WHERE FISCAL_YEAR = '2017' AND ACCT_UNIT = '90417' AND ACCOUNT LIKE '5%'

Appreciate your suggestions.

AW

RE: How to pivot columns to rows

Hi,

What you're asking for is not a pivot.

CODE

SELECT COMPANY,FISCAL_YEAR,ACCT_UNIT AS DEPT,ACCOUNT,SUB_ACCOUNT, DB_BEG_BAL-CR_BEG_BAL AS [Balance]
FROM GLAMOUNTS
WHERE FISCAL_YEAR = '2017' AND ACCT_UNIT = '90417' AND ACCOUNT LIKE '5%'

UNION

SELECT COMPANY,FISCAL_YEAR,ACCT_UNIT AS DEPT,ACCOUNT,SUB_ACCOUNT, DB_AMOUNT_01-CR_AMOUNT_01 
FROM GLAMOUNTS
WHERE FISCAL_YEAR = '2017' AND ACCT_UNIT = '90417' AND ACCOUNT LIKE '5%' 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: How to pivot columns to rows

There is UNPIVOT.

Since you only have two columns to unpivot, it'd just get overcomplicated to do such a query, though.
I second Skips solution, concise enough in this simple case.
Since you are just creating the "Pivoted" data, the best way is not to go through this step at all and query the fields you want per row and union these results, as Skip does.

Bye, Olaf.

RE: How to pivot columns to rows

(OP)
Many Thanks!! This is exactly what I needed. Disappointed I didn't figure this out

Thanks again,

AW

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close