Solved

using distinct values as column headers

Posted on 2016-09-13
6
51 Views
Last Modified: 2016-10-30
My goal here is to use test_date as the column header and display numscore under the test_date column by name.  Here is what I have so far.  I can get the date in it's own column but I can't figure out how to use the date as the column header instead.  There are many more dates and scores so the column header(s) should be dynamic for each distinct date that exists.  Can anyone help?  Thanks!!!

Current query
SELECT 
DISTINCT to_char(st.Test_Date,'yyyy-mm-dd') as Test_Date, 
ts.Name, 
sts.NumScore
FROM StudentTestScore sts
INNER JOIN Students s ON sts.StudentID = s.ID
INNER JOIN StudentTest st ON sts.StudentTestID = st.ID
INNER JOIN Test t ON st.TestID = t.ID
INNER JOIN TestScore ts ON sts.TestScoreID = ts.ID
WHERE t.Name = 'CMT' AND sts.NumScore > 0 and s.student_number = '3013744'
ORDER BY ts.Name, Test_Date

Open in new window


Current Results
test_date, name, numscore
2011-03-15, Math, 261
2012-03-15, Math, 258
2013-03-15, Math, 272
2011-03-15, Reading, 252
2012-03-15, Reading, 243
2013-03-15, Reading, 240

Desired Results
name, 2011-03-15, 2012-03-15, 2013-03-15
Math, 261, 258, 272
Reading, 252, 243, 240
0
Comment
Question by:Basssque
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 20

Accepted Solution

by:
Russ Suter earned 168 total points
ID: 41796302
First off, your syntax is Oracle PLSQL so this isn't a Microsoft SQL server question. You might want to update your tags.

If I were doing this in MS SQL Server I'd use a pivot query. Since I'm not familiar with pivot query syntax in Oracle I'll just leave this here. Perhaps someone else can elaborate.

https://oracle-base.com/articles/11g/pivot-and-unpivot-operators-11gr1
1
 

Author Comment

by:Basssque
ID: 41796524
Thanks, I updated the tags.  I'm not sure I can use pivot tables because I only have read access to the database for queries only, I can't write to it.
0
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41796530
You should be able to use pivot queries. They're just part of the query syntax. Anyway, I'll be stepping away from this question since, as stated before, I'm not very familiar with PL SQL syntax.
0
 
LVL 22

Assisted Solution

by:Helena Marková
Helena Marková earned 166 total points
ID: 41797305
0
 
LVL 35

Assisted Solution

by:johnsone
johnsone earned 166 total points
ID: 41797838
This is not a PL/SQL question.  PL/SQL is Oracle's language for functions/packages/procedures/triggers (the PL stands for Programming Language).  It would be similar to SQL Server's T-SQL.

Labeling a question as a PL/SQL question when it doesn't contain anything that has to do with functions/packages/procedures/triggers can prevent people from looking at the question.  If they are a SQL pro and don't know much PL/SQL, they can skip a question that they could really help on.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

734 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