我正在为我正在编写的数据库程序(使用 Oracle 数据库)使用 Java 和 SQL。假设我有一个如下表:
Student Major
Student1 CS
Student1 Math
Student1 CS
Student2 English
Student2 Bio
Student2 English
我可以标记学生从一个专业更改为另一个专业并回到原来的专业的最佳方式是什么?理想情况下,输出如下所示:
Student Major Flagged_Values
Student1 CS
Student1 Math
Student1 CS 1
Student2 English
Student2 CS
Student2 English 1
由于Student1和Student2改变了专业,然后又回到了原来的状态。我是数据库编程的新手,我最困惑的部分是根据学生或身份证号比较多列的值,例如我上面的示例。如果有人可以解释如何做到这一点,那将很有帮助。
String switches = "SELECT Table.Student, Table.Major, " +
"COUNT(*)OVER (partition by Major)WHERE(partition by Student)" +
"AS Switches FROM Table";
ResultSet result = st.executeQuery(switches);
这就是我到目前为止的代码,但我不知道在一个语句中进行两次分区是否可行,或者我是否需要一个子分区。如果我想添加一个带有标记值的新列,是否需要一个 while 循环来遍历上面的 ResultSet?提前致谢。