无法从调查中找出这个 SQL
Posted
技术标签:
【中文标题】无法从调查中找出这个 SQL【英文标题】:Can't figure out this SQL from a survey 【发布时间】:2017-10-22 19:13:45 【问题描述】:必须有一种更简单的方法来做到这一点,但我对 SQL 很陌生。我从一项大型调查中获得了这些数据,我需要这方面的帮助。我想找到每节课的平均和总课时,但数据的格式是所有成绩都在一个表中。这不应该那么难,但我无法弄清楚。谢谢!
SELECT
What_grade_are_you_in as Grade,
How_much_free_time_do_you_have_each_night as FreeTime,
AvgEnglish9HW,
AvgBible9HW,
AvgGov9HW,
AvgMath9HW,
AvgScience9HW,
AvgLang9HW,
AvgArt9HW,
AvgEnglish10HW,
AvgBible10HW,
AvgGov10HW,
AvgMath10HW,
AvgScience10HW,
AvgLang10HW,
AvgArt10HW,
AvgEnglish11HW,
AvgBible11HW,
AvgGov11HW,
AvgMath11HW,
AvgScience11HW,
AvgLang11HW,
AvgArt11HW,
AvgEnglish12HW,
AvgBible12HW,
AvgGov12HW,
AvgMath12HW,
AvgScience12HW,
AvgLang12HW,
AvgArt12HW,
SUM(AvgEnglish12HW, AvgBible12HW, AvgGov12HW, AvgMath12HW, AvgScience12HW, AvgLang12HW, AvgArt12HW) as TotalHW12,
SUM(AvgEnglish11HW,AvgBible11HW,AvgGov11HW,AvgMath11HW,AvgScience11HW,AvgLang11HW,AvgArt11HW) as TotalHW11,
SUM(AvgEnglish10HW,AvgBible10HW,AvgGov10HW,AvgMath10HW,AvgScience10HW,AvgLang10HW,AvgArt10HW) as TotalHW10,
SUM(AvgEnglish9HW,AvgBible9HW,AvgGov9HW,AvgMath9HW,AvgScience9HW,AvgLang9HW,AvgArt9HW) as TotalHW9
FROM `omnihealth-1.HWLoad.HWLoad` ,(
SELECT
AVG(On_average_how_much_homework_do_you_have_per_class_from_English) as AvgEnglish9HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Bible) as AvgBible9HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Gov) as AvgGov9HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Math) as AvgMath9HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Science) as AvgScience9HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Foreign_Language) as AvgLang9HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Fine_Arts) as AvgArt9HW
FROM `omnihealth-1.HWLoad.HWLoad`
WHERE What_grade_are_you_in = 9
SELECT
AVG(On_average_how_much_homework_do_you_have_per_class_from_English) as AvgEnglish10HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Bible) as AvgBible10HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Gov) as AvgGov10HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Math) as AvgMath10HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Science) as AvgScience10HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Foreign_Language) as AvgLang10HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Fine_Arts) as AvgArt10HW
FROM `omnihealth-1.HWLoad.HWLoad`
WHERE What_grade_are_you_in = 10
SELECT
AVG(On_average_how_much_homework_do_you_have_per_class_from_English) as AvgEnglish11HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Bible) as AvgBible11HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Gov) as AvgGov11HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Math) as AvgMath10HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Science) as AvgScience11HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Foreign_Language) as AvgLang11HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Fine_Arts) as AvgArt11HW
FROM `omnihealth-1.HWLoad.HWLoad`
WHERE What_grade_are_you_in = 11
SELECT
AVG(On_average_how_much_homework_do_you_have_per_class_from_English) as AvgEnglish12HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Bible) as AvgBible12HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Gov) as AvgGov12HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Math) as AvgMath12HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Science) as AvgScience12HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Foreign_Language) as AvgLang12HW,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Fine_Arts) as AvgArt12HW
FROM `omnihealth-1.HWLoad.HWLoad`
WHERE What_grade_are_you_in = 12
)
GROUP BY
Grade
ORDER BY
TotalHW12,TotalHW11,TotalHW10,TotalHW9 DESC
【问题讨论】:
【参考方案1】:注意:您的输出(在您问题中示例的外部 SELECT 语句中)根本没有意义!从一侧看,它按年级分组,但从另一侧看,所有年级都在同一行!完全没有意义!所以我继续并假设下面的输出是预期的
适用于 BigQuery 标准 SQL
#standardSQL
SELECT
Grade,
AvgEnglish,
AvgBible,
AvgGov,
AvgMath,
AvgScience,
AvgLang,
AvgArt,
AvgEnglish + AvgBible + AvgGov + AvgMath + AvgScience + AvgLang + AvgArt AS Total
FROM (
SELECT
What_grade_are_you_in AS Grade
AVG(On_average_how_much_homework_do_you_have_per_class_from_English) AS AvgEnglish,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Bible) AS AvgBible,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Gov) AS AvgGov,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Math) AS AvgMath,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Science) AS AvgScience,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Foreign_Language) AS AvgLang,
AVG(On_average_how_much_homework_do_you_have_per_class_from_Fine_Arts) AS AvgArt
FROM `omnihealth-1.HWLoad.HWLoad`
WHERE What_grade_are_you_in IN (9, 10, 11, 12)
GROUP BY 1
)
ORDER BY Total DESC
【讨论】:
以上是关于无法从调查中找出这个 SQL的主要内容,如果未能解决你的问题,请参考以下文章