【发布时间】:2020-03-26 18:31:47
【问题描述】:
我有以下候选表详细信息
Table_TraineeInfo
TraineeID BatchId Name Mobile
--------------------------------------------------
243 45 demo201 9888562341
244 45 demo202 9888562342
246 45 demo204 9888562344
这是我的上述候选人的批次详细信息,在两个公用表中都有参考 id 45
Table_Batch_Lookup
BatchId BatchStartDate BatchEndDate
------------------------------------------------------------------------
45 2019-11-27 00:00:00.000 2019-11-29 23:59:59.000
下面是我的Trainee出勤日志表,Table_TraineeInfo和Table_Attendance_Log之间的共同点是TraineeID
Table_Attendance_Log
TraineeID BatchId Attendance Date
------------------------------------------------------------
243 45 Present 2019-11-27 17:55:56.513
243 45 Present 2019-11-28 17:58:06.220
243 45 Absent 2019-11-29 18:00:56.820
244 45 Present 2019-11-29 18:05:03.930
246 45 Absent 2019-11-28 18:09:08.567
246 45 Present 2019-11-29 18:09:08.567
我想要像下面这样的输出合并三个表,并使用 SQL 查询或可能的方式将一个输出作为批处理候选人出勤报告。
TraineeID BatchId Name Mobile 2019-11-27 2019-11-28 2019-11-29 Score
-----------------------------------------------------------------------------------------------------------------------------
243 45 demo201 9888562341 Present Present Absent 3/2
244 45 demo202 9888562342 No Record No Record Present 3/1
246 45 demo204 9888562344 No Record Absent Present 3/1
我将解释上面的输出前四列将使用 Table_TraineeInfo 填充,接下来的数据将根据 Table_Batch_Lookup 中的 BatchStartDate 和 BatchEndDate 填充 出席和缺席将根据Table_Attendance_Log 出席者列表中没有可用的数据然后不填写任何记录,最后得分Present 将为1 值,满3 天。
【问题讨论】:
-
那么问题出在哪里?您肯定已经尝试过一些未能产生您需要的查询。
-
我没有得到实现输出的逻辑。 @MarkusDeibel
-
你需要动态 PIVOT 来实现你的输出。
-
问题与合并无关。我相信你已经习惯了简单的内部连接。您的问题是将日期值“旋转”到列中。我建议从 stackoverflow.com/questions/13372276/… 开始。
-
是的,任何与我的输出相关的样本或任何查询。 @mkRabbani
标签: c# sql asp.net sql-server-2008