Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SHOW OFF DAY Expand / Collapse
Author
Message
Posted Saturday, June 15, 2013 3:16 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 5:38 AM
Points: 102, Visits: 312
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
Post #1463838
Posted Saturday, June 15, 2013 5:30 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 5:38 AM
Points: 102, Visits: 312
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
Post #1463841
Posted Saturday, June 15, 2013 5:55 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, November 21, 2014 7:40 AM
Points: 198, Visits: 666
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 // Junior DBA
11g OCA
10.5 newbie
Post #1463844
Posted Saturday, June 15, 2013 6:19 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 5:38 AM
Points: 102, Visits: 312
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
Post #1463845
Posted Saturday, June 15, 2013 7:03 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, November 21, 2014 7:40 AM
Points: 198, Visits: 666
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 // Junior DBA
11g OCA
10.5 newbie
Post #1463846
Posted Monday, June 17, 2013 5:22 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:51 PM
Points: 35,606, Visits: 32,190
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1464092
Posted Monday, June 17, 2013 7:01 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 5:38 AM
Points: 102, Visits: 312
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
Post #1464126
Posted Monday, June 17, 2013 9:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:43 AM
Points: 6,869, Visits: 14,178
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
Post #1464201
Posted Monday, June 17, 2013 9:15 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, November 17, 2014 2:27 AM
Points: 418, Visits: 652
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


Post #1464205
Posted Monday, June 17, 2013 10:09 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, November 21, 2014 7:40 AM
Points: 198, Visits: 666
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 // Junior DBA
11g OCA
10.5 newbie
Post #1464245
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse