Solved

consolidate 4 lines of oracle query output to 1 line

Posted on 2016-09-29
4
60 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

Suggested Solutions

Title # Comments Views Activity
Help on model clause 5 31
Please help with the below query - SQL Server 11 18
SQL Syntax 5 37
Query Syntax 17 36
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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.​
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

778 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