连接具有多个条件的两个SQL语句以连续运行

时间:2013-07-19 18:35:33

标签: java sql database jdbc

我的表格如下所示:

Row     Student   Major      
 1        Jenn    Math
 2        Jenn    Science
 3        Jenn    CS
 4        Mark    English
 5        Mark    History
 6        Steve   Math 
 7        Steve   Science
 8        Steve   Engineering
 9        Ann     Biology
 10       Ann     Chemistry

我正在尝试在连续的行中查找模式,其中学生将专业从Math转换为Science,然后更改为第三个专业并标记这些值,所以我会:

 Row     Name    Major      Flagged_Values
 1        Jenn    Math
 2        Jenn    Science
 3        Jenn    CS                1
 4        Mark    English
 5        Mark    History
 6        Steve   Math 
 7        Steve   Science
 8        Steve   Engineering       1
 9        Ann     Biology
 10       Ann     Chemistry

在这种情况下,学生Jenn和Steve会遇到这个标准,所以他们会被标记为1。

然后我标记那些主修数学,科学和工程学的学生(在这种情况下将是史蒂夫),并添加另一列,其中包含1。我的问题是,如果我有两个单独的语句来标记这些模式,我后来想要标记符合BOTH标准的值,我该怎么做?我会使用addBatch方法吗?我有代码,但它没有产生预期的结果。我正在使用Java(JDBC)和使用Oracle数据库的SQL库。

String x = "SELECT STUDENTS.ROW, STUDENTS.MAJOR, STUDENTS.NAME " +
  "CASE WHEN prior_row.NAME IS NOT NULL" +
  "AND EXISTS(SELECT 'x' FROM STUDENTS prior_row " +
  "WHERE STUDENTS.MAJOR = prior_row.MAJOR" +
  "AND STUDENTS.ROW > prior_row.ROW + 1" +
  "SELECT STUDENTS.MAJOR, STUDENTS.ROW, STUDENTS.NAME WHERE" +
  "MAJOR < (SELECT MAJOR FROM STUDENTS WHERE MAJOR = 'MATH' 
  "AND WHERE MAJOR > (SELECT MAJOR FROM STUDENTS WHERE MAJOR = 'SCIENCE' THEN 1 ELSE NULL END Flagged_Values";

  st.addBatch(x);

String y = "SELECT STUDENTS.ROW, STUDENTS.MAJOR, STUDENTS.NAME" +
  "CASE WHEN previous.NAME IS NOT NULL" +
  "AND EXISTS(SELECT 'y' FROM STUDENTS previous" +
  "WHERE STUDENTS.MAJOR = previous.MAJOR" +
  "AND STUDENTS.ROW > previous.ROW + 1" +
  "SELECT STUDENTS.MAJOR, STUDENTS.ROW, STUDENTS.NAME WHERE" +
  "MAJOR < (SELECT THE_OUTCOME FROM STUDENTINFO WHERE MAJOR ='Math' +
  "AND WHERE MAJOR > (SELECT MAJOR FROM STUDENTS WHERE MAJOR = 'SCIENCE'" +
  "AND WHERE MAJOR > (SELECT MAJOR FROM STUDENTS WHERE MAJOR = 'Engineering'
  "THEN 1 ELSE NULL END Flag ";

  st.addBatch(y);

我正在尝试比较连续的实例。我的第二个问题:这是否被视为数据挖掘?任何帮助将不胜感激。

0 个答案:

没有答案