Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare @t table (employeeid int, datecol date, status varchar(2) )
- insert into @t values (10001, '01-05-2016', 'P'),
- (10001, '02-05-2016', 'P'),
- (10001, '03-05-2016', 'P'),
- (10001, '04-05-2016', 'P'),
- (10001, '05-05-2016', 'A'),
- (10001, '06-05-2016', 'P'),
- (10001, '07-05-2016', 'P'),
- (10001, '08-05-2016', 'L'),
- (10002, '07-05-2016', 'P'),
- (10002, '08-05-2016', 'L')
- --select * from @t
- select * ,
- SUM(case when status = 'P' then 1 else 0 end) OVER (PARTITION BY employeeid ORDER BY employeeid, datecol
- ROWS BETWEEN UNBOUNDED PRECEDING
- AND current row)
- from
- @t
- ;with cte as
- (
- select employeeid , datecol , ROW_NUMBER() over(partition by employeeid order by employeeid, datecol) rowno
- from
- @t where status = 'P'
- )
- select t.*, cte.rowno ,
- case when ( isnull(cte.rowno, 0) = 0)
- then LAG(cte.rowno) OVER (ORDER BY t.employeeid, t.datecol)
- else cte.rowno
- end LagValue
- from @t t left join cte on t.employeeid = cte.employeeid and t.datecol = cte.datecol
- order by t.employeeid, t.datecol
- SELECT
- main.EmployeeID,
- main.[Date],
- main.[Status],
- (
- SELECT SUM(CASE WHEN t.[Status] = 'P' THEN 1 ELSE 0 END)
- FROM [TableName] t
- WHERE t.EmployeeID = main.EmployeeID AND t.[Date] <= main.[Date]
- ) as TotalPresent
- FROM [TableName] main
- ORDER BY
- main.EmployeeID,
- main.[Date]
- select *
- , (select count(retail) from p g
- where g.date <= p.date and g.id = p.id and retail = 'P')
- from p
- order by ID, Date;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement