How to get percentage in Pivot SQL

Dear Experts,

How can I pivot this raw data with a percentage result?
Raw data:

29852.6              A
6532.2              B
58955.96      C

Result:
A                 B           C                      D
29852.6      6532.2      58955.96      0
31.31%      6.85%      61.84%      0.00%


Thanks.
JimiJ13I T ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
where did the column D come from?

please if supplying sample data and expected results it is necessary to make the data consistent with reality.

It will save you time too if you provide real table names and column names. e.g.
SomeTable
SomeNumber SomeCode
29852.6       A
6532.2        B
58955.96      C

Open in new window

0
PortletPaulfreelancerCommented:
sorry, more...

the good news, it can probably be done - with tedium.

the bad news, you cannot mix "data types" in a single column

the "%" symbol in the second row makes that row text, not numbers. hence if that row is text, then all columns must also be text. This makes it hugely more tedious to produce the output.

Is this a report (e.g. Crystal Reports)? or a screen (e.g. html)?
If it is anything like these then those tools should be used - NOT SQL!

SQL is NOT a "report writer"

-----
OR

make it easier:
a: more columns, not 2 rows
b: don't include the "%" symbol
0
PortletPaulfreelancerCommented:
| LABEL |     A |    B |     C | D |
|-------|-------|------|-------|---|
|   Sum | 29853 | 6532 | 58956 | 0 |
|     % |  31.3 |  6.9 |  61.8 | 0 |

Open in new window

It ain't pretty:
SELECT
      'Sum' AS Label
    , [A]
    , [B]
    , [C]
    , [D]
FROM (
            SELECT
                  SomeCode
                , SomeNumber * 1.0 as SomeNumber
            FROM SomeTable
      ) AS SourceTable
      PIVOT
      (
      SUM(SomeNumber)
      FOR SomeCode IN ([A], [B], [C], [D])
      ) AS PivotTable

UNION ALL

SELECT
      '%' AS Label
    , [A]
    , [B]
    , [C]
    , [D]
FROM (
            SELECT
                  SomeCode
                , SomeNumber * 100.0
                / sum(SomeNumber) over() as pct
            FROM SomeTable
      ) AS SourceTable
      PIVOT
      (
      MAX(pct)
      FOR SomeCode IN ([A], [B], [C], [D])
      ) AS PivotTable
;



CREATE TABLE SomeTable
	([SomeNumber] numeric, [SomeCode] varchar(1))
;
	
INSERT INTO SomeTable
	([SomeNumber], [SomeCode])
VALUES
	(29852.6, 'A'),
	(6532.2, 'B'),
	(58955.96, 'C'),
    (0,'D')
;

http://sqlfiddle.com/#!3/199a8/9

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

JimiJ13I T ConsultantAuthor Commented:
The D column is an invisible column when empty.

Thanks for the suggestions. What  I did was to add a subtotal column in query and made a dynamic  %  computation on another column per item  in Telerik Reporting that almost double the page width.  

Sorry, I cannot give the actual table as part of  agreement with client.

Great help and input anyway!
0
PortletPaulfreelancerCommented:
SQL is really only good for the "base data", after then ALL "presentation" should be performed in the reporting tool.
0
JimiJ13I T ConsultantAuthor Commented:
Yes, I know but there are requirements that you can only produce a tidy result by doing some dirty work in SQL.
Anyway what you have done is great, and I highly appreciated it. My client will be well pleased for it.  

Thanks.
0
JimiJ13I T ConsultantAuthor Commented:
Great!
Not so pretty but it delivers beautiful result in the report.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.