在两张桌子上获得今天的出席人数

问题描述 投票:0回答:1

下面是我的注册:Table_Trainee_Corporate_Info

Corporation_Trainee_Id  TraineeId   BatchId     Name        Mobile
 261                    COP000261   63          ankitha1    9555962321
 262                    COP000262   63          ankitha2    9555962322
 264                    COP000264   63          ankitha4    9555962324
 265                    COP000265   63          ankitha5    9555962325
 266                    COP000266   63          ankitha6    9555962326
 267                    COP000267   63          ankitha7    9555962327
 268                    COP000268   63          ankitha8    9555962328
 263                    COP000263   63          ankitha3    9555962323

下面是我的每日出勤记录表:Table_Trainee_Attendance_Info

 Trainee_atten_logId          Corporation_Trainee_Id     BatchId    Attendance  AttendanceDate
     1                            261                      63         Present   12-09-2019 15:31
     2                            262                      63         Present   12-09-2019 15:31
     3                            264                      63         Present   12-09-2019 15:31
     4                            265                      63         Present   12-09-2019 15:31
     5                            261                      63         Present   12-10-2019 15:34

我想要一个如下所示的输出;根据两个表的Corporation_Trainee_Id,今天的日期中有多少空值以及今天的日期中有多少空值。

 Corporation_Trainee_Id     Name        Mobile         Attendance
         261                ankitha1    9555962321      Present
         262                ankitha2    9555962322      NULL
         263                ankitha3    9555962322      NULL
         264                ankitha4    9555962323      NULL
         265                ankitha5    9555962323      NULL
         266                ankitha6    9555962324      NULL
         267                ankitha7    9555962324      NULL
         268                ankitha8    9555962325      NULL
sql sql-server sql-server-2008
1个回答
0
投票

您需要在此处加入左连接:

SELECT
    ci.Corporation_Trainee_Id,
    ci.Name,
    ci.Mobile,
    CASE WHEN ai.Corporation_Trainee_Id IS NOT NULL THEN 'Present' END AS Attendance
FROM Table_Trainee_Corporate_Info ci
LEFT JOIN Table_Trainee_Attendance_Info ai
    ON ci.Corporation_Trainee_Id = ai.Corporation_Trainee_Id AND
       CAST(ai.AttendanceDate AS date) = CAST(GETDATE() AS date)
WHERE
    ci.BatchId = 63
ORDER BY
    ci.Corporation_Trainee_Id;
© www.soinside.com 2019 - 2024. All rights reserved.