Solved

How to ge the column wise report from rowwise data.

Posted on 2013-06-16
10
308 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
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39251151
wrong post - sorry
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

21 Experts available now in Live!

Get 1:1 Help Now