Solved

consolidate 4 lines of oracle query output to 1 line

Posted on 2016-09-29
4
41 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you so much!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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 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…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

743 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

12 Experts available now in Live!

Get 1:1 Help Now