我正在嘗試顯示employees
的出勤記錄,特別是分支,讓我們說「浦那分支」。顯示那些數據庫中不存在日期的數據
顯示每週記錄時,它只顯示數據庫表中存在條目的日期記錄。我也想顯示那些沒有任何員工在場的日期的記錄。
假設在Log_TB
有2個員工目前爲28 Apr 2015
出2員工記錄,那麼就說明
而且如果該日期沒有記錄則返回什麼。
我想導致像
這裏是我的查詢
Declare
@Date date = GETDATE(),
@date1 date,
@Total int
set @date1 = (CONVERT(date,DATEADD(DD,-6,@Date),108));
select @Total = Count(User_TB.User_Id)
from User_TB join Branch_TB
on User_TB.User_Branch = Branch_TB.Branch_Name
where User_TB.User_Branch = 'Pune';
select convert(varchar,Log_TB.Date,106) AS Date,
CONVERT(CHAR(3),DATENAME(weekday,Log_TB.Date)) AS Day,
@Total AS Total,COUNT(Log_TB.User_Id) Present,
(@Total-COUNT(Log_TB.User_Id)) AS Absent
From Log_TB join User_TB on Log_TB.User_Id = User_TB.User_Id
where Log_TB.Date <= (CONVERT(date,@Date,108))
and Log_TB.Date >= @date1 and User_TB.User_Branch = 'Pune'
group by Log_TB.Date;
我的表定義
CREATE TABLE [dbo].[Log_TB]
(
[User_Id] [varchar](50) NOT NULL,
[First_Login] [time](0) NULL,
[Logout] [time](0) NULL,
[Date] [date] NULL,
[Working_Hrs] [time](0) NULL,
[extra_Int] [int] NOT NULL,
[extra_String] [varchar](50) NULL
)
可能的重複[在特定日期沒有找到記錄時返回resulset中的行](http://stackoverflow.com/questions/29647405/return-rows-in-a-resulset-when-沒有記錄找到特定日期) –