来自 OpenSQL 中另一个 table 的计算字段
Calculated field from another table in OpenSQL
下面的示例在 MySql 中有效,我如何在 OpenSql 中实现它?
SELECT StudentID, Name,
( SELECT COUNT(*) FROM StudentExam
WHERE StudentExam.StudentID = Student.StudentID )
AS ExamsTaken
FROM Student
ORDER BY ExamsTaken DESC;
SAP_BASIS 版本:740-13
为什么不直接使用 INNER JOIN
和 GROUP BY
?如果你还想展示那些没有参加过考试的学生,那么你可以用 UNION
.
来实现
SELECT StudentID, Name, COUNT(*) AS ExamsTaken
FROM Student INNER JOIN StudentExam
ON Student.StudentID = StudentExam.StudentID
GROUP BY StudentID, Name
UNION
SELECT StudentID, Name, 0 AS ExamsTaken
FROM Student
WHERE StudentID NOT IN (SELECT StudentID FROM StudentExam WHERE StudentID =
Student.StudentID)
ORDER BY ExamsTaken DESC.
下面是 OpenSQL 中的一些真实示例。
REPORT zzz.
START-OF-SELECTION.
SELECT scarr~carrid, scarr~carrname, COUNT(*) AS examstaken
FROM scarr INNER JOIN sflight
ON scarr~carrid = sflight~carrid
GROUP BY scarr~carrid, scarr~carrname
UNION
SELECT scarr~carrid, scarr~carrname, 0 AS examstaken
FROM scarr
WHERE carrid NOT IN ( SELECT carrid FROM sflight WHERE carrid = scarr~carrid )
ORDER BY examstaken DESCENDING " <== applies to the union of both SELECT
INTO TABLE @DATA(g_tab_result).
BREAK-POINT.
这是结果。
下面的示例在 MySql 中有效,我如何在 OpenSql 中实现它?
SELECT StudentID, Name,
( SELECT COUNT(*) FROM StudentExam
WHERE StudentExam.StudentID = Student.StudentID )
AS ExamsTaken
FROM Student
ORDER BY ExamsTaken DESC;
SAP_BASIS 版本:740-13
为什么不直接使用 INNER JOIN
和 GROUP BY
?如果你还想展示那些没有参加过考试的学生,那么你可以用 UNION
.
SELECT StudentID, Name, COUNT(*) AS ExamsTaken
FROM Student INNER JOIN StudentExam
ON Student.StudentID = StudentExam.StudentID
GROUP BY StudentID, Name
UNION
SELECT StudentID, Name, 0 AS ExamsTaken
FROM Student
WHERE StudentID NOT IN (SELECT StudentID FROM StudentExam WHERE StudentID =
Student.StudentID)
ORDER BY ExamsTaken DESC.
下面是 OpenSQL 中的一些真实示例。
REPORT zzz.
START-OF-SELECTION.
SELECT scarr~carrid, scarr~carrname, COUNT(*) AS examstaken
FROM scarr INNER JOIN sflight
ON scarr~carrid = sflight~carrid
GROUP BY scarr~carrid, scarr~carrname
UNION
SELECT scarr~carrid, scarr~carrname, 0 AS examstaken
FROM scarr
WHERE carrid NOT IN ( SELECT carrid FROM sflight WHERE carrid = scarr~carrid )
ORDER BY examstaken DESCENDING " <== applies to the union of both SELECT
INTO TABLE @DATA(g_tab_result).
BREAK-POINT.
这是结果。