Solved

How to convert data in table as column from a table appling self join or some other logic.

Posted on 2013-01-18
4
225 Views
Last Modified: 2013-01-18
I have the data in rows where in on the basis of those values i want to convert them into column.
The sample data and desired output is attached.
Sample-data.xlsx
0
Comment
Question by:searchsanjaysharma
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:David Todd
ID: 38795687
0
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 38795711
Hi,

I used the Union All to eliminate the difference between the delivered subject and the attended subject.

use ExpertsExchange
go

if exists(
	select *
	from sys.objects
	where
		object_id = object_id( N'dbo.SomeData' )
		and type in( N'U' )
	)
	drop table dbo.SomeData
;
go

if not exists(
	select *
	from sys.objects
	where
		object_id = object_id( N'dbo.SomeData' )
		and type in( N'U' )
	)
	create table dbo.SomeData (
		UID int
		, Subject varchar( 10 )
		, Attended int
		, Delivered int
	) on [PRIMARY]
go

insert dbo.SomeData( UID, Subject, Attended, Delivered )
	values( 101, 'SC-101', 	1,	4 )
	, ( 102, 	'SC-101',	2,	4 )
	, ( 103, 	'SC-101',	3,	4 )
	, ( 104, 	'SC-101',	4,	4 )
	, ( 105, 	'SC-101',	3,	6 )
	, ( 101, 	'SC-102',	4,	6 )
	, ( 102, 	'SC-102',	2,	6 )
	, ( 103, 	'SC-102',	6,	6 )
	, ( 104, 	'SC-102',	5,	6 )
	, ( 105, 	'SC-102',	4,	6 )
	, ( 101, 	'SC-103',	1,	3 )
	, ( 102, 	'SC-103',	1,	3 )
	, ( 103, 	'SC-103',	2,	3 )
	, ( 104, 	'SC-103',	3,	3 )
	, ( 105, 	'SC-103',	2,	3 )
	, ( 101, 	'SC-101',	1,	2 )
	, ( 102, 	'SC-101',	0,	2 )
	, ( 103, 	'SC-101',	1,	2 )
	, ( 104, 	'SC-101',	1,	2 )
	, ( 105, 	'SC-101',	1,	2 )
	, ( 101, 	'SC-102',	1,	3 )
	, ( 102, 	'SC-102',	2,	3 )
	, ( 103, 	'SC-102',	1,	3 )
	, ( 104, 	'SC-102',	2,	3 )
	, ( 105, 	'SC-102',	1,	3 )
	, ( 101, 	'SC-103',	0,	1 )
	, ( 102, 	'SC-103',	0,	1 )
	, ( 103, 	'SC-103',	1,	1 )
	, ( 104, 	'SC-103',	1,	1 )
	, ( 105,	'SC-103',	0,	1 )
;

select *
from dbo.SomeData
;

select
	UID
	, Subject + ' (Attended)' as Subject
	, Attended as Attendance
from dbo.SomeData

union all

select
	UID
	, Subject + ' (Delivered)' as Subject
	, Delivered as Attendance
from dbo.SomeData
;

select 
	UID
	, [SC-101 (Attended)]
	, [SC-101 (Delivered)]
	, [SC-102 (Attended)]
	, [SC-102 (Delivered)]
	, [SC-103 (Attended)]
	, [SC-103 (Delivered)]
from (
	select
		UID
		, Subject + ' (Attended)' as Subject
		, Attended as Attendance
	from dbo.SomeData

	union all

	select
		UID
		, Subject + ' (Delivered)' as Subject
		, Delivered as Attendance
	from dbo.SomeData
) p
pivot (
	sum( Attendance )
	for Subject in ( [SC-101 (Attended)], [SC-101 (Delivered)], [SC-102 (Attended)], [SC-102 (Delivered)], [SC-103 (Attended)], [SC-103 (Delivered)] )
	) as pvt
order by
	UID
;

Open in new window


HTH
  David

PS I have a database I keep as a testbed called ExpertsExchange (grin)
0
 

Author Comment

by:searchsanjaysharma
ID: 38795782
This i can achieve using group by also. i want to convert those into columns.
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 38795784
Tx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

20 Experts available now in Live!

Get 1:1 Help Now