Adding time in SQL Server 2008 - sql

I want to add time in SQL Server. I have a column called checktime in the database which is a datetime datatype. I want to add the time from the column checktime. How can I do this ??
I'm doing this calculation for calculating total hrs an employee has worked during a specified day.
My database look like this.. I want to add the time from checktime where checktype = 1 and checktype = 2 and then subtract the results.
(checktype = 1 means check in and checktype = 2 means check out)
How can I do this??
Id EmpId CheckTime CheckType
---------------------------------------------------------------------------------
3 5 2013-01-03 09:00:15.000 1
4 5 2013-01-03 11:00:00.000 2
5 5 2013-01-03 11:30:00.000 1
6 5 2013-01-03 13:00:00.000 2
7 5 2013-01-03 13:30:00.000 1
8 5 2013-01-03 16:00:00.000 2
9 5 2013-01-03 16:30:00.000 1
10 5 2013-01-03 18:00:00.000 2

This is what I would do, if I understand you question correctly:
SELECT
EmpId,
CheckTime,
CheckType,
ROW_NUMBER() OVER(PARTITION BY EmpId, DATEADD(dd,DATEDIFF(dd,0,cin.CheckTime),0), CheckType ORDER BY CheckTime) AS Seq
INTO
#PreparedTable
FROM
SourceTable
SELECT
cin.EmpId,
DATEADD(dd,DATEDIFF(dd,0,cin.CheckTime),0) AS CheckDate,
(SUM(DATEDIFF(ss,cin.CheckTime,cout.CheckTime)) / 3600.0) AS HoursWorked
FROM
#PreparedTable cin
JOIN
#PreparedTable cout
ON (cin.EmpId = cout.EmpID)
AND (DATEDIFF(dd,cin.CheckTime,cout.CheckTime) = 0)
AND (cin.Seq = cout.Seq)
AND (cin.CheckType = 1)
AND (cout.CheckType = 2)

Related

SELECTING records based on unique date and counting how many records on that date

I have a table that I'm going to simplify. Here's what it looks like:
tid session pos dateOn
-----------------------------------------------
1 23 0 12/24/2020 1:00:00
2 23 1 12/24/2020 1:01:23
3 12 0 12/24/2020 1:02:43
4 23 2 12/24/2020 1:04:01
5 23 3 12/24/2020 1:04:12
6 45 0 12/26/2020 4:23:15
This table tells me that there were 2 unique sessions 12/24/2020 and 1 on 12/26.
How do I write my SQL statement so that I get a result like this:
date recordCount
----------------------------
12/24/2020 2
12/26/2020 1
You should simply be able to convert to a date and aggregate:
select convert(date, dateon), count(distinct session)
from t
group by convert(date, dateon)
order by convert(date, dateon);

Creating a new calculated column in SQL

Is there a way to find the solution so that I need for 2 days, there are 2 UD's because there are June 24 2 times and for the rest there are single days.
I am showing the expected output here:
Primary key UD Date
-------------------------------------------
1 123 2015-06-24 00:00:00.000
6 456 2015-06-24 00:00:00.000
2 123 2015-06-25 00:00:00.000
3 658 2015-06-26 00:00:00.000
4 598 2015-06-27 00:00:00.000
5 156 2015-06-28 00:00:00.000
No of times Number of days
-----------------------------
4 1
2 2
The logic is 4 users are there who used the application on 1 day and there are 2 userd who used the application on 2 days
You can use two levels of aggregation:
select cnt, count(*)
from (select date, count(*) as cnt
from t
group by date
) d
group by cnt
order by cnt desc;

Getting date difference between consecutive rows in the same group

I have a database with the following data:
Group ID Time
1 1 16:00:00
1 2 16:02:00
1 3 16:03:00
2 4 16:09:00
2 5 16:10:00
2 6 16:14:00
I am trying to find the difference in times between the consecutive rows within each group. Using LAG() and DATEDIFF() (ie. https://stackoverflow.com/a/43055820), right now I have the following result set:
Group ID Difference
1 1 NULL
1 2 00:02:00
1 3 00:01:00
2 4 00:06:00
2 5 00:01:00
2 6 00:04:00
However I need the difference to reset when a new group is reached, as in below. Can anyone advise?
Group ID Difference
1 1 NULL
1 2 00:02:00
1 3 00:01:00
2 4 NULL
2 5 00:01:00
2 6 00:04:00
The code would look something like:
select t.*,
datediff(second, lag(time) over (partition by group order by id), time)
from t;
This returns the difference as a number of seconds, but you seem to know how to convert that to a time representation. You also seem to know that group is not acceptable as a column name, because it is a SQL keyword.
Based on the question, you have put group in the order by clause of the lag(), not the partition by.

SQL code to get count over each day of a range

I have a data.frame like below.
toolid startdate enddate stage
abc 1-Jan-13 5-Jan-13 production
abc 6-Jan-13 10-Jan-13 down
xyz 3-Jan-13 8-Jan-13 production
xyz 9-Jan-13 15-Jan-13 down
I want to get final output which will be like below. The output needs to return - count of each stage (there could be more than 2 stages) over each day between 1jan13 to 15jan13 (or any date range that an user wants). I was able to create the required result in R. I also wrote a cursor in SQL and it achieves the purpose. But is there a way to do the same without using cursors? I am looking for logic and direction.
date down production
1 2013-01-01 0 1
2 2013-01-02 0 1
3 2013-01-03 0 2
4 2013-01-04 0 2
5 2013-01-05 0 2
6 2013-01-06 1 1
7 2013-01-07 1 1
8 2013-01-08 1 1
9 2013-01-09 2 0
10 2013-01-10 2 0
11 2013-01-11 1 0
12 2013-01-12 1 0
13 2013-01-13 1 0
14 2013-01-14 1 0
15 2013-01-15 1 0
I think this may be what you want. It requires a recursive CTE to get a row for each day in the range.
with daterange as (
select startdate=min(startdate),enddate=max(enddate) from #source
), dates as (
select d=(select startdate from daterange) union all
select dateadd(day,1,d) from dates where d<(select enddate from daterange)
)
select
d,
down=(select count(*) from #source where d between startdate and enddate and stage='down'),
production=(select count(*) from #source where d between startdate and enddate and stage='production')
from dates
order by d;

Insert data from to a table

I have a table that lists number of comments from a particular site like the following:
Date Site Comments Total
---------------------------------------------------------------
2010-04-01 00:00:00.000 1 5 5
2010-04-01 00:00:00.000 2 8 13
2010-04-01 00:00:00.000 4 2 7
2010-04-01 00:00:00.000 7 13 13
2010-04-01 00:00:00.000 9 1 2
I have another table that lists ALL sites for example from 1 to 10
Site
-----
1
2
...
9
10
Using the following code i can find out which sites are missing entries for the previous month:
SELECT s.site
from tbl_Sites s
EXCEPT
SELECT c.site
from tbl_Comments c
WHERE c.[Date] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0)
Producing:
site
-----
3
5
6
8
10
I would like to be able to insert the missing sites that is listed from my query into the comments table with some default values, i.e '0's
Date Site Comments Total
---------------------------------------------------------------
2010-04-01 00:00:00.000 3 0 0
2010-04-01 00:00:00.000 5 0 0
2010-04-01 00:00:00.000 6 0 0
2010-04-01 00:00:00.000 8 0 0
2010-04-01 00:00:00.000 10 0 0
the question is, how did i update/insert the table/values?
cheers,
Lee
INSERT INTO CommentTable (Date, Site, Comments, Total)
SELECT '2010-04-01 00:00:00.000', Site, 0, 0
FROM SiteTable
WHERE Site NOT IN
(SELECT DISTINCT Site FROM CommmentTable
WHERE [Date] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0))
A Left Join from Site to your commenttable should do the work i guess
At least if I did unterstand your intention
EDIT : Sry thought you wanna select all sites with those comments