Solved

How to ge the column wise report from rowwise data.

Posted on 2013-06-16
10
314 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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