Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SHOW OFF DAY


SHOW OFF DAY

Author
Message
immaduddinahmed
immaduddinahmed
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 420
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
immaduddinahmed
immaduddinahmed
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 420
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
Dird
Dird
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 771
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
immaduddinahmed
immaduddinahmed
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 420
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

Dird
Dird
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 771
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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45247 Visits: 39929
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
immaduddinahmed
immaduddinahmed
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 420
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9013 Visits: 19028
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
Exploring Recursive CTEs by Example Dwain Camps
aaron.reese
aaron.reese
Mr or Mrs. 500
Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)Mr or Mrs. 500 (591 reputation)

Group: General Forum Members
Points: 591 Visits: 898
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
Dird
Dird
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 771
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search