Since I am new to bigquery, I am struggling with basic stuffs. The table contains 145 records. How to create array from the table. Here is the downloadable link https://drive.google.com/uc?export=download&id=1SvkAN8sqS7WnsBO-X3UnyXI8sen5TOZ0 . The downloadable file is in csv format. I would like to know highest marks of each student and their subject.
-
2what you have tried so far. show us and we can help! and btw, not clear what is expected output - provide example please! Please edit your question to show a Minimal, Complete, and Verifiable example of the code that you are having problems with, then we can try to help with the specific problem. You can also read How to Ask. :o)Mikhail Berlyant– Mikhail Berlyant2018-02-28 16:45:06 +00:00Commented Feb 28, 2018 at 16:45
2 Answers
Thanks to Monica's answer I finally got your question :o)
I noticed that data in downloadable link is just bunch of duplicate rows so if dedup them just using simple DISTINCT you end up with just 8 rows. so that part is still not clear
But if to ignore this aspect the way to get the highest mark and respective subject is as below. It answer both questions you asked:
1 - How to create array from the table
2 - how to get highest marks of each student and their subject
#standardSQL
WITH data AS (
SELECT DISTINCT name, Physics, Chemistry, Maths, Biology
FROM `project.dataset.table`
)
SELECT name,
(
SELECT pair
FROM UNNEST([STRUCT<highest_mark INT64, subject STRING>
(Physics, 'Physics'),
(Chemistry, 'Chemistry'),
(Maths, 'Maths'),
(Biology, 'Biology')
]) pair
ORDER BY pair.highest_mark DESC
LIMIT 1
).*
FROM data
If to apply above to table with your data - below is result
Row name highest_mark subject
1 Samuel 96 Maths
2 David 90 Physics
3 Mark 90 Physics
4 John 94 Physics
5 Mathew 94 Physics
6 Andrew 97 Physics
7 James 97 Physics
8 Peter 98 Chemistry
I think this solution is more readable and manageable than using branched CASE WHEN THEN ...
2 Comments
I tried this query and it worked.
WITH data AS (SELECT DISTINCT name, Physics, Chemistry, Maths, Biology
FROM `[Your_Project].[Your_dataset].[Your_table]`)
SELECT
name,
(CASE
WHEN Physics >= Chemistry AND Physics >= Maths AND Physics >= Biology THEN Physics
WHEN Chemistry >= Physics AND Chemistry >= Maths AND Chemistry >= Biology THEN Chemistry
WHEN Maths >= Physics AND Maths >= Chemistry AND Maths >= Biology THEN Maths
WHEN Biology >= Physics AND Biology >= Chemistry AND Biology >= Maths THEN Biology
ELSE Physics
END) AS maxmark,
(CASE
WHEN Physics >= Chemistry AND Physics >= Maths AND Physics >= Biology THEN 'Physics'
WHEN Chemistry >= Physics AND Chemistry >= Maths AND Chemistry >= Biology THEN 'Chemistry'
WHEN Maths >= Physics AND Maths >= Chemistry AND Maths >= Biology THEN 'Maths'
WHEN Biology >= Physics AND Biology >= Chemistry AND Biology >= Maths THEN 'Biology'
ELSE 'Physics'
END) as field
FROM
data