Retrieve Last n number of records in SQL Server 2008
Date : March 29 2020, 07:55 AM
around this issue SELECT TOP n * FROM tblCurrent_locations ORDER BY "Date" DESC, "Time" DESC
SELECT TOP n * FROM tblCurrent_locations ORDER BY [Date] DESC, [Time] DESC
|
Sql Server retrieve last date for multiple records
Date : March 29 2020, 07:55 AM
Hope this helps I have a table with 5 columns Emp, Surname, CSD, Term Rsn and Date. The first 4 columns will stay the same but the date will change eg. There can be 30 records for an Emp with 30 different dates. , Write as below: Create PROCEDURE testPro(
@reportStartDate date,
@reportEndDate date
)
AS
BEGIN
select Emp, Surname, CSD,[Working Days] , Max(Date) as [Date]
From (
SELECT DISTINCT Emp, Surname, CSD, CASE WHEN CSD<= @reportStartDate
THEN
CASE WHEN [Term Rsn] != 0 THEN
CASE WHEN [Date] <= @reportEndDate THEN
(DATEDIFF(dd,@reportStartDate, [Date]) - (2*
DATEDIFF(wk,@reportStartDate,[Date])))+1
ELSE (DATEDIFF(dd,@reportStartDate, @reportEndDate) - (2*
DATEDIFF(wk,@reportStartDate,@reportEndDate)))+1 END
ELSE (DATEDIFF(dd,@reportStartDate, @reportEndDate) - (2*
DATEDIFF(wk,@reportStartDate,@reportEndDate)))+1 END
ELSE
CASE WHEN [Term Rsn] != 0 THEN
CASE WHEN [Date] <= @reportEndDate THEN
(DATEDIFF(dd,CSD, [Date]) - (2*
DATEDIFF(wk,CSD,[Date])))+1
ELSE (DATEDIFF(dd,CSD, @reportEndDate) - (2*
DATEDIFF(wk,CSD,@reportEndDate)))+1 END
ELSE (DATEDIFF(dd,CSD, @reportEndDate) - (2*
DATEDIFF(wk,CSD,@reportEndDate)))+1 END
END AS [Working Days],[Date] -- Add all Dates here
FROM GyPremiumsTemp
WHERE CSD < @reportEndDate AND [Date] > @reportStartDate
) T
Group By Emp, Surname, CSD,[Working Days]
ORDER BY Emp ASC
END
EXEC testPro '2012-01-01', '2012-12-31'
|
How to retrieve multiple records of a particular id in one row using SQL Server
Tag : sql , By : Janne Laine
Date : March 29 2020, 07:55 AM
around this issue using the stuff() with select ... for xml path ('') method of string concatenation. select
issue_num
, issue_desc
, assigned_to_user_id = stuff((
select distinct ','+u.user_id
from Users u
inner join IssueAssigned sia
on u.id = sia.assigned_to_user_id
where sia.issue_num = i.issue_num
for xml path (''), type).value('.','nvarchar(max)')
,1,1,'')
, x.comment
, x.assign_date
from Issues i
cross apply (
select top 1 ia.comment, ia.assign_date
from IssueAssigned ia
where ia.issue_num = i.issue_num
) as x
+-----------+---------------+---------------------+-------------------+---------------------+
| issue_num | issue_desc | assigned_to_user_id | comment | assign_date |
+-----------+---------------+---------------------+-------------------+---------------------+
| abc123 | login issue | ray234,yash123 | replicating issue | 2017-03-15 00:00:00 |
| abc345 | session issue | aniket,ray234 | replicating issue | 2017-03-15 00:00:00 |
| abc334 | logeger issue | aniket | replicating issue | 2017-03-15 00:00:00 |
+-----------+---------------+---------------------+-------------------+---------------------+
|
SQL Server : self join - retrieve records matching the where clause as well as when no records in alias table
Tag : sql , By : Scott Everts
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a table like below: , interpret directly from your 2 conditions in WHERE clause select *
from TABLE2 t
where warehouseid = 576
and (
exists -- condition 1
(
select *
from TABLE2 x
where x.itemcode = t.itemcode
and x.warehouseid = 276
and x.flag = 'Y'
)
or not exists -- condition 2
(
select *
from TABLE2 x
where x.itemcode = t.itemcode
and x.warehouseid = 276
)
)
|
Retrieve parent child records in SQL Server
Tag : sql , By : Marianisho
Date : March 29 2020, 07:55 AM
|