sql server - SQL Transpose sum of field into columns -



sql server - SQL Transpose sum of field into columns -

of sql transpose questions using pivot , unpivot yet see 1 transpose single row columns. query:

select '' status, '' "count", sum(created) "created", sum(forwarded) "forwarded", sum(replied) "replied" ( select case when a.to_wg_id null , a.created_date_time = (select min(b.created_date_time) ymtn.message b b.thread_id = a.thread_id) 1 else 0 end "created", case when a.to_wg_id not null , a.from_wg_id not null 1 else 0 end "forwarded", case when a.to_wg_id null , a.created_date_time != (select min(b.created_date_time) ymtn.message b b.thread_id = a.thread_id) 1 else 0 end "replied" ymtn.message left bring together ymtn.workgroup b on a.from_wg_id=b.workgroup_id b.workgroup_id='1stop_process' )

and output above query (with 2 static fields - status , count - may not need if can figure out transpose right):

status count created forwarded replied 1693 209 1499

this desired output:

status count created 1693 forwarded 209 replied 1499

please help me find way this. thanks!

test data declare @table table ( [status] varchar(10) ,[count] varchar(10) ,created int ,forwarded int ,replied int ) insert @table values ('', '', 1693 , 209 ,1499) query ;with cte ( select * ( select created, forwarded, replied @table --<-- existing query here ) t unpivot (vals n in (created, forwarded, replied)) ) select n [status] ,vals [count] cte result set ╔═══════════╦═══════╗ ║ status ║ count ║ ╠═══════════╬═══════╣ ║ created ║ 1693 ║ ║ forwarded ║ 209 ║ ║ replied ║ 1499 ║ ╚═══════════╩═══════╝

sql sql-server transpose

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -