Solved

pivot data

Posted on 2009-07-15
14
234 Views
Last Modified: 2012-05-07
i have this table in SQL:

column1,column2,column3
10,20,30

and i want to build a view that gives this output:

column1, 10, 60, 0.2222
column2, 20, 60, 0.3333
column3, 30, 60, 0.5555

last two column are: total of column1+2+3 and % on total

thanks in advanced
0
Comment
Question by:Amien90
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 24862786
Use UNPIVOT.
More reference on UNPIVOT here:
http://www.yafla.com/dennisforbes/UNPIVOT-Normalization-SQL-Server-2000-and-SQL-Server-2005/UNPIVOT-Normalization-SQL-Server-2000-and-SQL-Server-2005.html 

;with CTE as (
select Property, Value
(select * from yourtable) o
unpivot (Value for property in (column1, column2, column3)) p
)
select 	Property, 
	Value, 
	(select sum(Value) from CTE) as total, 
	Value / (select sum(Value) from CTE) 
from CTE

Open in new window

0
 

Author Comment

by:Amien90
ID: 24862939
end of #2 there need to be a , right?
getting following error:

incorrect syntax near the keyword 'UNPIVOT'
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24863487
Oops, sorry Missed "FROM" actually
;with CTE as (
select Property, Value
from
(select * from yourtable) o
unpivot (Value for property in (column1, column2, column3)) p
)
select 	Property, 
	Value, 
	(select sum(Value) from CTE) as total, 
	Value / (select sum(Value) from CTE) 
from CTE

Open in new window

0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:Amien90
ID: 24863730
works .. two more issues:

1. last column has all 0 (zeros)
2. what if i want to cluster columns? so what if i want to add column4,5 and 6 .. but the percentage and total should only affect the clustered columns.
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 24863875
For issue 1 see attached.
Issue 2, I'm not following, can you elaborate? It would also help If you provide some sample data and expected result

;with CTE as (
select Property, Value
from
(select * from yourtable) o
unpivot (Value for property in (column1, column2, column3)) p
)
select 	Property, 
	Value, 
	(select sum(Value) from CTE) as total, 
	Value * 1.00 / (select sum(Value) from CTE) 
from CTE

Open in new window

0
 

Author Comment

by:Amien90
ID: 24863985
column 1 till 3 is category I

column 4 till 6 is category II

value of total must only be over category for column 1 till 3 ..

when i add column 4 till 6 in the current query .. i will get the total of column 1-6

column1,column2,column3,column4,column5,column6
10,20,30,1,2,3

column1, 10, 60, 0.2222
column2, 20, 60, 0.3333
column3, 30, 60, 0.5555
column4,  1, 6, %
column4,  2, 6, %
column4,  3, 6, %
0
 

Author Comment

by:Amien90
ID: 24864047
typo ... triple column4, should be column4,column5 , column6 at the end..

output now, when i add column4,5,6

column1, 10, 66, 0.2222
column2, 20, 66, 0.3333
column3, 30, 66, 0.5555
column4,  1, 66, ....
column4,  2, 66, ...
column4,  3, 66, ....
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24864088
So maybe something like this:
;with CTE as (
select Property, Value
from
(select * from yourtable) o
unpivot (Value for property in (column1, column2, column3)) p
)
select 	Property, 
	Value, 
	(select sum(Value) from CTE where property in ('column1', 'column2', 'column3')) as total, 
	CASE WHEN property in ('column1', 'column2', 'column3') then Value * 1.00 / (select sum(Value) from CTE where property in ('column1', 'column2', 'column3')) 
	else 0
	end as perc
from CTE

Open in new window

0
 

Author Comment

by:Amien90
ID: 24864154
yes .. but i also want to see the values of column4,5 and 6 ..

i'v changed: unpivot (Value for property in (column1, column2, column3, column4, column5, column6)) p

that gives me this result:

column1, 10, 66, 0.2222
column2, 20, 66, 0.3333
column3, 30, 66, 0.5555
column4,  1, 66, ....
column5,  2, 66, ...
column6,  3, 66, ....

which is almost good because i need

column1, 10, 60, 0.2222
column2, 20, 60, 0.3333
column3, 30, 60, 0.5555
column4,  1, 6, %
column5,  2, 6, %
column6,  3, 6, %

so for column1 till 3 the total column only need to SUM the colums1 till 3
total column on column 4 till 6 needs to be the SUM of column 4 till 6
0
 

Author Comment

by:Amien90
ID: 24864158
thanks for the swift replies btw
0
 

Author Comment

by:Amien90
ID: 24864451
(select sum(Value) from CTE) as total

perhaps add a where clause?

columns can be clustered by taking the first 5 chars of the column name .. ic property
0
 

Author Comment

by:Amien90
ID: 24864608

select  Property, 
        Value, 
        (select sum(Value) from CTE WHERE LEFT(t.property,5) = LEFT(property,5)) as total, 
        Value * 1.00 / (select sum(Value) from CTE WHERE LEFT(t.property,5) = LEFT(property,5)) as percentage
from CTE t

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 24865969
What about this?
;with CTE as (
select Property, Value, case when Property in ('column1', 'column2', 'column3') then 1 else 0 end as part
from
(select * from yourtable) o
unpivot (Value for property in (column1, column2, column3, column4, column5, column6)) p
)
select 	Property, 
	Value, 
	(select sum(Value) over(partition by part) from CTE) as total,
	Value * 1.00 / (select sum(Value) over(partition by part) from CTE) as percentage
 from CTE

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 24865988
Sorry I meant like this:
;with CTE as (
select Property, Value, case when property in ('column1', 'column2', 'column3') then 1 else 0 end as part
from
(select * from unpivo) o
unpivot (Value for property in (column1, column2, column3, column4, column5, column6)) p
)
select Property, Value, sum(Value) over (partition by part) as total, value * 1.0 / sum(Value) over (partition by part) as percentage from CTE
order by Property

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

707 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