员工表的SQL查询

时间:2013-02-28 03:43:23

标签: sql vb.net

我需要帮助来编写SQL来打印以下内容

表1:Emp_Master

Emp_ID   FirstName
------------------------------
1            John
2            Mark
3            Steve

表2:Emp_Transaction

Trans ID    Emp_ID    Payment_Date  Paid_status
---------------------------------------------------------
1               1             01/07/13         True
2               1             01/15/13         False
3               1             01/21/13         False
4               2             01/07/13         False
5               2             01/15/13         False
6               2             01/21/13         False
7               3             01/07/13         True
8               3             01/15/13         True
9               3             01/21/13         False

结果应该像这样打印(最早的未付日期)

Emp_ID   FirstName            Payment_Date        Paid_Status
--------------------------------------------------------------
1             John                  01/15/13           False
2             Mark                  01/07/13           False
3             steve                 01/21/13           Fals

4 个答案:

答案 0 :(得分:2)

我的第一个答案过于复杂。这就是你所需要的一切。

SELECT Emp_Master.Emp_ID Emp_ID, FirstName, MIN(Payment_Date) Payment_Date,  Paid_Status
FROM Emp_Master
 INNER JOIN  Emp_Transaction ON Emp_Master.Emp_ID = Emp_Transaction.Emp_ID
WHERE Paid_Status = 'false'
GROUP BY Emp_Master.Emp_ID, FirstName, Paid_Status

答案 1 :(得分:1)

尝试此查询

SELECT e.Emp_ID, e.FirstName, min(Payment_Date), et.Paid_Status 
FROM Emp_Master e, Emp_Transaction et
WHERE e.Emp_ID = et.Emp_ID AND et.Paid_Status= 'False' 
GROUP BY  e.Emp_ID, e.FirstName, et.Paid_Status 

答案 2 :(得分:0)

试试这个

  select model1.Emp_ID  ,
         model1.FirstName ,
         min(model2.Payment_Date) ,
         model2.Paid_Status
         from Emp_Master model1 ,Emp_Transaction model2 
         where model1.Emp_ID  =  model2.Emp_ID GROUP BY  model1.Emp_ID, model2.FirstName;

答案 3 :(得分:-1)

您可以使用Join

  

选择e.Emp_ID,e.FirstName,min(t.Payment_Date),     来自Emp_Master e的t.Paid_status,   Emp_Transaction t其中e.Emp_ID = t.Emp_ID和t.Paid_Status ='false';