我需要根据PERSON_ID连接两个表,并收集每个person_ID的平均成绩的平均值,仅包括数值。这两个表格被称为学生和成绩。我需要将列PERSON_ID,ENROLL_PERIOD和GRADE组合在一起 到目前为止我已经尝试了
select students.PERSON_ID, students.ENROLL_PERIOD, AVG(Cast(grades.GRADE as numeric))
from Students
INNER JOIN Grades on Students.PERSON_ID = Grades.PERSON_ID
where ENROLL_PERIOD IS NOT NULL AND ENROLL_PERIOD <> ''
Order by ENROLL_PERIOD ASC
GROUP BY PERSON_ID,ENROLL_PERIOD
我收到错误:
关键字&#39; GROUP&#39;附近的语法不正确。 [156](严重程度15)
表格如下:
PERSON ID : ENROLL_PERIOD : GRADE
_________________________________
12401 : 109 : 4
12401 : 109 : 7
12401 : 109 : B
43245 : 112 : 12
43245 : 112 : IB
我不确定,如果我朝着正确的方向前进,那么如果有人能帮助我,我将非常感激。我是MS SQL的新手。
答案 0 :(得分:2)
你需要把订单作为最后一个声明, 并且您需要排除所有非数值,然后才能在avg函数中使用它们 这样的事可能
declare @students table(person_id int, enroll_period int)
declare @grades table(person_id int, grade varchar(2))
insert into @students values (12401, 109), (43245, 112)
insert into @grades values(12401, '4'), (12401, '7'), (12401, 'B'), (43245, '12'), (43245, 'IB')
select s.PERSON_ID,
s.ENROLL_PERIOD,
avg( case when isnumeric(g.grade) = 1 then convert(int, g.grade) else null end) as AvgGrade
from @students s
INNER JOIN @grades g on s.PERSON_ID = g.PERSON_ID
where ENROLL_PERIOD IS NOT NULL AND ENROLL_PERIOD <> ''
GROUP BY s.PERSON_ID, ENROLL_PERIOD
Order by ENROLL_PERIOD ASC
返回以下结果
PERSON_ID ENROLL_PERIOD AvgGrade
--------- ------------- -------
12401 109 5
43245 112 12
修改强>
在你的实际表格上它可能看起来像这样:
select s.PERSON_ID,
s.ENROLL_PERIOD,
avg( case when isnumeric(g.grade) = 1 then convert(int, g.grade) else null end) as AvgGrade
from students s
INNER JOIN grades g on s.PERSON_ID = g.PERSON_ID
where ENROLL_PERIOD IS NOT NULL AND ENROLL_PERIOD <> ''
GROUP BY s.PERSON_ID, ENROLL_PERIOD
Order by ENROLL_PERIOD ASC
答案 1 :(得分:0)
print_r($btypes);
//Array ( [0] => 11x13 Red and Yellow 2 [1] => Party Bounce and Slide 13 x 18 )
if(in_array("12x15 Purple and Yellow 2" || "11x13 Red and Yellow 1" || "12x15 Yellow and Purple old" || "11x13 Red and Yellow 2" || "12x15 Purple and Yellow 1" ,$btypes) &&
in_array("Bootcamp Obstacle Course" || "Terminator Torment Obstacle Course" || "Lizard Obstacle Course" || "Bugs Obstacle Course" || "Nemo Obstacle Course",$btypes))
{
return "Standard Bouncy Castle and an Obstacle Course";
}
排序依据
分组答案 2 :(得分:0)
您需要在排序之前按语法放置组。
sql语法顺序:
SELECT yourcolumns
FROM tablenames
JOIN tablenames
WHERE condition
GROUP BY yourcolumns
HAVING aggregatecolumn condition
ORDER BY yourcolumns