Solved

How to ge the column wise report from rowwise data.

Posted on 2013-06-16
10
321 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Passing value to a stored procedure 8 110
Present Absent from working date rage 11 48
How can I use this function? 3 32
How come this XML node is not read? 3 26
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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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