Solved

How to ge the column wise report from rowwise data.

Posted on 2013-06-16
10
311 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
index  - last use and update 8 56
How to run sql statements from SQLCMD or command line 2 55
Help with SQL joins 9 43
How to use TOP 1 in a T-SQL sub-query? 14 41
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…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

14 Experts available now in Live!

Get 1:1 Help Now