Solved

using distinct values as column headers

Posted on 2016-09-13
6
48 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
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 34

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle- set role and grant privileges 6 28
ODBC in excel 2016 in Windows 10 via VBA 16 58
string fuctions 4 25
What is the proper way to use for criteria in left join? 7 25
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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

813 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

10 Experts available now in Live!

Get 1:1 Help Now