Solved

using distinct values as column headers

Posted on 2016-09-13
6
40 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
6 Comments
 
LVL 20

Accepted Solution

by:
Russ Suter earned 168 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 34

Assisted Solution

by:johnsone
johnsone earned 166 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

762 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

9 Experts available now in Live!

Get 1:1 Help Now