forever loop sql - sql

I am trying to find all staff not logged on in the last 6 months so I have this code. It seems to be looping forever but I'm not sure why
--select all staff not logged on in the last 6 months
SELECT
[fkStaffId],
[dteLogon]
FROM
[dbo].[tbl_Staff_Logon]
WHERE
[fkStaffId]
NOT IN
(--select all staff logged on in the last 6 months
SELECT DISTINCT
[fkStaffId]
FROM
[dbo].[tbl_Staff_Logon]
WHERE
[dteLogon] > DATEADD(MONTH, -6, GETDATE()) --this is a list of each staffID
--logged on in the last 6 months
)
ORDER BY [dteLogon] DESC
anybody got any idea why?
if i just execute
SELECT DISTINCT
[fkStaffId]
FROM
[dbo].[tbl_Staff_Logon]
WHERE
[dteLogon] > DATEADD(MONTH, -6, GETDATE())
on it's own, I get a sensible sized list of 309 lines which is about the number I'm expecting, however, the last time I tried to run the entire code, it was still executing after 25 minutes!!

I'd do a GROUP BY, and use HAVING to only return users who have not logged in recently:
SELECT
[fkStaffId], MAX([dteLogon])
FROM
[dbo].[tbl_Staff_Logon]
GROUP BY [fkStaffId]
HAVING MAX([dteLogon]) < DATEADD(MONTH, -6, GETDATE())

Related

How to i find records in the last month that do not have a "Date created" column

Im trying to find records that have not had notes created during the last 1 month. The table only registers when a note is created.
I am trying to find NULL values, but that would not be the correct logic
SELECT *
FROM vpersonnotesalldata AS pn
WHERE pn.flddatecreated > '20190501'
AND pn.fldnotedatecreated < '20190530'
If you want records which don't have a note in last 30 days, try this:
select p.* from person p where personid not in (
select personid from Note where dateCreated < dateadd(d, -30, GetDate())
)
Obviously use your actual table names in your sql
Try this with example last month
SELECT *
FROM vpersonnotesalldata AS pn
WHERE pn.NoteColumnHERE IS NULL
AND pn.CreateDateColumnHERE
BETWEEN '01.05.2019'
AND '31.05.2019'
It picks all pn.NoteColumnHERE which are NULL in the Span of pn.CreateDateColumnHERE BETWEEN 01.05. and 31.05.
I hope the Date Input is correct for your SQL Version. In Microsoft SQL it is working!
The performance will be much better if you use EXISTS / NOT EXISTS instead of IN / NOT IN
SELECT *
FROM Client C
WHERE NOT EXISTS (
SELECT 1
FROM vpersonnotesalldata
WHERE
fldClientNumber = C.fldClientNumber
AND fldnotedatecreated BETWEEN
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) --First day of previous month
AND DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1) --Last Day of previous month
)

SQL Week over Week query for given timeframe

I have a table that is used for support tickets. I am trying to create a query that shows a week over week breakdown (how many tickets were created) for the last 8 weeks.
I have a calendar table created already and I am trying to make use of this to create the query.
Example:
SELECT *
FROM dbo.Calendar
WHERE calendar_DT >= DATEADD(week, -8, GETUTCDATE() -1)
AND calendar_DT <= GETUTCDATE()
ORDER BY calendar_DT ASC
Here I have a basic query that gives me all of the dates within the last 8 weeks. From here, I am trying to group this data by week so that I have a weekStart, weekEnd and then a count of records created which I will join another table for.
I attempted to do this without a calendar table but it seems that if records didn't exist for a specific week then theres gaps in the results instead of a 0.
This was my original attempt. Its close to what I want for the end result. However, I am not sure where the null is coming from and its missing a 2 weeks of data where no requests were created during this time.
SELECT DATEADD(week, DATEDIFF(week, 0, createdDate), 0) [From],
DATEADD(week, DATEDIFF(week, 0, createdDate), 0) + 6 [To],
COUNT(DISTINCT reqID) newRequests
FROM dbo.support_tickets
WHERE createdDate >= DATEADD(week, -8, GETUTCDATE())
AND tool = 244
GROUP BY DATEDIFF(week, 0, createdDate) WITH ROLLUP
ORDER BY DATEDIFF(week, 0, createdDate)
Desired result looks like the image above, a total count of records created for each of the last 8 weeks.
Do I need to continue with the use of the calendar table or can it be done without it and still show the to/from even if theres no results for that week?
The NULL row is the result of your GROUP BY...WITH ROLLUP. That's the rollup. The newRequests value of 7 is the total of your other records.
If you want dates to be listed that aren't in your table, you'll need to get them from somewhere else, which is just what a calendar table is for. So you're actually in pretty good shape once you add that join.

SQL Query Statement Return Different Results

Newbie to the forum. I am trying to write a MS SQL query that returns the number of unique user logons in the last 24 hours. I am not a SQL expert. Below are two queries that I have. The 2nd one is and extension of the first where I want to group the result by date and hour. My question is, the 2nd query return result larger than the first. What am I missing? Thanks\
1ST
SELECT COUNT(DISTINCT(RESOURCE_ID)) AS 'UniqueLogonUsers'
FROM live.AUDIT_LOG WITH (NOLOCK)
WHERE AUDIT_TYPE = 0 AND AUDIT_TIME >= DateAdd(hh, -24, GETDATE())
2ND
SELECT CAST(AUDIT_TIME AS DATE) AS 'WhichDate',
DATEPART(hh, AUDIT_TIME) AS 'WhichHour',
COUNT(DISTINCT(RESOURCE_ID)) AS 'UniqueLogonUsers'
FROM live.AUDIT_LOG WITH (NOLOCK)
WHERE AUDIT_TYPE = 0 AND AUDIT_TIME >= DateAdd(hh, -24, GETDATE())
GROUP BY CAST(AUDIT_TIME AS DATE), DATEPART(hh, AUDIT_TIME)
ORDER By CAST(AUDIT_TIME AS DATE) DESC, DATEPART(hh, AUDIT_TIME) DESC
Your first query should only return 1 row - a count of logins in the last 24 hours. Your second breaks that count into rows for each Day/Hour combination, so thus will have more rows - one for each combination, with the counts of the logins during that hour.
Could it be because you are using GETDATE() which would have returned a different value each time? Can you try putting a fixed date in there instead of GETDATE()?

Need to find out last 15 days record (each day which record are last inserted in my table )

Need Help
Lets say--I have a staging table, everyday record are inserted, there is column is T_CREATED, This column has date (ddmmyyy hh24miss) example “12/16/2014 11:47:14 AM”
My scenario was, I need to find out when is last record last 15 days. Which means each day(in 24 hr) which record are last inserted
Can I get suggestion here?
I know I have to use MAX(T_CREATED) but could not figure it out.
Thanks
Sohel
I am not sure about the syntax in Oracle, this code will return the latest T_CREATED for each day.
-- SQL Server
SELECT MAX(obj_ID) AS MaxID, CONVERT(DATE, T_CREATED) AS [Date]
FROM source_tbl
WHERE CONVERT(DATE, T_CREATED) > CONVERT(DATE, DATEADD(DAY, -15, GETDATE()))
GROUP BY CONVERT(DATE, T_CREATED)
ORDER BY CONVERT(DATE, T_CREATED) DESC
-- Oracle
SELECT MAX(T_CREATED) AS MaxID, TRUNC(T_CREATED) AS [Date]
FROM source_tbl
WHERE TRUNC(T_CREATED) > TRUNC(SYSDATE - 15)
GROUP BY TRUNC(T_CREATED)
ORDER BY TRUNC(T_CREATED) DESC

How to choose the 12 prior weeks in SQL

It had a lot of row with NULL value which I deleted using the following query:
/* DELETE EVERY ROW EXCEPT THE ROW WE NEED */
DELETE FROM [Database].[dbo].[Table]
WHERE
([Week Of*] IS NULL)
How do I write a query which will get the prior 12 weeks from the current rolling week and insert the [F4] value into a table
SELECT f4
FROM yourtable
WHERE DATEADD(week, -12, [Week Of*]) >= 12
I ended up using the following which gave me the last 12 weeks (how ever I have to use 14 to get the 12 weeks. Still trying to figure out why:
SELECT CONVERT(VARCHAR(10),[Week Of*],110) AS [Date], [F18] AS [TOTALS] FROM [db].[DBO].[table] WHERE CONVERT(VARCHAR(10),[Week Of*],110) >= DATEADD(week, -14, GETDATE())