I want to select last hour results - sql

SELECT DISTINCT
ip.IPAddress,
CONVERT(varchar, dan.AuditDate, 100) AS Date,
ip.AuditAction,ip.Username,
(CASE
WHEN CONCAT(k.FirstName, ' ', k.LastName) IS NULL OR CONCAT(k.FirstName, ' ', k.LastName) = ' '
THEN k.Name
ELSE CONCAT(k.FirstName, ' ', k.LastName)
END) AS 'Name'
FROM
IPAddress ip
INNER JOIN
user k ON k.ID = ip.PerformerID
INNER JOIN
audit dan ON ip.IPAddress = dan.Value1
AND ip.Date = CAST(dan.AuditDate AS Date)
AND ip.AuditAction = dan.AuditStr
AND ip.PerformerID = dan.PerformerID
INNER JOIN
LoginMoreOne n ON n.LUsername = ip.Username
AND n.LUsername IS NOT NULL
WHERE
LEN(ip.IPAddress) > 8
AND dan.AuditDate > DATEADD(HOUR, DATEDIFF(hour, 0, DATEADD(mi, 30, GETDATE())), 0)
GROUP BY
ip.IPAddress, ip.Date, ip.AuditAction,
ip.Username, ip.PerformerID,
k.FirstName, k.LastName, k.Name,
CAST(dan.AuditDate AS Date), dan.AuditDate
For example if I execute this query at 2.30pm, I want the result from 1pm to 2pm, or if i execute this query at 3.10pm, I want the result from 2pm-3pm, and so on, How do I edit my where statement to be able get the desired result

The expression dateadd(hour, datediff(hour, 0, getdate()), 0) will gives you date & time at current hour, with minutes, seconds = 0
-1 gives you the previous hour with 0 mins, 0 secs
AuditDate >= dateadd(hour, datediff(hour, 0, getdate()) - 1, 0)
and AuditDate < dateadd(hour, datediff(hour, 0, getdate()), 0)

Instead
dan.AuditDate > DATEADD(HOUR, datediff(hour, 0, dateadd(mi, 30, GETDATE())), 0)
Use:
dan.AuditDate > DATEADD(HH, -1.5, GETDATE()) AND dan.AuditDate <= DATEADD(HH,-0.5, GETDATE())

Related

How to get value 0 if the result of sql select except statement return no records?

I have a problem with the SQL Except statement, in my case, I select two columns and one column can have values and the others cannot have because the count of the first SQL statement equals the count of the second SQL after the except statement.
Result that I got :
GroupId absences
1 3
Expected result
GroupId absences
1 3
2 0
How can I do in this case?
select
groupId,
(case when groupId = null then 0 else COUNT(*) end) as absence
from (
select
groupId,
COUNT(*) as absences1
from (
select distinct
MONTh,
DAY,
e.groupId
from employee_c c,
holiday hl,
employee e,
groups,
Get_Calendar_Date(
DATEADD(MONTH, DATEDIFF(MONTH, 0, CAST(DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS datetime)) + #Month - 1, 0),
DATEADD(MONTH, DATEDIFF(MONTH, -1, CAST(DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS datetime)) + #Month-1, -1)
)
where DATENAME(DD, Weekday) IN (
select WorkingdayId+1
from timetable s,
groups ds,
grouptime de
where dd.groupId = ds.groupId
and dd.groupId = de.groupId
and s.timetableId = de.timetableId
and de.groupId = ds.groupdId)
and DATEPART(MM,hl.startDate) = #Month
and c.isActive=1
except
(select Month,
Day,
d.departmentId
from department d,
Get_Calendar_Date(
DATEADD(MONTH, DATEDIFF(MONTH, 0, CAST(DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS datetime)) + #Month - 1, 0),
DATEADD(MONTH, DATEDIFF(MONTH, -1, CAST(DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS datetime)) + #Month - 1, -1)
),
holiday hle,
employee_c c,
employee e
where datepart(MM,hle.startDate) = #Month
and cast(Date as date) between hle.startDate and hle.endDate
and c.isActive=1
and d.groupId =e.groupId
and c.employeeId=e.employeeId
and c.isActive=1
)
) sc
group by Month,Day,groupId
) s
group by groupId

removing nulls in case statement

I am trying to return the current Monday along with previous monday's data. I want to roll up my nulls or 0000 to have one record return.
select
da.Customer,
sum(case
when DAteadd(wk, datediff(wk, 0, Getdate()), 1) = d.FullDate
then (da.[USD - Balance])
else 0
end) as MondayofCurrentWeek,
sum(case
when DAteadd(wk, datediff(wk, 0, Getdate() - 7), 1) = d.FullDate
then (da.[USD - Balance])
else 0
end) as PreviousMonday
from
Dataset_DemandArchive da
inner join
Dimdate d on d.DateNameUS = da.CREDAT_0
where
da.CREDAT_0 in (dateadd(wk, datediff(wk, 0, Getdate()), 1),
dateadd(wk, datediff(wk, 0, Getdate() - 7), 1))
group by
Customer, FullDate
order by
1
Why not just use window functions?
select da.*
from (select da.Customer, d.FullDate,
sum(da.[USD - Balance]) as MondayCurrentWeek
lag(sum(da.[USD - Balance]), 7) over (partition by da.Customer order by d.FullDate)
from Dataset_DemandArchive da join
Dimdate d
on d.DateNameUS = da.CREDAT_0
group by Customer, FullDate
) da
where da.CREDAT_0 = dateadd(week, datediff(week, 0, Getdate()), 1)

How to get count record for every month for year from SQL Server?

I tried this code, it's returning all records with month name, but without total count
SELECT
DATENAME(month, DATEADD(month, MONTH([R_datetime]), -1 )) MonName,
COUNT(*) count
FROM
[Tbl_TechRequest]
WHERE
([R_datetime]) BETWEEN DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)
AND DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1)
GROUP BY
[R_datetime]
SELECT
DATEPART(year, R_datetme),
DATEPART(month, R_datetime),
count (*)
from myTable
where
[R_datetime]) > = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1)
group by
DATEPART(year, R_datetme),
DATEPART(month, R_datetime)
OR
SELECT
DATEPART(year, R_datetme)*100 + DATEPART(month, R_datetime),
count(*)
from myTable
where
[R_datetime]) > = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1)
group by
DATEPART(year, R_datetme)*100 + DATEPART(month, R_datetime)

MS SQL Convert date to short

I am trying to get all of the records from a table where the ExpiryDate field is exactly one month away.
The problem however is that the ExpiryDate is stored with the hours, minutes and seconds. How do I therefore say - Get me all of the records from my table where the ExpiryDate (dd/MM/yyyy) is one month ahead of now (dd/MM/yyyy).
This is what I have currently:
SELECT * FROM dbo.Custom_MembershipTransaction mt (nolock)
WHERE mt.ExpiryDate = DATEADD(MONTH, 1, GETDATE())
AND mt.IsComplete = 1;
You need some buffertime like
SELECT * FROM dbo.Custom_MembershipTransaction mt (nolock)
WHERE mt.ExpiryDate < DATEADD(MONTH, 1, GETDATE())
AND mt.ExpiryDate > DATEADD(DAY, -1, DATEADD(MONTH, 1, GETDATE()))
AND mt.IsComplete = 1;
This will get you all recors that have an expiredate between 1 month ahead an (1 month - 1 day) ahead.
SELECT * FROM dbo.Custom_MembershipTransaction mt (nolock)
WHERE mt.ExpiryDate >= DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(MONTH, 1, GETDATE())), 0)
AND mt.ExpiryDate < DATEADD(DAY, DATEDIFF(DAY, 0, DATEADD(MONTH, 1, GETDATE()) + 1), 0)
AND mt.IsComplete = 1;
I didn't make operation on ExpiryDate as it might be used by an index
Try this:
SELECT * FROM dbo.Custom_MembershipTransaction mt (nolock)
WHERE convert(varchar, mt.ExpiryDate, 102) = convert(varchar, DATEADD(MONTH, 1, GETDATE()),102)
AND mt.IsComplete = 1;

SQL Server 2005 select fields from a certain time frame

Right now I'm using this command to retrieve all fields from the current day:
SELECT COUNT(*)
FROM [SecureOrders]
WHERE DateTime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
AND
DateTime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
However, I want to be able to get the fields that were entered between noon yesterday and noon today - how can I go about doing that?
0.5 is noon (eg half a day)
WHERE DateTime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), -0.5)
AND
DateTime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0.5)
DECLARE #NoonToday DATETIME;
SET #NoonToday = DATEADD(HOUR, 12, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP));
SELECT COUNT(*) FROM [SecureOrders]
WHERE [DateTime] >= DATEADD(DAY, -1, #NoonToday)
AND [DateTime] < #NoonToday;
it might seem ugly but should work
SELECT
COUNT(*)
FROM
[SecureOrders]
WHERE
DateTime >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) - 0.5 AND DateTime < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1) + 0.5
A variation on #Aaron Bertrand's solution, without declaring a variable and without treating a non-zero integer value as a date:
SELECT COUNT(*)
FROM SecureOrders o
CROSS JOIN (
SELECT DATEADD(HOUR, DATEDIFF(DAY, 0, GETDATE()) * 24 + 12, 0)
) AS d (TodayNoon)
WHERE o.DateTime < d.TodayNoon
AND o.DateTime >= DATEADD(DAY, -1, d.TodayNoon)