Solved

consolidate 4 lines of oracle query output to 1 line

Posted on 2016-09-29
4
57 Views
Last Modified: 2016-09-29
I've written the query below and it outputs 4 lines of data.  I need to consolidate the output to a single line, can someone help me out?  Thanks!!!

Query
SELECT 
distinct s.student_number as StuNum,
case when sts.testscoreid = '301' THEN sts.numscore else null end as EBRW_Total,
case when sts.testscoreid = '120' THEN sts.numscore else null end as Math_Total,
case when sts.testscoreid = '122' THEN sts.numscore else null end as Total_Score
FROM students s
LEFT JOIN StudentTestScore sts ON sts.StudentID = s.ID
LEFT JOIN StudentTest st ON sts.StudentTestID = st.ID
LEFT JOIN Test t ON st.TestID = t.ID
WHERE s.student_number = '12345' AND t.Name = 'SAT' AND sts.NumScore > 0

Open in new window

My Current Results
StuNum,EBRW_Total,Math_Total,Total_Score
12345,,,1010
12345,,450,
12345,560,,
12345,,,,

Open in new window

Desired results
StuNum,EBRW_Total,Math_Total,Total_Score
12345,560,450,1010

Open in new window

0
Comment
Question by:Basssque
  • 2
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41821911
See if this works:
SELECT 
distinct s.student_number as StuNum,
max(case when sts.testscoreid = '301' THEN sts.numscore else null end) as EBRW_Total,
max(case when sts.testscoreid = '120' THEN sts.numscore else null end) as Math_Total,
max(case when sts.testscoreid = '122' THEN sts.numscore else null end) as Total_Score
FROM students s
LEFT JOIN StudentTestScore sts ON sts.StudentID = s.ID
LEFT JOIN StudentTest st ON sts.StudentTestID = st.ID
LEFT JOIN Test t ON st.TestID = t.ID
WHERE s.student_number = '12345' AND t.Name = 'SAT' AND sts.NumScore > 0
group by s.student_number

Open in new window

0
 

Author Comment

by:Basssque
ID: 41821916
That works great!
Can you explain the use of max(), I'm not sure I understand how it's being used here.  Thanks!!
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 500 total points
ID: 41821920
For every row you only ever return one value per column.

For example, EBRW_Total will only have a value in one of the 4 rows.  MAX ignores nulls so the MAX value is the only value returned.
0
 

Author Closing Comment

by:Basssque
ID: 41821923
Thank you so much!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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 video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

895 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

15 Experts available now in Live!

Get 1:1 Help Now