Solved

How to ge the column wise report from rowwise data.

Posted on 2013-06-16
10
318 Views
Last Modified: 2013-06-23
I have an table mstmassstudents
where in there are fields
course,subjectcode,uid,ims,ems,total,grade.

This data get stored in this manner.

I want the output as desired.  How to use pivot or any other method.

Sample Data attached.
Query-Data.xlsx
0
Comment
Question by:searchsanjaysharma
  • 6
  • 2
10 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39251104
SELECT
    UID
  , max(CASE WHEN SUBJECTCODE = 'CMP-126' THEN IMS   END) AS [CMP-126(IMS)]
  , max(CASE WHEN SUBJECTCODE = 'CMP-126' THEN EMS   END) AS [CMP-126(EMS)]
  , max(CASE WHEN SUBJECTCODE = 'CMP-126' THEN TOTAL END) AS [CMP-126(TOTAL)]
  , max(CASE WHEN SUBJECTCODE = 'CMP-126' THEN GRADE END) AS [CMP-126(GRADE)]
  , max(CASE WHEN SUBJECTCODE = 'CMT-121' THEN IMS   END) AS [CMT-121(IMS)]
  , max(CASE WHEN SUBJECTCODE = 'CMT-121' THEN EMS   END) AS [CMT-121(EMS)]
  , max(CASE WHEN SUBJECTCODE = 'CMT-121' THEN TOTAL END) AS [CMT-121(TOTAL)]
  , max(CASE WHEN SUBJECTCODE = 'CMT-121' THEN GRADE END) AS [CMT-121(GRADE)]
FROM mstmassstudents
GROUP BY
   UID

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39251108
to the above you may want to include:

WHERE SUBJECTCODE IN ('CMP-126','CMT-121')
0
 

Author Comment

by:searchsanjaysharma
ID: 39251136
Fetching null, why we have put max.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39251151
wrong post - sorry
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39251153
You need max() to allow summation of many rows into one row
in this query max() will not amend or affect the data
it simply allows summation to a single row per UID

compare a query with max() and one without max() here:
http://sqlfiddle.com/#!3/4d532/1

without max() it really much the same as your base table.

>>fetching null
if there is a null in the source data you would get a null in a "cell" of the output
not sure what you mean really.
0
 

Author Comment

by:searchsanjaysharma
ID: 39252226
tx
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39252257
Grade C
why???
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39252634
What grade should I award?
C should only be given for an incomplete solution that does not fully address or answer the question ... The asker must justify giving a C grade
@searchsanjaysharma I would very much appreciate knowing what was incomplete with my answer.

I shall request attention on this so it does not auto-close.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

765 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