2012-03-21 2 views
1

従業員の名前と年を格納するテーブルがあります。列は次のとおりです。SQL照会select * <this>の場合は<condition>

employeeID int NOT NULL, 
year int NOT NULL 

プライマリキーは、employeeIDとyearのコンポジットです。私はその年の2012年が存在するテーブルのすべてのレコードを選択する必要がありますが、2011年ではありません。つまり、2011年ではなく2012年に働いた従業員のリストが必要です。これを行うことはできません!この質問を理解している場合は、下記のJavaコードをスキップしてください。

私はJavaでの従業員の表を解析した場合、それは次のようになります。

// This is what is returned at the end 
List theQueryResults = new List(); 

// This would be all employees and the years they worked, assume it's filled. 
Table employees = new Table (int employeeID, int year); 

for (int i = 0; i < employees.count(); i++) { 
    boolean addToQuery = false; 
    for (int j = 0; j < employees[0].length; j++) { 
     if (theArray[i][j] == 2011) { 
      addToQuery = false; 
      break; 
     } 
     if (theArray[i][j] == 2012) { addToQuery = true; } 
    } 

    if (addToQuery == true) { theQueryResults.add(employees[i].id); } 
} 

私は応答をいただければと思います。私は脳を持っています。オラクルで

答えて

4
SELECT employeeID 
FROM tab 
WHERE year = 2012 
and employeeID not in (SELECT employeeID FROM tab WHERE year = 2011) 
2

、あなたはMINUS演算子MSSQLの場合

select employeeId from tab 
where year = 2012 
minus 
select employeeId from tab 
where year = 2011 
+0

この作品は、2つの結果セットの間で異なるため、この作品は勝てません。 '*'ではなく明示的な列(例えば 'employeeID'のみ)を指定してください(他の列でもマイナス演算子を動かすことができます) –

+0

@Damien_The_Unbelieverありがとう!標準のSQLで私の答え –

+0

'minus' ==' EXCEPT'を更新しました。 – onedaywhen

0
select employeeID, year 
from table 
where employeeID not in (select employeeID from table where year = 2011) 
and year = 2012 

を使用することができます。

関連する問題