Table1:
empid DT
9999 2018-10-23 19:00:00.000
9999 2018-10-24 07:00:00.000
9999 2018-10-21 08:00:00.000
9999 2018-10-22 06:00:00.000
9999 2018-10-24 03:00:00.000
9999 2018-10-24 05:00:00.000
9999 2018-10-23 06:00:00.000
9999 2018-10-23 21:00:00.000
Table 2:
emp_id in_time out_time
9999 2018-10-21 08:00:00.000 2018-10-22 06:00:00.000
9999 2018-10-22 06:00:00.000 2018-10-22 07:00:00.000
9999 2018-10-23 19:00:00.000 2018-10-24 07:00:00.000
I tried this query to get all data in table1 between intime and outtime from table2 but i cant able to get in between these in_time and out_time
select a.emp_reader_id, DT, EVENTID1
from trnevents a
left outer join daily_attendance_data d on d.emp_reader_id = a.emp_reader_id
left outer join employee e on e.emp_reader_id = a.emp_reader_id
where (CONVERT(VARCHAR(26), A.DT, 23) >= CONVERT(VARCHAR(26), '2018-10-23', 23)
and CONVERT(VARCHAR(26), A.DT, 23) <= CONVERT(VARCHAR(26), '2018-10-24', 23))
and a.emp_reader_id=9999
group by a.emp_Reader_id, a.dt,a.eventid
order by emp_reader_id, DT asc
Expected Output:
in_time:2018-10-23 19:00:00.000
out_time:2018-10-24 07:00:00.000
2018-10-23 19:00:00.000
2018-10-24 03:00:00.000
2018-10-24 05:00:00.000
2018-10-24 07:00:00.000
if DT is datetime then below where condition will work no need any varchar conversion
where DT>='2018-10-23 19:00:00.000' and DT<='2018-10-24 07:00:00.000'
Related
I have a sql query that brings back a list of references (products) that were at a specific status and an effective date. Unfortunately when one product moves to a different status the system doesn't put an end date in, so I am wanting to generate the end date, based on the effective date and sequence number. Is this possible?
Product Status EffectiveDate Enddate SeqNo
10 *UC 2017-10-02 00:00:00.000 NULL 8590
584 UC 2017-02-28 00:00:00.000 NULL 8380
584 APA 2017-07-07 00:00:00.000 NULL 8620
584 APA3 2017-08-10 00:00:00.000 NULL 8630
902 *UC 2017-10-13 00:00:00.000 NULL 8590
902 APA 2017-10-13 00:00:00.000 NULL 8620
1017 *UC 2017-09-01 00:00:00.000 NULL 8590
1017 APA 2017-10-10 00:00:00.000 NULL 8620
SO I would want to return the following...
Product Status EffectiveDate EndDate SeqNo
10 *UC 2017-10-02 00:00:00.000 NULL 8590
584 UC 2017-02-28 00:00:00.000 2017-07-07 00:00:00.000 8380
584 APA 2017-07-07 00:00:00.000 2017-08-10 00:00:00.000 8620
584 APA3 2017-08-10 00:00:00.000 NULL 8630
902 *UC 2017-10-13 00:00:00.000 2017-10-13 00:00:00.000 8590
902 APA 2017-10-13 00:00:00.000 NULL 8620
1017 *UC 2017-09-01 00:00:00.000 2017-10-10 00:00:00.000 8590
1017 APA 2017-10-10 00:00:00.000 NULL 8620
Many thanks.
You can use lead() :
select t.*, lead(EffectiveDate) over (partition by product order by SeqNo) as EndDate
from table t;
However, lead() starts from version 2012 +, so you can use apply instead :
select t.*, t1.EffectiveDate as EndDate
from table t outer apply
(select top (1) t1.*
from table t1
where t1.product = t.product and t1.SeqNo > t.SeqNo
order by t1.SeqNo
) t1;
I have 2 tables one called Production and the other called Schedule.
I am trying to find is there is some production outside the schedule.
So far I am getting duplicated value because the production could be inside one schedule but outside the other one.
So far I have no luck with this sql query I was wondering if someone can point me to the right direction.
thanks in advance.
SELECT TB1.*
FROM Production AS TB1
INNER JOIN Schedule AS TB2
ON TB1.ProduceDate < TB2.StartDate OR TB1.ProduceDate > tb2.EndDate
GROUP BY TB1.ID,TB1.ProduceDate
ORDER BY Tb1.ProduceDate
ID Produce Date
1 2017-02-03 09:00:00.000
2 2017-02-03 11:00:00.000
3 2017-02-03 13:00:00.000
4 2017-02-03 18:00:00.000
7 2017-02-03 19:00:00.000
5 2017-02-03 20:00:00.000
6 2017-02-03 23:00:00.000
Production Table Data
ID ProduceDate
1 2017-02-03 09:00:00.000
2 2017-02-03 11:00:00.000
3 2017-02-03 13:00:00.000
4 2017-02-03 18:00:00.000
5 2017-02-03 20:00:00.000
6 2017-02-03 23:00:00.000
7 2017-02-03 19:00:00.000
Schedule Table Data
ID StartDate EndDate
1 2017-02-03 10:00:00.000 2017-02-03 12:00:00.000
2 2017-02-03 15:00:00.000 2017-02-03 19:00:00.000
I think you just want not exists:
select p.*
from production p
where not exists (select 1
from schedule s
where p.producedate >= s.startdate and
p.producedate <= s.enddate
);
select Production.*
from Production
left join Schedule
on ProduceDate between StartDate and EndDate
where Schedule.id is null
I have the following SQL Server query problem.
If there is a row where Issue_DATE = as Maturity_Date in another row, and if both rows have the same ID and Amount USD, then none of these rows should be displayed.
Here is a simplified version of my table:
ID ISSUE_DATE MATURITY_DATE AMOUNT_USD
1 2010-01-01 00:00:00.000 2015-12-01 00:00:00.000 5000
1 2010-01-01 00:00:00.000 2001-09-19 00:00:00.000 700
2 2014-04-09 00:00:00.000 2019-04-09 00:00:00.000 400
1 2015-12-01 00:00:00.000 2016-12-31 00:00:00.000 5000
5 2015-02-24 00:00:00.000 2015-02-24 00:00:00.000 8000
4 2012-11-29 00:00:00.000 2015-11-29 00:00:00.000 10000
3 2015-01-21 00:00:00.000 2018-01-21 00:00:00.000 17500
2 2015-02-02 00:00:00.000 2015-12-05 00:00:00.000 12000
1 2015-01-12 00:00:00.000 2018-01-12 00:00:00.000 18000
2 2015-12-05 00:00:00.000 2016-01-10 00:00:00.000 12000
Result should be:
ID ISSUE_DATE MATURITY_DATE AMOUNT_USD
1 2010-01-01 00:00:00.000 2001-09-19 00:00:00.000 700
2 2014-04-09 00:00:00.000 2019-04-09 00:00:00.000 400
5 2015-02-24 00:00:00.000 2015-02-24 00:00:00.000 8000
4 2012-11-29 00:00:00.000 2015-11-29 00:00:00.000 10000
3 2015-01-21 00:00:00.000 2018-01-21 00:00:00.000 17500
1 2015-01-12 00:00:00.000 2018-01-12 00:00:00.000 18000
I tried with self join, but I do not get right result.
Thanks in advance!
Can you try something like this? 'not exists' is the way of doing it.
select * from table t1 where not exists (select 'x' from table t2 where t1.issue_date = t2.maturity_date and t1.amount_usd=t2.amount_usd and t1.id = t2.id)
I'd think about making subquery of all the dupes and then eliminating them from the first table like so:
select t1.ID
, t1.ISSUE_DATE
, t1.MATURITY_DATE
, t1.AMOUNT_USD
FROM
t1
LEFT JOIN
(select a.ID
, a.ISSUE_DATE
, a.MATURITY_DATE
, a.AMOUNT_USD
FROM
t1 a
INNER JOIN
ti b
) dupes
on
t1.ID = dupes.ID
WHERE dupes.ID IS NULL;
There is a table test that contains data as shown below:
id id1 id2 id3 date1
1 1 2 2500 2010-09-30 00:00:00.000
2 1 2 4700 2005-01-01 00:00:00.000
3 1 2 4700 2009-08-01 00:00:00.000
4 1 3 2500 2010-09-30 00:00:00.000
5 1 3 4700 2003-02-01 00:00:00.000
6 1 8 4000 2007-04-01 00:00:00.000
7 1 8 4000 2013-09-01 00:00:00.000
8 1 8 4060 2007-04-01 00:00:00.000
9 1 8 8500 2010-09-30 00:00:00.000
What I need to do is order this data in the following format:
id1 id2 id3 date1 date2
1 2 2500 2010-09-30 00:00:00.000 9999-12-31 23:59:59.997
1 2 4700 2005-01-01 00:00:00.000 2009-07-31 00:00:00.000
1 2 4700 2009-08-01 00:00:00.000 9999-12-31 23:59:59.997
1 3 2500 2010-09-30 00:00:00.000 9999-12-31 23:59:59.997
1 3 4700 2003-02-01 00:00:00.000 9999-12-31 23:59:59.997
1 8 4000 2007-04-01 00:00:00.000 2013-08-31 00:00:00.000
1 8 4000 2013-09-01 00:00:00.000 9999-12-31 23:59:59.997
1 8 4060 2007-04-01 00:00:00.000 9999-12-31 23:59:59.997
1 8 8500 2010-09-30 00:00:00.000 9999-12-31 23:59:59.997
Using the following logic:
If there is only one record for the same id1, id2 and id3 we use the original date for date1 and we use the maximum date available in sql server (9999-12-31) for date2.
If there is more than one record for the same id1, id2 and id3 we still keep the original date for date1 and we use for date2 the original date field minus 1 day from the next record . The last record will use also the maximum date available in sql server (9999-12-31) for date2.
I manage to build the following query but it is not perfect as it brings some invalid records:
select * from
(select
t1.id1,t1.id2,t1.id3,t1.date1,
case
when t1.date1=t2.date1 then CONVERT(DATETIME, '12/31/9999 23:59:59.997')
else DATEADD(day, -1, t2.date1)
end as date2
from test t1
inner join test t2
on t1.id1=t2.id1 and t1.id2=t2.id2 and t1.id3=t2.id3
) sub
where date2>=date1
order by id1,id2,id3,date1 asc
The result of the query is:
id1 id2 id3 date1 date2
1 2 2500 2010-09-30 00:00:00.000 9999-12-31 23:59:59.997
1 2 4700 2005-01-01 00:00:00.000 9999-12-31 23:59:59.997 *
1 2 4700 2005-01-01 00:00:00.000 2009-07-31 00:00:00.000
1 2 4700 2009-08-01 00:00:00.000 9999-12-31 23:59:59.997
1 3 2500 2010-09-30 00:00:00.000 9999-12-31 23:59:59.997
1 3 4700 2003-02-01 00:00:00.000 9999-12-31 23:59:59.997
1 8 4000 2007-04-01 00:00:00.000 9999-12-31 23:59:59.997 *
1 8 4000 2007-04-01 00:00:00.000 2013-08-31 00:00:00.000
1 8 4000 2013-09-01 00:00:00.000 9999-12-31 23:59:59.997
1 8 4060 2007-04-01 00:00:00.000 9999-12-31 23:59:59.997
1 8 8500 2010-09-30 00:00:00.000 9999-12-31 23:59:59.997
As you can see, the records marked with * are not necessary but I don't know how to get rid off that rows.
This should do:
select id1,id2,id3, date1,
isnull(lead(date1) over(partition by id1,id2,id3 order by date1)-1, '99991231') as date2
from test
Try something like this
SELECT t1.id1, t1.id2, t1.id3, t1.date1 as date1,
ISNULL(t2.date1, CONVERT(DATETIME, '12/31/9999 23:59:59.997')) as date2
FROM test t1
LEFT JOIN test t2 ON t1.id1 = t2.id1 AND t1.id2 = t2.id2 AND t1.id3 = t2.id3
AND t1.id < t2.id
ORDER BY t1.id1, t1.id2, t1.id3, date1, date2
i want to split the time and calculate time difference using sql server 2005
my default output is like this:
EnrollNo AttDateFirst AttDateLast
111 2011-12-09 08:46:00.000 2011-12-09 08:46:00.000
112 2011-12-09 08:40:00.000 2011-12-09 17:30:00.000
302 2011-12-09 09:00:00.000 2011-12-09 18:30:00.000
303 2011-12-09 10:00:00.000 2011-12-09 18:35:00.000
I want my new output to be like this:
Enroll No ..... FirtTime LastTime Time Diff
111 ..... 8:46:00 8:45:00 00:00:00
112 ..... 8:30:00 17:30:00 9:00:00
302 ..... 9:00:00 18:30:00 9:30:00
303 ..... 10:00:00 18:35:00 8:35:00
You can use this query:
select EnrollNo, convert(varchar, AttDateFirst, 8) as FirstTime,
convert(varchar, AttDateLast, 8) as LastTime,
convert(varchar, AttDateLast - AttDateFirst, 8) as [Time Diff]
from YourTable
to return the following results:
EnrollNo FirstTime LastTime Time Diff
----------- ------------------------------ ------------------------------ ------------------------------
111 08:46:00 08:46:00 00:00:00
112 08:30:00 17:30:00 09:00:00
302 09:00:00 18:30:00 09:30:00
303 10:00:00 18:35:00 08:35:00
you can use
select DATEDIFF(day,2007-11-30,2007-11-20) AS NumberOfDays,
DATEDIFF(hour,2007-11-30,2007-11-20) AS NumberOfHours,
DATEDIFF(minute,2007-11-30,2007-11-20) AS NumberOfMinutes from
test_table
to split u can use
substring(AttDateFirst,charindex(' ',AttDateFirst)+1 ,
len(AttDateFirst)) as [FirstTime]