SHOW OFF DAY

  • HI

    i want to show off days in my report

    my sql data is like this

    date----------timein------timeout--shift----------eid

    2-May-2013--9:20AM---6:17PM-----G-----------17090

    3-May-2013--9:09AM---2:01PM-----G-----------17090

    4-May-2013--9:03AM---10:41AM----G-----------17090

    sql data doesnot show 5 may data in sql or u can say sunday data becouse sunday is off and no employee come on sunday thats why i cant show off day in report

    please give me a suggestion to look data like this

    or any month when sunday come report show O in shift and show date just like below

    date----------timein------timeout--shift----------------eid

    2-May-2013--9:20AM---6:17PM-----G----------------17090

    3-May-2013--9:09AM---2:01PM-----G----------------17090

    4-May-2013--9:03AM---10:41AM----G----------------17090

    5-May-2013--------------------------O----------------17090

    i hope u under stand

    Thank in advance

    immad

  • This is my calendar data structure

    CREATE TABLE [dbo].[Calendar]

    (

    [Date] [datetime] NULL

    )

    i insert all 2013 months dates in this table

    i want to show offday dates in database like this

    date----------------------------eid----shift--------timein---------------------timeout--

    2013-05-04 00:00:00.000--17031----G----2013-06-13 09:15:00.000-----2013-06-13 15:23:00.000

    2013-05-04 00:00:00.000--17031----O-----------NULL------------------------NULL

    please help me out

    immad

  • select c.DATE, s.eid, isnull(s.Shift,'O'), s.timein, s.timeout

    from Shift s right outer join calendar c on s.date=c.date

    Like this? The "shift" table would be replaced with the table containing "timein" etc


    Dird

  • sir please implemenet on this query

    select

    [date],

    min([Timein]) as First_Record,

    sum(DATEDIFF(minute, [Timein], [Timeout])) as Time_Minutes

    into #temp1 from Atend

    where eid = 26446

    group by [date]

    GO

    select

    t.[date],

    t.eid,

    t.[Timein] as timein,

    t.[Timeout] as timeout,

    CONVERT(VARCHAR(8), DATEADD(minute, Time_Minutes, 0), 108) AS SpendTime,

    case when (540 - Time_Minutes) > 0 Then '- ' else '+ ' end

    +CONVERT(VARCHAR(8), DATEADD(minute, ABS(540 - Time_Minutes), 0), 108) as excesshorttime

    FROM Atend t

    left join #temp1 t2 on t.[date] = t2.[date] and t.[Timein] = t2.First_Record

    where eid = 26446

    order by t.[date], t.[Timein]

    i want this type of data

    date----------------------------eid----shift--------timein---------------------timeout--

    2013-05-04 00:00:00.000--17031----G----2013-06-13 09:15:00.000-----2013-06-13 15:23:00.000

    2013-05-04 00:00:00.000--17031----O-----------NULL------------------------NULL

    immad

  • Why would "2013-05-04" be an off day AND 'G'?

    Why does your query have SpendTime, excesshorttime when the example of what you want doesn't have it?

    You also give no info about the data -_ I thought eid = employee id but then it doesn't make sense why you look for the minimum timein (since an employee would only timein once per day?). You also have no "shift" column so I assumed you want a case. The solution below may work although it won't be the best.

    with t1 as (select [date], min([Timein]) as First_Record,

    sum(DATEDIFF(minute, [Timein], [Timeout])) as Time_Minutes

    from Atend where eid = 26446

    group by [date]),

    t2 as (select

    t.[date],t.eid,t.[Timein] as timein,t.[Timeout] as timeout,

    CONVERT(VARCHAR(8), DATEADD(minute, Time_Minutes, 0), 108) AS SpendTime,

    case when (540 - Time_Minutes) > 0 Then '- ' else '+ ' end

    +CONVERT(VARCHAR(8), DATEADD(minute, ABS(540 - Time_Minutes), 0), 108) as excesshorttime

    FROM Atend t inner join t1 t1 on t.[date] = t1.[date] and t.[Timein] = t1.First_Record

    where eid = 26446)

    select c.date, isnull(t2.eid,26446) eid,

    case when t2.timein is null then 'O'

    else 'G' end as Shift, t2.timein, t2.timeout

    from calendar c left outer join t2 t2 on c.date= t2.date

    order by c.[date], t2.[timein]


    Dird

  • I'd like to recommend that you should not have separate date and time columns for this because it will someday become a huge pain when someone works through midnight.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • i am making attendance project for a factory.employee come in factory time in and time out. on sunday employee have off day and some time employee get absent in my data i dont have absent and off day data i want to create this. my data is like this

    date---------------------------eid------------------timein-----------------------------timeout-------------------------------spendtime---------excesshsort

    2013-05-02 00:00:00.000--17031----2013-06-13 09:34:00.000---2013-06-13 18:30:00.000-------08:56:00------ 00:04:00

    2013-05-03 00:00:00.000--17031------2013-06-13 09:55:00.000------2013-06-13 13:30:00.000-------04:41:00--------- 04:19:00

    i want this type of data

    date---------------------------eid----------------timein----------------------------------timeout------------------------spendtime---------excesshsort

    2013-05-02 00:00:00.000--17031-----2013-06-13 09:34:00.000------2013-06-13 18:30:00.000-------08:56:00------ 00:04:00

    2013-05-03 00:00:00.000--17031------2013-06-13 09:55:00.000------2013-06-13 13:30:00.000-------04:41:00------ 04:19:00

    2013-05-04 00:00:00.000---17031---------2013-06-13 09:15:00.000------2013-06-13 15:23:00.000-----06:08:00-- 02:52:00

    2013-05-05 00:00:00.000--17031--------------NULL------------------------NULL-------------------------------NULL---------NULL

    when employee absent or off day data show in table off day mean sunday

    i hope u under stand

    thanks

    immad

  • There's another element to this issue here. The OP has a calendar table but is unsure how to plug it into the query above.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • The answer to the OP question is to use LEFT JOIN

    SELECT Calendar.date, shiftQuery.* From Calendar LEFT JOIN shiftQuery on Calendar.date = shiftQuery.date

    This will give you the calendar record EVERY TIME and the shift record IF IT EXISTS

  • I'm guessing it would actually be used in an OUTER JOIN like in the possible solution I gave~ from what he says the main table doesn't contain saturday/sunday records so he created the calendar for it.

    @OP: did you try the code I sent back?


    Dird

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply