i need update fields 'ErrCode' from select between 2 table. i try query it's update all record.
Table LeaveEmp
EmpRun Date Start Stop
00003 2012-10-02 00:00:00.000 2012-10-02 13:00:00.000 2012-10-02 17:00:00.000
00004 2012-10-02 00:00:00.000 NULL NULL
Table modifyTime
EmpNo cDate cIn cOut ErrCode
00001 2012-10-02 00:00:00.000 NULL NULL NULL
00002 2012-10-02 00:00:00.000 2012-10-02 07:35:00.000 2012-10-02 20:12:00.000 NULL
00003 2012-10-02 00:00:00.000 2012-10-02 13:00:00.000 2012-10-02 17:00:00.000 NULL
00004 2012-10-02 00:00:00.000 NULL NULL NULL
00005 2012-10-02 00:00:00.000 2012-10-02 07:13:00.000 2012-10-02 19:24:00.000 NULL
00006 2012-10-02 00:00:00.000 2012-10-02 07:55:00.000 2012-10-02 20:58:00.000 NULL
*****00003,00004 from Table LeaveEmp.step one i insert Table LeaveEmp to Table modifyTime**
code:
UPDATE modifyTime
SET ErrCode = '3'
WHERE EXISTS (SELECT coalesce(l.Start,'2012-10-02')
FROM LeaveEmp l
WHERE l.Start = '2012-10-02' and modifyTime.cDate = '2012-10-02')
this get output:
EmpNo cDate cIn cOut ErrCode
00001 2012-10-02 00:00:00.000 NULL NULL 3
00002 2012-10-02 00:00:00.000 2012-10-02 07:35:00.000 2012-10-02 20:12:00.000 3
00003 2012-10-02 00:00:00.000 2012-10-02 13:00:00.000 2012-10-02 17:00:00.000 3
00004 2012-10-02 00:00:00.000 NULL NULL 3
00005 2012-10-02 00:00:00.000 2012-10-02 07:13:00.000 2012-10-02 19:24:00.000 3
00006 2012-10-02 00:00:00.000 2012-10-02 07:55:00.000 2012-10-02 20:58:00.000 3
but i need output:
EmpNo cDate cIn cOut ErrCode
00001 2012-10-02 00:00:00.000 NULL NULL NULL
00002 2012-10-02 00:00:00.000 2012-10-02 07:35:00.000 2012-10-02 20:12:00.000 NULL
00003 2012-10-02 00:00:00.000 2012-10-02 13:00:00.000 2012-10-02 17:00:00.000 3
00004 2012-10-02 00:00:00.000 NULL NULL 3
00005 2012-10-02 00:00:00.000 2012-10-02 07:13:00.000 2012-10-02 19:24:00.000 NULL
00006 2012-10-02 00:00:00.000 2012-10-02 07:55:00.000 2012-10-02 20:58:00.000 NULL
i need update from Table LeaveEmp only.
Thanks you fou your time. :)
You are missing a link between modifyTime and LeaveEmp... try this
UPDATE modifyTime
SET ErrCode = '3'
FROM modifyTime m
INNER JOIN LeaveEmp l
ON m.EmpNo = l.EmpRun
WHERE l.Start = '2012-10-02' and modifyTime.cDate = '2012-10-02'
Related
I have a data like this
RID Region StartDate EndDate
944 Canada 2016-01-09 00:00:00.000 2016-01-16 23:59:59.000
955 Canada 2016-01-17 00:00:00.000 2016-01-24 23:59:59.000
981 Canada 2016-02-01 00:00:00.000 2016-02-08 23:59:59.000
996 Canada 2016-02-09 00:00:00.000 2016-02-16 23:59:59.000
1006 Canada 2016-01-25 00:00:00.000 2016-01-31 23:59:59.000
1020 Canada 2016-02-17 00:00:00.000 2016-02-24 23:59:59.000
1030 Canada 2016-02-25 00:00:00.000 2016-02-29 23:59:59.000
1041 Canada 2016-03-01 00:00:00.000 2016-03-08 23:59:59.000
1046 Canada 2016-03-09 00:00:00.000 2016-03-16 23:59:59.000
1062 Canada 2016-03-17 00:00:00.000 2016-03-24 23:59:59.000
1073 Canada 2016-03-24 00:00:00.000 2016-03-31 23:59:59.000
1083 Canada 2016-04-01 00:00:00.000 2016-04-08 23:59:59.000
1105 Canada 2016-04-09 00:00:00.000 2016-04-16 23:59:59.000
1118 Canada 2016-04-17 00:00:00.000 2016-04-24 23:59:59.000
1128 Canada 2016-04-25 00:00:00.000 2016-04-30 23:59:59.000
1164 Canada 2016-05-01 00:00:00.000 2016-05-08 23:59:59.000
now i try to select data like this
select * from tab1 where Region='Canada'
and StartDate ='2016-01-09 00:00:00.000'
and EndDate ='2016-01-24 23:59:59.000'
desired result is
RID Region StartDate EndDate
944 Canada 2016-01-09 00:00:00.000 2016-01-16 23:59:59.000
955 Canada 2016-01-17 00:00:00.000 2016-01-24 23:59:59.000
but when i execute this query data is empty
any solution?
I think you were intending to restrict to a date range, but you actually restricted to two points in time instead. Try this query:
SELECT *
FROM tab1
WHERE Region = 'Canada' AND
StartDate >= '2016-01-09 00:00:00.000' AND
EndDate <= '2016-01-24 23:59:59.000'
Try this.
SELECT *
FROM tab1
WHERE Region = 'Canada'
AND StartDate >='2016-01-09 00:00:00.000'
AND EndDate <='2016-01-24 23:59:59.000'
The 'between' must work. I tried this. If in case it is not working, try convert function for those datetime columns.
SELECT *
FROM tab1
WHERE Region = 'Canada' AND
StartDate >= convert(datetime,'2016-01-09 00:00:00.000') AND
EndDate <= convert(datetime,'2016-01-24 23:59:59.000')
I need to extract and split data from a membership table.
I want to split the range to get one line per year.
DateFrom and dateTo can be any day of the year but when dates are split, we assume that a row ends on december 31 and a new row start on january 1st
Here's a look of the data
membershipId - groupId - ClientId - DateFrom - DateTo
2707 20008 1579 1997-01-01 00:00:00.000 1997-12-31 00:00:00.000
20989 20008 1579 1999-01-01 00:00:00.000 2004-12-31 00:00:00.000
39874 20298 1579 2005-01-01 00:00:00.000 2008-12-31 00:00:00.000
50295 21661 1579 2009-01-01 00:00:00.000 2009-12-31 00:00:00.000
50988 20399 1579 2010-01-01 00:00:00.000 2010-12-31 00:00:00.000
52378 21661 1579 2011-01-01 00:00:00.000 2013-12-31 00:00:00.000
57274 21660 1579 2014-01-01 00:00:00.000 3000-01-01 00:00:00.000
The expected result is : (every range split)
2707 20008 1579 1997-01-01 00:00:00.000 1997-12-31 00:00:00.000
20989 20008 1579 1999-01-01 00:00:00.000 1999-12-31 00:00:00.000
20989 20008 1579 2000-01-01 00:00:00.000 2000-12-31 00:00:00.000
20989 20008 1579 2001-01-01 00:00:00.000 2001-12-31 00:00:00.000
20989 20008 1579 2002-01-01 00:00:00.000 2002-12-31 00:00:00.000
20989 20008 1579 2003-01-01 00:00:00.000 2003-12-31 00:00:00.000
20989 20008 1579 2004-01-01 00:00:00.000 2004-12-31 00:00:00.000
50295 21661 1579 2009-01-01 00:00:00.000 2009-12-31 00:00:00.000
50988 20399 1579 2010-01-01 00:00:00.000 2010-12-31 00:00:00.000
52378 21661 1579 2011-01-01 00:00:00.000 2011-12-31 00:00:00.000
52378 21661 1579 2012-01-01 00:00:00.000 2012-12-31 00:00:00.000
52378 21661 1579 2013-01-01 00:00:00.000 2013-12-31 00:00:00.000
57274 21660 1579 2014-01-01 00:00:00.000 3000-01-01 00:00:00.000
I tried to use recursive CTE based on this :
Possible recursive CTE query using date ranges
But I cannot achieve the desired result.
I made this query :
WITH splitDates(startDate,endDate, newDate,client, groupingId ) as
(
SELECT m.datefrom as startDate, m.dateTo
, CASE
when year(m.dateFrom) <> year(m.dateto) then CAST(CAST(year(m.dateFrom) AS varchar) + '-' + CAST(12 AS varchar) + '-' + CAST(31 AS varchar) AS DATETIME)
else m.dateTo
end
, m.legalEntityId, m.groupingId
from adesse.dbo.membership m
UNION ALL
SELECT DATEADD(year, 1, startDate),
CAST(CAST(year(startDate)+1 AS varchar) + '-' + CAST(12 AS varchar) + '-' + CAST(31 AS varchar) AS DATETIME)
,CAST(CAST(year(startDate)+1 AS varchar) + '-' + CAST(12 AS varchar) + '- ' + CAST(31 AS varchar) AS DATETIME)
,client, groupingId
FROM splitDates
WHERE year(startDate) <> year(endDate)
)
SELECT *
FROM splitDates
where client = 1579
order by startDate
But the result is incomplete :(
startDate endDate newDate client groupingId
1997-01-01 00:00:00.000 1997-12-31 00:00:00.000 1997-12-31 00:00:00.000 1579 20008
1999-01-01 00:00:00.000 2004-12-31 00:00:00.000 1999-12-31 00:00:00.000 1579 20008
2000-01-01 00:00:00.000 2000-12-31 00:00:00.000 2000-12-31 00:00:00.000 1579 20008
2005-01-01 00:00:00.000 2008-12-31 00:00:00.000 2005-12-31 00:00:00.000 1579 20298
2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 2006-12-31 00:00:00.000 1579 20298
2009-01-01 00:00:00.000 2009-12-31 00:00:00.000 2009-12-31 00:00:00.000 1579 21661
2010-01-01 00:00:00.000 2010-12-31 00:00:00.000 2010-12-31 00:00:00.000 1579 20399
2011-01-01 00:00:00.000 2013-12-31 00:00:00.000 2011-12-31 00:00:00.000 1579 21661
2012-01-01 00:00:00.000 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 1579 21661
2014-01-01 00:00:00.000 3000-01-01 00:00:00.000 2014-12-31 00:00:00.000 1579 21660
2015-01-01 00:00:00.000 2015-12-31 00:00:00.000 2015-12-31 00:00:00.000 1579 21660
Thx for the help
I'm not sure if your last date is suppose to be 3000-01-01 but this should work
CREATE TABLE members (membershipId INT, groupId INT, clientId INT, dateFrom DATETIME, dateTo DATETIME)
INSERT INTO members VALUES
(2707, 20008, 1579, '1997-01-01 00:00:00.000', '1997-12-31 00:00:00.000'),
(20989, 20008, 1579, '1999-01-01 00:00:00.000', '2004-12-31 00:00:00.000'),
(39874, 20298, 1579, '2005-01-01 00:00:00.000', '2008-12-31 00:00:00.000'),
(50295, 21661, 1579, '2009-01-01 00:00:00.000', '2009-12-31 00:00:00.000'),
(50988, 20399, 1579, '2010-01-01 00:00:00.000', '2010-12-31 00:00:00.000'),
(52378, 21661, 1579, '2011-01-01 00:00:00.000', '2013-12-31 00:00:00.000'),
(57274, 21660, 1579, '2014-01-01 00:00:00.000', '3000-01-01 00:00:00.000')
;
WITH cte AS
(
SELECT
membershipId,
groupId,
clientId,
dateFrom,
DATEADD(day, -1, DATEADD(YEAR,1,dateFrom)) newDateTo,
dateTo
FROM
members
UNION ALL
SELECT
m.membershipId,
m.groupId,
m.clientId,
DATEADD(YEAR,1,c.dateFrom),
DATEADD(day, -1, DATEADD(YEAR,2,c.dateFrom)),
c.dateto
FROM
members m
JOIN cte c ON c.membershipId = m.membershipId
AND DATEADD(YEAR,1,c.dateFrom) < m.dateTo
)
SELECT
membershipId,
groupId,
clientId,
dateFrom,
newDateTo dateTo
FROM
cte
ORDER BY
membershipId, dateFrom
OPTION (MAXRECURSION 0);
DROP TABLE members
SQL Fiddle
hi friend i have following my table structure and data
CREATE TABLE [dbo].[Pairs_Details](
[sno] [int] IDENTITY(1,1) NOT NULL,
[userid] [nvarchar](50) NULL,
[date] [datetime] NULL,
[ljoin] [int] NULL,
[rjoin] [int] NULL
) ON [PRIMARY]
sno userid date ljoin rjoin
1 LDS 2014-02-17 00:00:00.000 1 NULL
2 LDS 2014-02-17 00:00:00.000 NULL 1
3 LDS1 2014-02-18 00:00:00.000 1 NULL
4 LDS 2014-02-18 00:00:00.000 1 NULL
5 LDS1 2014-02-18 00:00:00.000 NULL 1
6 LDS 2014-02-18 00:00:00.000 1 NULL
7 SUNIL1 2014-02-19 00:00:00.000 1 NULL
8 LDS1 2014-02-19 00:00:00.000 1 NULL
9 LDS 2014-02-19 00:00:00.000 1 NULL
10 SUNIL1 2014-02-19 00:00:00.000 NULL 1
11 LDS1 2014-02-19 00:00:00.000 1 NULL
12 LDS 2014-02-19 00:00:00.000 1 NULL
13 SUNIL2 2014-02-19 00:00:00.000 1 NULL
14 LDS1 2014-02-19 00:00:00.000 NULL 1
15 LDS 2014-02-19 00:00:00.000 1 NULL
16 rajesh123 2014-02-19 00:00:00.000 1 NULL
17 SUNIL1 2014-02-19 00:00:00.000 NULL 1
18 LDS1 2014-02-19 00:00:00.000 1 NULL
19 LDS 2014-02-19 00:00:00.000 1 NULL
20 SUNIL2 2014-02-19 00:00:00.000 NULL 1
21 LDS1 2014-02-19 00:00:00.000 NULL 1
22 LDS 2014-02-19 00:00:00.000 1 NULL
23 LDS2 2014-02-19 00:00:00.000 1 NULL
24 LDS 2014-02-19 00:00:00.000 NULL 1
25 SUNIL1 2014-02-20 00:00:00.000 NULL 1
26 LDS1 2014-02-20 00:00:00.000 1 NULL
27 LDS 2014-02-20 00:00:00.000 1 NULL
28 rajesh123 2014-02-20 00:00:00.000 NULL 1
29 SUNIL1 2014-02-20 00:00:00.000 NULL 1
30 LDS1 2014-02-20 00:00:00.000 1 NULL
31 LDS 2014-02-20 00:00:00.000 1 NULL
32 LDS 2014-02-24 00:00:00.000 NULL 1
33 Jitendra123 2014-02-27 00:00:00.000 1 NULL
34 LDS2 2014-02-27 00:00:00.000 1 NULL
35 LDS 2014-02-27 00:00:00.000 NULL 1
36 rajeev123 2014-02-27 00:00:00.000 1 NULL
37 Jitendra123 2014-02-27 00:00:00.000 1 NULL
40 jyoti123 2014-02-27 00:00:00.000 1 NULL
41 SUNIL1 2014-02-27 00:00:00.000 1 NULL
42 LDS1 2014-02-27 00:00:00.000 1 NULL
43 LDS 2014-02-27 00:00:00.000 1 NULL
44 meeta 2014-03-01 00:00:00.000 1 NULL
45 jyoti123 2014-03-01 00:00:00.000 1 NULL
46 SUNIL1 2014-03-01 00:00:00.000 1 NULL
47 LDS1 2014-03-01 00:00:00.000 1 NULL
48 LDS 2014-03-01 00:00:00.000 1 NULL
38 LDS2 2014-02-27 00:00:00.000 1 NULL
39 LDS 2014-02-27 00:00:00.000 NULL 1
and this is my stored procdure .
create proc [dbo].[pair_Scounting]
(
#userid nvarchar(50),
#start_date datetime,
#end_date datetime
)
as
begin
SET DATEFIRST 1;
SELECT userid,
Sum(ISNULL(ljoin,0)) AS ljoin,
Sum(ISNULL(rjoin,0)) AS rjoin, DATEPART(wk, Date) AS WeekNumber,
CASE
WHEN YEAR(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date]))) < YEAR(Min([date]))
THEN CAST(DATEADD(YEAR, DATEDIFF(YEAR, 0,DATEADD(YEAR, 0 ,GETDATE())), 0) AS Varchar(50)) + ' TO ' + Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date])) AS Varchar(50))
ELSE
Cast(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date])) AS Varchar(50)) + ' TO ' + Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date])) AS Varchar(50))
END DateRange,
Case
When Sum(ISNULL(ljoin,0)) > Sum(ISNULL(rjoin,0)) Then Sum(ISNULL(ljoin,0))-Sum(ISNULL(rjoin,0))
End LeftCary,
Case
When Sum(ISNULL(rjoin,0)) > Sum(ISNULL(ljoin,0)) Then Sum(ISNULL(rjoin,0))-Sum(ISNULL(ljoin,0))
End RightCary
FROM Pairs_Details where userid=#userid and date between #start_date and #end_date
Group By userid,DATEPART(wk, Date)
end
GO
if i execute my stored procedure as follows it return following result
exec pair_Scounting 'LDS','2014-02-17','2014-02-28'
userid ljoin rjoin WeekNumber DateRange LeftCary RightCary
LDS 10 2 8 17-02-2014 TO 23-02-2014 8 NULL
LDS 1 3 9 24-02-2014 TO 2-03-2014 NULL 2
so how we can add this LeftCary (8) with ljoin of next DateRange(24-02-2014 To 2-03-2014)and
RightCary(2)with rjoin of next DateRange(4-03-2014 TO 10-03-2014)
means here ljoin of DateRange(24-02-2014 TO 2-03-2014) is 1 and LeftCary of DateRange( 17-02-2014 TO 23-02-2014) is 8 so total ljoin of DateRange((24-02-2014 TO 2-03-2014)) is 8+1=9
My Desired result as follows
userid ljoin rjoin WeekNumber DateRange LeftCary RightCary
LDS 10 2 8 17-02-2014 TO 23-02-2014 8 NULL
LDS 9 3 9 24-02-2014 TO 2-03-2014 NULL 2
please any one can suggest us how this can be done
thanks
I would turn the stored procedure into an inline table valued function instead and join it on itself. The following presumes that you won't have duplicate WeekNumbers:
SELECT x.userid,
(x.ljoin + ISNULL((SELECT y.LeftCarry FROM dbo.InLine(#param1, #param2, #param3) AS y WHERE y.WeekNumber = (x.WeekNumber - 1)), 0)) AS ljoin_carry,
(x.rjoin + ISNULL((SELECT z.RightCarry FROM dbo.InLine(#param1, #param2, #param3) AS z WHERE z.WeekNumber = (x.WeekNumber - 1)), 0)) AS rjoin_carry,
x.WeekNumber,
x.DateRange,
x.LeftCarry,
x.RightCarry
FROM dbo.InLine(#param1, #param2, #param3) AS x
ORDER BY x.WeekNumber
i need show datetime select only. But show all datetime in table filesTA.
this code:
SELECT *
FROM filesTA tf
WHERE NOT tf.EmpNo
IN (
SELECT lr.EmployeeRun
FROM LeaveRecord lr
WHERE lr.StartDate = '2012-10-01'
)
Output:
EmpNo | ChkDate | ChkIn | ChkOut
00001 | 2012-10-01 00:00:00.000 | 2012-10-01 07:21:00.000 | 2012-10-01 12:21:00.000
00002 | 2012-10-01 00:00:00.000 | 2012-10-01 08:13:00.000 | 2012-10-01 19:55:00.000
00003 | 2012-10-15 00:00:00.000 | 2012-10-15 07:06:00.000 | 2012-10-15 20:12:00.000
00004 | 2012-10-22 00:00:00.000 | 2012-10-22 07:12:00.000 | 2012-10-22 19:15:00.000
I need Output:
EmpNo | ChkDate | ChkIn | ChkOut
00001 | 2012-10-01 00:00:00.000 | 2012-10-01 07:21:00.000 | 2012-10-01 12:21:00.000
00002 | 2012-10-01 00:00:00.000 | 2012-10-01 08:13:00.000 | 2012-10-01 19:55:00.000
THANKS FOR YOUR TIME.
SELECT *
FROM filesTA tf
WHERE tf.EmpNo NOT
IN (
SELECT lr.EmployeeRun
FROM LeaveRecord lr
WHERE lr.StartDate = '2012-10-01'
)
I need show datetime when I select datetime only. Because I try to run SQL but show all datetime.
Table Emp:
EmpNo fullName
00001 Midna
00002 Klog
00003 Porla
00004 Seka
00005 Mila
Table tFile:
EmpNo cDate cTime
00001 2012-10-29 00:00:00.000 2012-10-29 07:52:00.000
00001 2012-10-29 00:00:00.000 2012-10-29 19:00:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 07:40:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 19:32:00.000
00005 2012-10-29 00:00:00.000 2012-10-29 07:58:00.000
00005 2012-10-29 00:00:00.000 2012-10-29 18:35:00.000
This code
SELECT
em.EmpNo as 'EmpNo',
case
when tf.cDate <> null then tf.cDate
else coalesce(tf.cDate, '2012-10-29')
end as 'cDate',
Min(tf.cTime) as 'timeIn',
Max(tf.cTime) as 'timeOut'
FROM
tFile tf
Full Outer join
Emp em On tf.EmpNo = em.EmpNo
Group By
em.EmpNo,tf.cDate
Order By
'EmpNo'
returns this result:
EmpNo cDate timeIn timeOut
-------------------------------------------------------------------------------------
00001 2012-10-21 00:00:00.000 2012-10-21 07:22:00.000 2012-10-21 17:35:00.000
00001 2012-10-24 00:00:00.000 2012-10-24 07:30:00.000 2012-10-24 19:00:00.000
00001 2012-10-29 00:00:00.000 2012-10-29 07:52:00.000 2012-10-29 19:00:00.000
00002 2012-10-25 00:00:00.000 2012-10-25 07:58:00.000 2012-10-25 18:35:00.000
00002 2012-10-22 00:00:00.000 2012-10-22 08:04:00.000 2012-10-22 17:55:00.000
00002 2012-10-24 00:00:00.000 2012-10-24 08:00:00.000 2012-10-24 18:45:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 07:40:00.000 2012-10-29 19:32:00.000
00003 2012-10-29 00:00:00.000 NULL NULL
00004 2012-10-29 00:00:00.000 NULL NULL
00005 2012-10-28 00:00:00.000 2012-10-28 07:30:00.000 2012-10-28 19:20:00.000
00005 2012-10-27 00:00:00.000 2012-10-27 07:38:00.000 2012-10-27 19:30:00.000
00005 2012-10-29 00:00:00.000 2012-10-29 07:58:00.000 2012-10-29 18:35:00.000
But I need this result:
I select date ex. 2012-10-29 then I need to show all rows with 2012-10-29 only.
But some Empno don't have data in 2012-10-29 it's set NULL.
EmpNo cDate timeIn timeOut
---------------------------------------------------------------------------------------
00001 2012-10-29 00:00:00.000 2012-10-29 07:52:00.000 2012-10-29 19:00:00.000
00002 2012-10-29 00:00:00.000 2012-10-29 07:40:00.000 2012-10-29 19:32:00.000
00003 2012-10-29 00:00:00.000 NULL NULL
00004 2012-10-29 00:00:00.000 NULL NULL
00005 2012-10-29 00:00:00.000 2012-10-29 07:58:00.000 2012-10-29 18:35:00.000
Thanks for your time. :)
how about this?
where(datediff(dd, coalesce(cTime, getdate()), getdate()) = 0
or, if you really only ever want to look at October 29th:
where(datediff(dd, coalesce(cTime, '2012-10-29'), '2012-10-29') = 0