SQL Week over Week query for given timeframe - sql

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.

Related

Matching date with calculated DATEADD date

I am trying to create a table with columns containing the current date, prior year date, and additional column for the total sum revenue as below:
cur_date | py_date | py_rev
I'm trying to compare revenue across any daily period across years. Assume all dates and revenue values are included in the same SQL Server table.
I attempted to use a case statement using [date] = DATEADD(wk,-52,[date]) as the condition to return the appropriate total. The full line code is below:
select
[date] as cur_date,
DATEADD(wk,-52,[date]) py_date,
SUM(case when [date] = DATEADD(wk,-52,[date]) then sum_rev else 0 end) as py_rev
from summary
group by [date]
When running this code the py_date is as expected but py_rev returns 0 as if there is no match. What's most confusing is if I hard code a random date in place of the DATEADD portion then a total is returned. I have also tried CAST to format both date and the DATEADD portion as date with no luck.
Is there something about DATEADD that will not match to other date columns that I'm missing?
If you want the previous years revenue, then lag() is one method. This works assuming that "previous year" means 52 weeks ago and you have records for all dates:
select [date] as cur_date,
dateadd(week, -52, [date]) as py_date,
lag(sum_rev, 52 * 7) over (order by date) as py_rev
from summary;
If you do not have records for all dates, then another approach is needed. You can use a LEFT JOIN:
select s.date, dateadd(week, -52, s.[date]),
sprev.sum_rev as py_rev
from summary s left join
summary sprev
on sprev.date = dateadd(week, -52, s.[date]);

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()?

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())

Can we do partition date wise?

I am new to the concept of Partition. I know horizontal partition but can we do partition date - wise?
in my project I want that whenever we enter in new-year, partition should be created. Can anyone explain how to do this? I am working on ERP sw and it has data of past year and I need partition on year wise.(for example APR-2011 to MAR-2012 is a year)
I think what you are looking for is something like this:
DECLARE #referenceDate datetime = '3/1/2011'
SELECT
[sub].[Period] + 1 AS [PeriodNr],
YEAR(DATEADD(YEAR, [sub].[Period], #referenceDate)) AS [PeriodStartedIn],
COUNT(*) AS [NumberOfRecords],
SUM([sub].[Value]) AS [TotalValue]
FROM
(
SELECT
*,
FLOOR(DATEDIFF(MONTH, #referenceDate, [timestamp]) / 12.0) AS [Period]
FROM [erp]
WHERE [timestamp] >= #referenceDate
) AS [sub]
GROUP BY [sub].[Period]
ORDER BY [sub].[Period] ASC
The fiddle is found here.
When you say partitioning I am curious if you mean in a windowed function or just grouping data in general. Let me show you two ways to aggregate data with partitioning it in a self demonstrating example:
declare #Orders table( id int identity, dt date, counts int)
insert into #Orders values ('1-1-12', 2),('1-1-12', 3),('1-18-12', 1),('2-11-12', 5),('3-1-12', 2),('6-1-12', 8),('10-1-12', 2),('1-13-13', 8)
-- To do days I need to do a group by
select
dt as DayDate
, SUM(counts) as sums
from #Orders
group by dt
-- To do months I need to group differently
select
DATEADD(month, datediff(month, 0, dt), 0) as MonthDate
-- above is a grouping trick basically stating count from 1/1/1900 the number of months of difference from my date field.
--This will always yield the current first day of the month of a date field
, SUM(counts) as sums
from #Orders
group by DATEADD(month, datediff(month, 0, dt), 0)
-- well that is great but what if I want to group different ways all at once?
-- why windowed functions rock:
select
dt
, counts
, SUM(counts) over(partition by DATEADD(year, datediff(year, 0, dt), 0)) as YearPartitioning
, SUM(counts) over(partition by DATEADD(month, datediff(month, 0, dt), 0)) as MonthPartitioning
-- expression above will work for year, month, day, minute, etc. You just need to add it to both the dateadd and datediff functions
, SUM(counts) over(partition by dt) as DayPartitioning
from #Orders
The important concepts on grouping is the traditional group by clause which you MUST LIST that which is NOT performing a math operation on as a pivot to do work on. So in my first select I just chose date and then said sum(counts). It then saw on 1-1-12 it had two values so it added both of them and on everything else it added them individually. On the second select method I perform a trick on the date field to make it transform to the first day of the month. Now this is great but I may want all of this at once.
Windowed functions do groupings inline, meaning they don't need a grouping clause as that is what the over() portion is doing. It however may repeat the values since you are not limiting your dataset. This means that if you look at the third column of the third select 'YearPartitioning' it repeats the number 23 seven times. WHy? Well because you never told the statement to do any grouping outside the function so it is showing every row. The number 23 will occur as long as the expression is true that the year is the same for all values. Just remember this when selecting from a windowed expression.

Calculating Open incidents per month

We have Incidents in our system with Start Time and Finish Time and project name (and other info) .
We would like to have report: How many Incidents has 'open' status per month per project.
Open status mean: Not finished.
If incident is created in December 2009 and closed in March 2010, then it should be included in December 2009, January and February of 2010.
Needed structure should be like this:
Project Year Month Count
------- ------ ------- -------
Test 2009 December 2
Test 2010 January 10
Test 2010 February 12
....
In SQL Server:
SELECT
Project,
Year = YEAR(TimeWhenStillOpen),
Month = DATENAME(month, MONTH(TimeWhenStillOpen)),
Count = COUNT(*)
FROM (
SELECT
i.Project,
i.Incident,
TimeWhenStillOpen = DATEADD(month, v.number, i.StartTime)
FROM (
SELECT
Project,
Incident,
StartTime,
FinishTime = ISNULL(FinishTime, GETDATE()),
MonthDiff = DATEDIFF(month, StartTime, ISNULL(FinishTime, GETDATE()))
FROM Incidents
) i
INNER JOIN master..spt_values v ON v.type = 'P'
AND v.number BETWEEN 0 AND MonthDiff - 1
) s
GROUP BY Project, YEAR(TimeWhenStillOpen), MONTH(TimeWhenStillOpen)
ORDER BY Project, YEAR(TimeWhenStillOpen), MONTH(TimeWhenStillOpen)
Briefly, how it works:
The most inner subselect, that works directly on the Incidents table, simply kind of 'normalises' the table (replaces NULL finish times with the current time) and adds a month difference column, MonthDiff. If there can be no NULLs in your case, just remove the ISNULL expression accordingly.
The outer subselect uses MonthDiff to break up the time range into a series of timestamps corresponding to the months where the incident was still open, i.e. the FinishTime month is not included. A system table called master..spt_values is also employed there as a ready-made numbers table.
Lastly, the main select is only left with the task of grouping the data.
A useful technique here is to create either a table of "all" dates (clearly that would be infinite so I mean a sufficiently large range for your purposes) OR create two tables: one of all the months (12 rows) and another of "all" years.
Let's assume you go for the 1st of these:
create table all_dates (d date)
and populate as appropriate. I'm going to define your incident table as follows
create table incident
(
incident_id int not null,
project_id int not null,
start_date date not null,
end_date date null
)
I'm not sure what RDBMS you are using and date functions vary a lot between them so the next bit may need adjusting for your needs.
select
project_id,
datepart(yy, all_dates.d) as "year",
datepart(mm, all_dates.d) as "month",
count(*) as "count"
from
incident,
all_dates
where
incident.start_date <= all_dates.d and
(incident.end_date >= all_dates.d or incident.end_date is null)
group by
project_id,
datepart(yy, all_dates.d) year,
datepart(mm, all_dates.d) month
That is not going to quite work as we want as the counts will be for every day that the incident was open in each month. To fix this we either need to use a subquery or a temporary table and that really depends on the RDBMS...
Another problem with it is that, for open incidents it will show them against all future months in your all_dates table. adding a all_dates.d <= today solves that. Again, different RDBMSs have different methods of giving back now/today/systemtime...
Another approach is to have an all_months rather than all_dates table that just has the date of first of the month in it:
create table all_months (first_of_month date)
select
project_id,
datepart(yy, all_months.first_of_month) as "year",
datepart(mm, all_months.first_of_month) as "month",
count(*) as "count"
from
incident,
all_months
where
incident.start_date <= dateadd(day, -1, dateadd(month, 1, first_of_month)
(incident.end_date >= first_of_month or incident.end_date is null)
group by
project_id,
datepart(yy, all_months.first_of_month),
datepart(mm, all_months.first_of_month)