Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using distinct values as column headers

Posted on 2016-09-13
6
Medium Priority
?
55 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 672 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 664 total points
ID: 41797305
0
 
LVL 35

Assisted Solution

by:johnsone
johnsone earned 664 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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

705 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