Solved

using distinct values as column headers

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 2 46
error in my cursor 5 41
Oracle DB Slows After Datapump Until Next Reboot 27 96
Access join syntax when converting to T-SQL query 4 37
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

820 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