显示数据库表中不存在的那些日期的数据

时间:2015-04-28 06:17:51

标签: sql sql-server tsql

我正在尝试显示Log_TB特定分支的出勤记录,让Say" Pune Branch"。

在显示每周记录时,它仅显示数据库表中有条目的日期记录。我还希望显示那些没有任何员工在场的日期记录。

假设在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; 有2名员工记录,那么它显示

Then It Shows

如果该日期没有记录,那么它什么都不返回。

我想要结果

I want result Like

这是我的查询

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
)

我的表定义

    before: {
    method: function(doc) {
        var retVal = false ;
        var pai = Q.fcall(function(){
            if(!_.isEmpty(doc) && _.pick(doc, 'name') ) {
                console.log('Ist level, true condition: ', doc);
                return true;
            }
            else{
                console.log('Ist level, false condition: ', doc);
                return false;
            }
        })
            .then(function(check){
                console.log('Check value: ', check);

                if( check ){
                 Meteor.call('CategoryNameAvailable', doc.name, function (error, result) {

                        console.log('Returned result from server', result);
                        if (!result) {
                            if(Contexts.Category.keyIsInvalid('name')){
                                Contexts.Category.resetValidation('name');
                            }
                            console.log('Returned result from server inside if condition  ', result);
                            Collections.Category.simpleSchema().namedContext("CategoryInsertForm").addInvalidKeys([{
                                name: "name",
                                type: "notUnique"
                            }]);

                            console.log('Doc value in meteor call function: ', doc);
                            Session.set('retVal', true);
                            console.log('retVal value in meteor call function: ', retVal);
                        }
                        return 'myResult';
                    });
                    // return 'Gogo';
                    /*  Meteor call End  */
                }

            })
            .then(function(chi){
                console.log('Chi value: ', chi);
            })
            .done();

        console.log('Pai value-2: ', pai);

    }  /* End of  method */
} /* End of 'before' hook */

2 个答案:

答案 0 :(得分:0)

您需要的是NumbersDate表格。你可以用这样的东西生成它

DECLARE @date1 DATE = '20150401',@Date DATE= GETDATE();
;WITH CTE AS
(
SELECT @date1 as datecol
UNION ALL SELECT DATEADD(day,1,datecol) FROM CTE WHERE DATEADD(day,1,datecol) <= @Date
)
SELECT * FROM CTE

在您的查询中,您可以像这样使用它

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';

DECLARE @date1 DATE = '20150401',@Date DATE= GETDATE();
;WITH CTE AS
(
SELECT @date1 as datecol
UNION ALL SELECT DATEADD(day,1,datecol) FROM CTE WHERE     DATEADD(day,1,datecol) <= @Date
)
select convert(varchar,CTE.datecol,106) AS Date,
CONVERT(CHAR(3),DATENAME(weekday,CTE.datecol)) AS Day,
@Total AS Total,COUNT(User_TB.User_Id) Present,
(@Total-COUNT(User_TB.User_Id)) AS Absent 
From CTE LEFT JOIN Log_TB ON Log_TB.Log_TB.Date = CTE.datecol
LEFT join User_TB on Log_TB.User_Id = User_TB.User_Id 
and User_TB.User_Branch = 'Pune'
group by CTE.datecol;

答案 1 :(得分:0)

Declare @date datetime   -- its a starting date 
Set @Date = '1-Apr-2015'


Declare @table table (date datetime)
-- inserts data from starting date till getdate
while @date <> convert(datetime,convert(varchar(10),getdate(),101),101)
begin
    insert into @table
    select @date aa
    select @date = @date+1
end

select * from @table

只需加入@table,您的数据就可以加入

Select * 
from @table left join (Your data) with condition date.