Group entries per week where each column got specific date validation - sql

I am trying to group tickets per week in a year when data field X are between that week. The final result should be something like Week,Datefield1,Datefield2..
What I reached so far:
DECLARE #YearStartDate datetime
SET #YearStartDate = '2016-01-04 00:00:00.000'
DECLARE #YearEndDate datetime
SET #YearEndDate = '2016-04-24 00:00:00.000'
SELECT
Year(T.CreateDate) as 'Year',
'Week ' + cast(datepart(wk, CreateDate) as varchar(2)) as 'Week',
Sum(CASE WHEN CreateDate BETWEEN #YearStartDate and #YearEndDate
THEN 1 Else 0 End) 'Created'
FROM mytable AS T
GROUP BY Year(T.CreateDate),datepart(wk,T.CreateDate)
ORDER BY Year(T.CreateDate),datepart(wk, T.CreateDate)
Result:
Year Week Created
----------- ------- -----------
2016 Week 1 0
2016 Week 2 5
2016 Week 3 3
2016 Week 4 2
Goal:
Year Week Created Schuduled Closed
----------- ------- ----------- ----------- -----------
2016 Week 1 0 0 0
2016 Week 2 5 3 2
2016 Week 3 3 2 2
2016 Week 4 2 2 0
Suggestion?
SELECT
Year(..) as 'Year',
'Week ' + cast(datepart(wk, ...) as varchar(2)) as 'Week',
Sum(CASE WHEN CreateDate BETWEEN #YearStartDate and #YearEndDate
THEN 1 Else 0 End) 'Created'
Sum(CASE WHEN ScheduledDate BETWEEN #YearStartDate and #YearEndDate
THEN 1 Else 0 End) 'Scheduled'
Sum(CASE WHEN ClosedDate BETWEEN #YearStartDate and #YearEndDate
THEN 1 Else 0 End) 'Closed'
FROM mytable AS T
GROUP BY Year(...)
ORDER BY Year(...)
Constraints:
I can count the entries per week for one datafield but for several datafields (created,scheduled,pending,closed) I think I need to change the logic. Maybe create a inner join or left join against the same table.
I was wondering if you could give me some guidance on finding the right path. Thanks for taking the time to help me.
All the best.

Right, join on other similar SELECTs for scheduled and closed should work. Something as
DECLARE #YearStartDate datetime = '2016-01-04 00:00:00.000';
DECLARE #YearEndDate datetime = '2016-04-24 00:00:00.000';
SELECT CLR.*, SCH.Schuduled, CLS.Closed FROM
(SELECT
Year(T.CreateDate) as 'Year',
'Week ' + cast(datepart(wk, CreateDate) as varchar(2)) as 'Week',
Sum(CASE WHEN CreateDate BETWEEN #YearStartDate and #YearEndDate THEN 1 Else 0 End) 'Created'
FROM mytable AS T
GROUP BY Year(T.CreateDate),datepart(wk,T.CreateDate)
) CRT
JOIN
(SELECT
Year(T.ScheduleDate) as 'Year',
'Week ' + cast(datepart(wk, ScheduleDate) as varchar(2)) as 'Week',
Sum(CASE WHEN ScheduleDate BETWEEN #YearStartDate and #YearEndDate THEN 1 Else 0 End) 'Schuduled'
FROM mytable AS T
GROUP BY Year(T.ScheduleDate),datepart(wk,T.ScheduleDate)
) SCH
ON CRT.Year = SCH.Year
AND CRT.Week = SCH.Week
JOIN
(SELECT
Year(T.ClosedDate) as 'Year',
'Week ' + cast(datepart(wk, ClosedDate) as varchar(2)) as 'Week',
Sum(CASE WHEN ClosedDate BETWEEN ClosedDate and #YearEndDate THEN 1 Else 0 End) 'Closed'
FROM mytable AS T
GROUP BY Year(T.ClosedDate),datepart(wk,T.ClosedDate)
) CLS
ON CRT.Year = CLS.Year
AND CRT.Week = CLS.Week
ORDER BY Year, Week

You have three different date columns. One method would pre-aggregation along the dimensions and then use full outer join. Alternatively, you can use union all and aggregation:
SELECT Year(dte) as [Year],
'Week ' + datename(wk, dte) as [Week],
Sum(Created) as Created,
Sum(Scheduled) as Scheduled,
Sum(Closed) as Closed
FROM ((SELECT createddate as dte, 1 as created, 0 as schedule, 0 as closed
FROM mytable
) UNION ALL
(SELECT scheduleddate as dte, 0 as created, 1 as schedule, 0 as closed
FROM mytable
) UNION ALL
(SELECT closeddate as date, 1 as created, 0 as schedule, 0 as closed
FROM mytable
)
) t
GROUP BY Year(date), datename(wk, dte)
ORDER BY Year(date), datename(wk, dte);
I would encourage a couple of things:
Do not use single quotes for column aliases. Only use single quotes for string and date names.
Do not use reserved works such as "year" for column or table names.
Also note the use of datename(). This returns a string so no conversion is needed.

Related

Count of days for rest of month returning incorrect value for EOM date

The below code when run for the last day of the month it is giving me a week day count of 1 when it should be 0 - how can I fix it?
Declare #EndDate DateTime = '03-31-2021'
;WITH mycte AS (
SELECT #EndDate + 1 DateValue
UNION ALL
SELECT DateValue + 1
FROM mycte
WHERE DateValue < EOMONTH(#EndDate)
)
select
count(case when datepart(dw, DateValue) = 1 then 1 end) SunCount
, count(case when datepart(dw, DateValue) = 7 then 1 end) SatCount
, count(case when datepart(dw, DateValue) between 2 and 6 then 1 end) WeekCount
from mycte
Your initial CTE is actually creating a date of 2021-04-01 which is a week day, so that's where your count of 1 is coming from. If you want to restrict the counts to just the month in question, you could add a WHERE clause to your end query like this. This way, you get zeros for all counts.
Declare #EndDate DateTime = '03-31-2021'
;WITH mycte AS (
SELECT #EndDate + 1 DateValue
UNION ALL
SELECT DateValue +1
FROM mycte
WHERE DateValue < EOMONTH(#EndDate)
)
select
count(case when datepart(dw, DateValue) = 1 then 1 end) SunCount
, count(case when datepart(dw, DateValue) = 7 then 1 end) SatCount
, count(case when datepart(dw, DateValue) between 2 and 6 then 1 end) WeekDayCount
from mycte
where DATEPART(MM,DateValue)=DATEPART(MM,#EndDate)

Teradata - Split date range into month columns with day count

I need to split different date ranges over a quarter period into month columns with only the days actually used in that month. Each record (range) would be different.
Example:
Table
Record_ID Start_Date End_Date
1 10/27 11/30
2 11/30 12/14
3 12/14 12/31
Range 1 = 10/5 to 12/14
Range 2 = 11/20 to 12/31
Range 3 = 10/28 to 12/2
Output:
Range 1
Oct Nov Dec
27 30 14
Similar to #ULick's answer using sys_calendar.calendar, but a little more succinct:
CREATE VOLATILE MULTISET TABLE datetest (record_id int, start_date date, end_date date) ON COMMIT PRESERVE ROWS;
INSERT INTO datetest VALUES (1, '2017-10-05', '2017-12-14');
INSERT INTO datetest VALUES (2, '2017-11-20','2017-12-31');
SELECT record_id,
SUM(CASE WHEN month_of_year = 10 THEN 1 ELSE 0 END) as October,
SUM(CASE WHEN month_of_year = 11 THEN 1 ELSE 0 END) as November,
SUM(CASE WHEN month_of_year = 12 THEN 1 ELSE 0 END) as December
FROM datetest
INNER JOIN sys_calendar.calendar cal
ON cal.calendar_date BETWEEN start_date and end_date
GROUP BY record_id;
DROP TABLE datetest;
Because Quarter was mentioned in the question (I'm not sure how it relates here) there is also quarter_of_year and month_of_quarter available in the sys_calendar to slice and dice this even further.
Also, if you are on 16.00+ There is PIVOT functionality which may help get rid of the CASE statements here.
First join with the calendar to get all the dates within the range and get the number of days per each month (incl. full month, not mentioned in Start_Date and End_Date).
Then sum up each month in a column per Range.
create table SplitDateRange ( Range bigint, Start_Date date, End_Date date );
insert into SplitDateRange values ( 1, '2018-10-05', '2018-12-14' );
insert into SplitDateRange values ( 2, '2018-11-20', '2018-12-31' );
insert into SplitDateRange values ( 3, '2018-10-28', '2018-12-02' );
select
Range
, sum(case when mon = 10 then days else 0 end) as "Oct"
, sum(case when mon = 11 then days else 0 end) as "Nov"
, sum(case when mon = 12 then days else 0 end) as "Dec"
from (
select
Range
, extract(MONTH from C.calendar_date) as mon
, max(C.calendar_date) - min(calendar_date) +1 as days
from Sys_Calendar.CALENDAR as C
inner join SplitDateRange as DR
on C.calendar_date between DR.Start_Date and DR.End_Date
group by 1,2
) A
group by Range
order by Range
;
Different approach, avoids the cross join to the calendar by applying Teradata Expand On feature for creating time series. More text, but should be more efficient for larger tables/ranges:
SELECT record_id,
Sum(CASE WHEN mth = 10 THEN days_in_month ELSE 0 END) AS October,
Sum(CASE WHEN mth = 11 THEN days_in_month ELSE 0 END) AS November,
Sum(CASE WHEN mth = 12 THEN days_in_month ELSE 0 END) AS December
FROM
( -- this Derived Table simply avoids repeating then EXTRACT/INTERVAL calculations (can't be done directly in the nested Select)
SELECT record_id,
Extract(MONTH From Begin(expanded_pd)) AS mth,
Cast((INTERVAL( base_pd P_INTERSECT expanded_pd) DAY) AS INT) AS days_in_month
FROM
(
SELECT record_id,
PERIOD(start_date, end_date+1) AS base_pd,
expanded_pd
FROM datetest
-- creates one row per month
EXPAND ON base_pd AS expanded_pd BY ANCHOR PERIOD Month_Begin
) AS dt
) AS dt
GROUP BY 1

SQL Year summary starting from given month

I have the following SQL query which Display 12 months of summary report from the given date. Output shows starting from January to December for whatever the given date. I want the outcome to start from the given date's month.
If the given date is '2016-05-01' I want the output to be like this
May 16 |June 16| July 16| ........... | Jan 17 | Feb 17 | March 17 | April 17 |
How can I achieve this?
Can some one suggest?
SELECT Name,SUM(Amount) AS PremiumTot,TotType,
sum(case when month(Dates) = 1 then Tot else 0 end) Jan,
sum(case when month(Dates) = 2 then Tot else 0 end) Feb,
sum(case when month(Dates) = 3 then Tot else 0 end) March,
sum(case when month(Dates) = 4 then Tot else 0 end) April,
sum(case when month(Dates) = 5 then Tot else 0 end) May,
sum(case when month(Dates) = 6 then Tot else 0 end) June,
sum(case when month(Dates) = 7 then Tot else 0 end) July,
sum(case when month(Dates) = 8 then Tot else 0 end) Aug,
sum(case when month(Dates) = 9 then Tot else 0 end) Sep,
sum(case when month(Dates) = 10 then Tot else 0 end) Oct,
sum(case when month(Dates) = 11 then Tot else 0 end) Nov,
sum(case when month(Dates) = 12 then Tot else 0 end) Dece
FROM
(
SELECT InvoiceMasterID,Dates ,Name,CompanyCommission AS Tot ,0 AS Expences,Amount,1 as TotType
FROM CommissionView
UNION ALL
SELECT InvoiceMasterID,Dates,Name, 0 AS Tot ,-AgentCommission AS Expences,Amount,2 as TotType
FROM CommissionViewCredit
) a
WHERE Dates between #fromDates AND Datesadd(yy,1,#fromDates)
GROUP BY Name,TotType
Seems, you want to pivot data. So, use PIVOT table!
If you want to create dynamic columns from given date, use CTE (Common Table Expressions)!
--declare variables for given date range
DECLARE #startDate DATE = '2016-05-01'
DECLARE #endDate DATE = DATEADD(mm,11,#startDate)
--declare variable to store months as: [month1], [month2], [etc.]
DECLARE #Months VARCHAR(1000) = ''
--use cte to create range of dates
;WITH MyDates AS
(
SELECT #startDate AS MyDate
UNION ALL
SELECT DATEADD(mm,1,MyDate) AS MyDate
FROM MyDates
WHERE MyDate<#endDate
)
SELECT #Months = STUFF((SELECT '],[' + CONVERT(VARCHAR(7), MyDate, 121)
FROM MyDates
FOR XML PATH('')), 1, 2, '') + ']'
--PRINT #Months:
-- prints: [2016-05],[2016-06], ... ,[2017-04]
DECLARE #qry NVARCHAR(MAX) = ''
SET #qry = N'SELECT ' + #Months +
' FROM ( ' +
' SELECT CONVERT(VARCHAR(7), Dates, 121) AS MyDate, CompanyCommission AS Tot ' +
'FROM CommissionView ' +
') AS DT ' +
'PIVOT (SUM(Tot) FOR MyDate IN(' + #Months + ')) AS PT'
EXEC (#qry)
For further information, please see:
Dynamic PIVOT
Pivots with dynamic columns
CAST and CONVERT
Good luck!

SQL - How to count records for each status in one line per day?

I have a table Sales
Sales
--------
id
FormUpdated
TrackingStatus
There are several status e.g. Complete, Incomplete, SaveforLater, ViewRates etc.
I want to have my results in this form for the last 8 days(including today).
Expected Result:
Date Part of FormUpdated, Day of Week, Counts of ViewRates, Counts of Sales(complete), Counts of SaveForLater
--------------------------------------
2015-05-19 Tuesday 3 1 21
2015-05-18 Monday 12 5 10
2015-05-17 Sunday 6 1 8
2015-05-16 Saturday 5 3 7
2015-05-15 Friday 67 5 32
2015-05-14 Thursday 17 0 5
2015-05-13 Wednesday 22 0 9
2015-05-12 Tuesday 19 2 6
Here is my sql query:
select datename(dw, FormUpdated), count(ID), TrackingStatus
from Sales
where FormUpdated <= GETDATE()
AND FormUpdated >= GetDate() - 8
group by datename(dw, FormUpdated), TrackingStatus
order by datename(dw, FormUpdated) desc
I do not know how to make the next step.
Update
I forgot to mention, I only need the Date part of the FormUpdated, not all parts.
You can use SUM(CASE WHEN TrackingStatus = 'SomeTrackingStatus' THEN 1 ELSE 0 END)) to get the status count for each tracking status in individual column. Something like this. SQL Fiddle
select
CONVERT(DATE,FormUpdated) FormUpdated,
DATENAME(dw, CONVERT(DATE,FormUpdated)),
SUM(CASE WHEN TrackingStatus = 'ViewRates' THEN 1 ELSE 0 END) c_ViewRates,
SUM(CASE WHEN TrackingStatus = 'Complete' THEN 1 ELSE 0 END) c_Complete,
SUM(CASE WHEN TrackingStatus = 'SaveforLater' THEN 1 ELSE 0 END) c_SaveforLater
from Sales
where FormUpdated <= GETDATE()
AND FormUpdated >= DATEADD(D,-8,GetDate())
group by CONVERT(DATE,FormUpdated)
order by CONVERT(DATE,FormUpdated) desc
You can also use a PIVOT to achieve this result - you'll just need to complete the list of TrackingStatus names in both the SELECT and the FOR, and no GROUP BY required:
WITH DatesOnly AS
(
SELECT Id, CAST(FormUpdated AS DATE) AS DateOnly, DATENAME(dw, FormUpdated) AS DayOfWeek, TrackingStatus
FROM Sales
)
SELECT DateOnly, DayOfWeek,
-- List of Pivoted Columns
[Complete],[Incomplete], [ViewRates], [SaveforLater]
FROM DatesOnly
PIVOT
(
COUNT(Id)
-- List of Pivoted columns
FOR TrackingStatus IN([Complete],[Incomplete], [ViewRates], [SaveforLater])
) pvt
WHERE DateOnly <= GETDATE() AND DateOnly >= GetDate() - 8
ORDER BY DateOnly DESC
SqlFiddle
Also, I think your ORDER BY is wrong - it should just be the Date, not day of week.

SQL Results group by month

I'm trying to return some results spread over a rolling 12 month period eg:
MONTH IN OUT
January 210 191
February 200 111
March 132 141
April 112 141
May 191 188
etc...
How do I spread the results over a date range, populating the first column with the month name?
IN MSSQL it would be something like:
SELECT COUNT(problem.problem_type = 'IN') AS IN,
COUNT(problem.problem_type = 'OUT') AS OUT,
DATEPART(year, DateTime) as Year,
DATEPART(month, DateTime) as Month
FROM problem
WHERE (DateTime >= dbo.FormatDateTime('2010-01-01'))
AND
(DateTime < dbo.FormatDateTime('2010-01-31'))
GROUP BY DATEPART(year, DateTime),
DATEPART(month, DateTime);
But this is against an Oracle database so DATEPART and DateTime are not available.
My Problem table is roughly:
problem_ID Problem_type IN_Date OUT_Date
1 IN 2010-01-23 16:34:29.0 2010-02-29 13:06:28.0
2 IN 2010-01-27 12:34:29.0 2010-01-29 12:01:28.0
3 OUT 2010-02-13 13:24:29.0 2010-09-29 15:04:28.0
4 OUT 2010-02-15 16:31:29.0 2010-07-29 11:03:28.0
Use:
SELECT SUM(CASE WHEN p.problem_type = 'IN' THEN 1 ELSE 0 END) AS IN,
SUM(CASE WHEN p.problem_type = 'OUT' THEN 1 ELSE 0 END) AS OUT,
TO_CHAR(datetime, 'YYYY') AS year,
TO_CHAR(datetime, 'MM') AS month
FROM PROBLEM p
WHERE p.DateTime >= TO_DATE('2010-01-01', 'YYYY-MM-DD')
AND p.DateTime < TO_DATE('2010-01-31', 'YYYY-MM-DD')
GROUP BY TO_CHAR(datetime, 'YYYY'), TO_CHAR(datetime, 'MM')
You could also use:
SELECT SUM(CASE WHEN p.problem_type = 'IN' THEN 1 ELSE 0 END) AS IN,
SUM(CASE WHEN p.problem_type = 'OUT' THEN 1 ELSE 0 END) AS OUT,
TO_CHAR(datetime, 'MM-YYYY') AS mon_year
FROM PROBLEM p
WHERE p.DateTime >= TO_DATE('2010-01-01', 'YYYY-MM-DD')
AND p.DateTime < TO_DATE('2010-01-31', 'YYYY-MM-DD')
GROUP BY TO_CHAR(datetime, 'MM-YYYY')
Reference:
TO_CHAR
TO_DATE
You probably want something like
SELECT SUM( (CASE WHEN problem_type = 'IN' THEN 1 ELSE 0 END) ) in,
SUM( (CASE WHEN problem_type = 'OUT' THEN 1 ELSE 0 END) ) out,
EXTRACT( year FROM DateTime ) year,
EXTRACT( month FROM DateTime ) month
FROM problem
WHERE DateTime >= date '2010-01-01'
AND DateTime < date '2010-01-31'
GROUP BY EXTRACT( year FROM DateTime ),
EXTRACT( month FROM DateTime )