I have a query that will return the total number of calls I received each day of last month for a queue. if no calls it will return zero for that day. However when running this report for for December of 2013 the results are blank. It appears to be due to the fact the query is returning the dates with the year of 2014 instead of 2013. How can I adjust the query so the date is 12/01/2013, etc.
DECLARE #pMnth int,#pYr int,#pQueue varchar
SET #pMnth = '12'
SET #pYr = '2013'
SET #pQueue = 'Queue Name'
;
WITH
CTE_Days AS
(
SELECT DATEADD(month, #pMnth, DATEADD(month, -MONTH(GETDATE()), DATEADD(day, -DAY(GETDATE()) + 1, CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)))) as Dt
UNION ALL
SELECT DATEADD(day, 1, Dt)
FROM CTE_Days
WHERE Dt < DATEADD(day, -1, DATEADD(month, 1, DATEADD(month, #pMnth, DATEADD(month, -MONTH(GETDATE()), DATEADD(day, -DAY(GETDATE()) + 1, CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME))))))
)
SELECT tbl1.Dt,ISNULL(Calls,0) AS Calls
FROM CTE_Days tbl1
LEFT JOIN(
SELECT CAST(clmdate AS Date) AS ClDt,COUNT(*) AS Calls
FROM dbo.tblcalls
WHERE DATEPART(yyyy,clmdate) = #pYr
AND DATEPART(mm,clmdate) = #pMnth
AND clmqueue = #pQueue
GROUP BY CAST(clmdate AS Date)
) tbl2 ON tbl1.Dt = tbl2.ClDt
Not really sure what you're trying to do with all the variables, but if you want the first day of last month:
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0) -- < SQL 2012
SELECT DATEADD(day,1,EOMONTH(GETDATE(),-2)) -- SQL 2012
Last day of last month:
SELECT DATEADD(day,-1,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)) -- < SQL 2012
SELECT EOMONTH(GETDATE(),-1) -- SQL 2012
In your code (assuming you're not on 2012):
DECLARE #pMnth int,#pYr int,#pQueue varchar
SET #pMnth = '12'
SET #pYr = '2013'
SET #pQueue = 'Queue Name'
;
WITH
CTE_Days AS
(
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0) as Dt
UNION ALL
SELECT DATEADD(day, 1, Dt)
FROM CTE_Days
WHERE Dt < DATEADD(day,-1,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))
)
SELECT tbl1.Dt,ISNULL(Calls,0) AS Calls
FROM CTE_Days tbl1
LEFT JOIN(
SELECT CAST(clmdate AS Date) AS ClDt,COUNT(*) AS Calls
FROM dbo.tblcalls
WHERE DATEPART(yyyy,clmdate) = #pYr
AND DATEPART(mm,clmdate) = #pMnth
AND clmqueue = #pQueue
GROUP BY CAST(clmdate AS Date)
) tbl2 ON tbl1.Dt = tbl2.ClDt
Update:
To incorporate your variables, I would change the month and year variables to strings, and replace GETDATE() with a date variable:
DECLARE #pMnth CHAR(2)
,#pYr CHAR(4)
,#pQueue VARCHAR(MAX)
,#dt DATE
SET #pMnth = '12'
SET #pYr = '2013'
SET #pQueue = 'Queue Name'
SET #dt = CAST(#pYr+RIGHT('0'+#pMnth,2)+'01' AS DATE)
WITH
CTE_Days AS
(
SELECT DATEADD(month, DATEDIFF(month, 0, #dt)-1, 0) as Dt
UNION ALL
SELECT DATEADD(day, 1, Dt)
FROM CTE_Days
WHERE Dt < DATEADD(day,-1,DATEADD(month, DATEDIFF(month, 0, #dt), 0))
)
SELECT tbl1.Dt,ISNULL(Calls,0) AS Calls
FROM CTE_Days tbl1
LEFT JOIN(
SELECT CAST(clmdate AS Date) AS ClDt,COUNT(*) AS Calls
FROM dbo.tblcalls
WHERE DATEPART(yyyy,clmdate) = #pYr
AND DATEPART(mm,clmdate) = #pMnth
AND clmqueue = #pQueue
GROUP BY CAST(clmdate AS Date)
) tbl2 ON tbl1.Dt = tbl2.ClDt
Related
The following TSQL returns last 6 month count of data, but if a month does not have records it does not just return 0 as I need it to do, how can I make it do so?
declare #thismonth as Date = DateAdd(d, 1 - DatePart(d, CURRENT_TIMESTAMP), CURRENT_TIMESTAMP);
declare #firstmonth as Date = DateAdd(m, -6, #thismonth);
SELECT YEAR(StartDate) year,
MONTH(StartDate) month,
COUNT(StartDate) EmployeeStartet
FROM
EFP_EmploymentUser
WHERE StartDate BETWEEN #firstmonth AND #thismonth
GROUP BY YEAR(StartDate), MONTH(StartDate)
ORDER BY YEAR(StartDate) DESC, MONTH(StartDate) DESC;
You could do something like this:
declare #lastMonth as Date = DateAdd(d, 1 - DatePart(d, CURRENT_TIMESTAMP), CURRENT_TIMESTAMP);
declare #firstMonth as Date = DateAdd(m, -6, #lastMonth);
WITH months AS (
SELECT #firstMonth AS thisMonth
UNION ALL
SELECT DateAdd(m, 1, thisMonth) AS thisMonth
FROM months
WHERE thisMonth < #lastMonth
),
data AS (
-- your sql here
SELECT YEAR(StartDate) year,
MONTH(StartDate) month,
COUNT(StartDate) EmployeeStartet
FROM
EFP_EmploymentUser
WHERE StartDate BETWEEN #firstmonth AND #thismonth
GROUP BY YEAR(StartDate), MONTH(StartDate)
)
SELECT
YEAR(m.thisMonth) AS year,
MONTH(m.thisMonth) AS month,
ISNULL(d.EmployeeStartet, 0) AS EmployeeStartet
FROM months m
LEFT OUTER JOIN data d
ON
d.year = YEAR(m.thisMonth)
AND d.month = MONTH(m.thisMonth)
ORDER BY m.thisMonth DESC
Left join your data to a tally
declare #thismonth as Date = DateAdd(d, 1 - DatePart(d, CURRENT_TIMESTAMP), CURRENT_TIMESTAMP);
declare #firstmonth as Date = DateAdd(m, -6, #thismonth);
with m as (
select #firstMonth md union
select DateAdd(m, 1, #firstMonth) union
select DateAdd(m, 2, #firstMonth) union
select DateAdd(m, 3, #firstMonth) union
select DateAdd(m, 4, #firstMonth) union
select DateAdd(m, 5, #firstMonth)
),
ym as (
select YEAR(md) y, MONTH(md) m
from m
)
select ym.y, ym.m, COUNT(e.StartDate) EmployeeStartet
from ym
left join EFP_EmploymentUser e
ON (e.StartDate BETWEEN #firstmonth AND #thismonth)
AND ym.y = YEAR(e.StartDate) AND ym.m = MONTH(e.StartDate)
GROUP BY ym.y, ym.m
ORDER BY ym.y DESC, ym.m DESC;
I have two dates from 2019/07/26 and 2019/08/08 from these dates i want result
Year Month ActualDays WorkingDays
------------------------------------
2019 07 06 04
2019 08 08 06
DECLARE #start DATETIME, #end DATETIME
SET #start = '2019-07-26'
SET #end = '2019-08-08'
;WITH c(d) AS
(
SELECT TOP (DATEDIFF(DAY, #start, #end) + 1 )
DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY name)-1, #start)
FROM
sys.all_columns
)
SELECT
YEAR(t.[date]), MONTH(t.[date]), t.[days]
FROM
(SELECT
[date] = DATEADD(MONTH, DATEDIFF(MONTH, 0, d), 0),
[days] = COUNT(*)
FROM
c
GROUP BY
DATEDIFF(MONTH, 0, d)
UNION ALL
SELECT
d, NULL
FROM
c) AS t
WHERE
t.[days] IS NOT NULL
From this I will get days group by month and year I need working days
You can try this-
DECLARE #startdate DATE = '20190701'
, #enddate DATE = '20190716'
DECLARE #temp TABLE(thedate DATE, DoCount INT);
WITH CTE (thedate) AS
(
SELECT #startdate
UNION ALL
SELECT DATEADD(day, 1, thedate)
FROM CTE
WHERE thedate < #enddate
)
INSERT INTO #temp
SELECT thedate,CASE WHEN DATEPART(dw, [thedate]) IN (1,7) THEN 0 ELSE 1 END
FROM CTE
SELECT YEAR(thedate),MONTH(thedate),
COUNT(DoCount) AS ActualDays,
SUM(DoCount) WorkingDays
FROM #temp
GROUP BY YEAR(thedate),
MONTH(thedate)
I'm trying to write a stored procedure which groups up rows based on their month and return a sum of all items if they exist and 0 if they don't.
For the date part of the query, what I am trying to get is today's date - extract the month and go back 5 months to gather any data if it exists.
At this stage, the query runs fine as is but I'm wondering if there's any way to optimise this as it looks like I'm running the same set of data over and over again and also it's hard coded to an extent.
The dataset I am trying to achieve is as follows:
Month TotalAmount TotalCount
-----------------------------------
2017-11 0 0
2017-12 200.00 2
2018-01 300.00 3
2018-02 0 0
2018-03 300.00 3
2018-04 100.00 1
Using the following query below, I was able to achieve what I want but as you can see, it's hard coding back the past 5 months so if I wanted to go back 12 months, I'd have to add in more code.
DECLARE #5MonthAgo date = CAST(DATEADD(MONTH, -5, GETDATE()) + 1 - DATEPART(DAY, DATEADD(MONTH, -5, GETDATE())) AS DATE)
DECLARE #4MonthAgo date = CAST(DATEADD(MONTH, -4, GETDATE()) + 1 - DATEPART(DAY, DATEADD(MONTH, -4, GETDATE())) AS DATE)
DECLARE #3MonthAgo date = CAST(DATEADD(MONTH, -3, GETDATE()) + 1 - DATEPART(DAY, DATEADD(MONTH, -3, GETDATE())) AS DATE)
DECLARE #2MonthAgo date = CAST(DATEADD(MONTH, -2, GETDATE()) + 1 - DATEPART(DAY, DATEADD(MONTH, -2, GETDATE())) AS DATE)
DECLARE #1MonthAgo date = CAST(DATEADD(MONTH, -1, GETDATE()) + 1 - DATEPART(DAY, DATEADD(MONTH, -1, GETDATE())) AS DATE)
DECLARE #CurrentMonth date = CAST(GETDATE() + 1 - DATEPART(DAY, GETDATE()) AS DATE)
-- Table to return grouped and sum data
DECLARE #StatsTable TABLE ([Month] DATE,
[Total Amount] DECIMAL(18,2),
[Total Count] INT
)
-- Temporary table to hold onto data batch - so table isn't used later on
DECLARE #TempGenTable TABLE ([Id] INT,
[Date] DATETIME,
[Lines] INT NULL,
[Amount] DECIMAL(18, 2) NULL
)
INSERT INTO #TempGenTable
SELECT
Id, Date, Lines, Amount
FROM
TallyTable
WHERE
Date >= #5MonthAgo
INSERT INTO #StatsTable
SELECT
#5MonthAgo,
COALESCE((SELECT SUM(Amount)
FROM #TempGenTable
WHERE Date >= #5MonthAgo AND Date < #4MonthAgo
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Date), 0)), 0),
COALESCE((SELECT COUNT(Id)
FROM #TempGenTable
WHERE Date >= #5MonthAgo AND Date < #4MonthAgo
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Date), 0)), 0)
UNION
SELECT
#4MonthAgo,
COALESCE((SELECT SUM(Amount)
FROM #TempGenTable
WHERE Date >= #4MonthAgo AND Date < #3MonthAgo
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Date), 0)), 0),
COALESCE((SELECT COUNT(Id)
FROM #TempGenTable
WHERE Date >= #4MonthAgo AND Date < #3MonthAgo
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Date), 0)), 0)
...
Is there an easier way to be able to get the above data with more flexibility in the number of months?
Is it better to just have the query pass in a month variable and it checks just the current month and have a loop within the controller to go back x number of months?
I would generate the data using a recursive CTE and then use left join:
with months as (
select datefromparts(year(getdate()), month(getdate()), 1) as month_start, 5 as n
union all
select dateadd(month, -1, month_start), n - 1
from months
where n > 0
)
select m.month_start, count(s.id), sum(s.amount)
from months m left join
#StatsTable s
on m.month_start = s.month
group by m.month_start
order by m.month_start;
You haven't provided sample data, so I'm not sure what s.month looks like. You might want the join condition to be:
on s.month >= m.month_start and s.month < dateadd(month, 1, m.month_start)
Below is a set-based method to generate the needed monthly periods:
--sample data
CREATE TABLE dbo.TallyTable (
Id int
, Date datetime
, Lines int
, Amount decimal(18, 2)
);
INSERT INTO dbo.TallyTable
VALUES
(1, '2017-12-05', 1, 50.00)
,(2, '2017-12-06', 1, 150.00)
,(3, '2018-01-10', 1, 100.00)
,(4, '2018-01-11', 1, 100.00)
,(5, '2018-01-12', 1, 100.00)
,(6, '2018-03-15', 1, 225.00)
,(7, '2018-03-15', 1, 25.00)
,(8, '2018-03-15', 1, 50.00)
,(9, '2018-04-20', 1, 100.00);
GO
DECLARE #Months int = 5; --number of historical months
WITH
t10 AS (SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t(n))
,t100 AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) - 1 AS num FROM t10 AS a CROSS JOIN t10 AS b)
, periods AS (SELECT
CONVERT(varchar(7), DATEADD(month, DATEDIFF(month, '', GETDATE()) - num, ''),121) AS Month
, DATEADD(month, DATEDIFF(month, '', CAST(GETDATE() AS date)) - num, '') AS PeriodStart
, DATEADD(month, DATEDIFF(month, '', CAST(GETDATE() AS date)) - num + 1, '') AS NextPeriodStart
FROM t100
WHERE num <= #Months
)
SELECT periods.Month, COALESCE(SUM(Amount), 0) AS TotalAmount, COALESCE(COUNT(ID), 0) AS TotalCount
FROM periods
LEFT JOIN dbo.TallyTable ON
TallyTable.Date >= PeriodStart
AND TallyTable.Date < NextPeriodStart
GROUP BY periods.Month
ORDER BY periods.Month;
I'm in the process of writing a stored procedure (for SQL Server 2012) that is supposed to calculate the number of hours for our employee from 16-15th of every month.
I have the following database structure
I have written a stored procedure to calculate the hours but I think I can only get the week start date to filter my condition. The stored procedure is returning me the wrong result because the weekly start date is not always the 16th.
CREATE PROCEDURE [dbo].[spGetTotalHoursBetween16to15EveryMonth]
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
DECLARE #SixteenthDate datetime2(7) = DATEADD(DAY, 15, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
DECLARE #currentDate datetime2(7) = getDate()
DECLARE #LastSixteenthDate datetime2(7) = DATEADD(DAY, 15, DATEADD(MONTH, DATEDIFF(MONTH, 0, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) - 1, 0)), 0))
IF(#currentDate >= #SixteenthDate)
BEGIN
SELECT
(Sum(Day1Hours) + sum(Day2Hours) + Sum(Day3Hours) +
sum(Day4Hours) + Sum(Day5Hours) + sum(Day6Hours) + Sum(Day7Hours)) AS Total
FROM
dbo.TimeSheets
WHERE
WeekStartDate BETWEEN DATEADD(wk, DATEDIFF(wk, 0, #SixteenthDate), -1) AND #currentDate
END
ELSE
BEGIN
SELECT
(Sum(Day1Hours) + sum(Day2Hours) + Sum(Day3Hours) +
sum(Day4Hours) + Sum(Day5Hours) + sum(Day6Hours) + Sum(Day7Hours)) AS Total
FROM
dbo.TimeSheets
WHERE
WeekStartDate BETWEEN DATEADD(wk, DATEDIFF(wk, 0, #LastSixteenthDate), -1) AND #currentDate
END
END TRY
BEGIN CATCH
THROW
END CATCH
END
I'd probably just do it the simple way:
declare #today date = convert(date,current_timestamp)
declare #prev_month_end date = dateadd( day , -day(#today) , #today )
declare #period_start date = dateadd( day , 16 , #prev_month_end ) -- 16th of THIS month
declare #period_end date = dateadd( month , 1 , #period_start ) -- 16th of NEXT month
select #period_start = dateadd(month, -1 , #period_start ) ,
#period_end = dateadd(month, -1 , #period_end )
where day(#today) < 16
select total_hours = coalesce(sum(t.hours),0)
from ( select id = t.id , report_date = dateadd(day,0,t.WeekStartDate) , hours = t.Day1Hours from dbo.TimeSheets t
union all select id = t.id , report_date = dateadd(day,1,t.WeekStartDate) , hours = t.Day2Hours from dbo.TimeSheets t
union all select id = t.id , report_date = dateadd(day,2,t.WeekStartDate) , hours = t.Day3Hours from dbo.TimeSheets t
union all select id = t.id , report_date = dateadd(day,3,t.WeekStartDate) , hours = t.Day4Hours from dbo.TimeSheets t
union all select id = t.id , report_date = dateadd(day,4,t.WeekStartDate) , hours = t.Day5Hours from dbo.TimeSheets t
union all select id = t.id , report_date = dateadd(day,5,t.WeekStartDate) , hours = t.Day6Hours from dbo.TimeSheets t
union all select id = t.id , report_date = dateadd(day,6,t.WeekStartDate) , hours = t.Day7Hours from dbo.TimeSheets t
) t
where t.report_date >= #period_start
and t.report_date < #period_end
Always start with start or an end of a month.
E.g. Here's some logic
Start Date = Start date of previous month + 16
End Date = Start date of current month + 15
Following might help you figure out the dates
-- First Day of the month
select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
-- Last Day of previous month
select dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate() ), 0))
More examples are here
Life is easier with a little data normalization. Don't work from the spreadsheet-style table
CREATE VIEW Timesheets_Normalized AS
SELECT
[id]
,[Date] = DATEADD(day,[Date_Offset],[WeekStartDate])
,[Hours]
FROM MyTable a
UNPIVOT([Hours] FOR [Date_Column] IN (Day1Hours,Day2Hours,Day3Hours,Day4Hours,Day5Hours,Day6Hours,Day7Hours)) b
INNER JOIN (VALUES (0,'Day1Hours'),(1,'Day2Hours'),(2,'Day3Hours'),(3,'Day4Hours'),(4,'Day5Hours'),(5,'Day6Hours'),(6,'Day7Hours')) c([Date_Offset],[Date_Column])
ON (b.[Date_Column] = c.[Date_Column])
Then you can get your answers very simply:
SELECT
MIN([Date]) AS [PayrollMonthStart]
,MAX([Date]) AS [PayrollMonthEnd]
,SUM([Hours]) AS [TotalHours]
FROM Timesheets_Normalized
GROUP BY YEAR(DATEADD(day,-15,[Date])),MONTH(DATEADD(day,-15,[Date]))
HAVING CAST(GETDATE() AS date) BETWEEN MIN([Date]) AND MAX([Date])
I want to split date range in months. I will pass startdate(1-jan-2011) and enddate(31-dec-2011) as a parameter then it must return result like
1-jan-2011 - 31-jan-2011
1-feb-2011 - 28-feb-2011
1-mar-2011 - 31-mar-2011
Please send me a stored procedure.....
Thanks,
Abhishek
Try this:
CREATE PROC SplitDateRange
#from DATETIME,
#to DATETIME
AS
BEGIN
SET NOCOUNT ON;
SET #from = CONVERT(VARCHAR, DATEADD(DAY, -DATEPART(DAY, #from)+1, #from), 112)
-- Sql 2000
CREATE TABLE #temp (DateFrom DATETIME, DateTo DATETIME)
WHILE #from < #to
BEGIN
INSERT #temp VALUES (#from, DATEADD(DAY, -1, DATEADD(MONTH, 1, #from)))
SET #from = DATEADD(MONTH, 1, #from)
END
SELECT * FROM #temp
DROP TABLE #temp
--sql 2005+
/*
;WITH Ranges(DateFrom, DateTo) AS
(
SELECT #from DateFrom, DATEADD(DAY, -1, DATEADD(MONTH, 1, #from)) DateTo
UNION ALL
SELECT DATEADD(MONTH, 1, DateFrom), DATEADD(DAY, -1, DATEADD(MONTH, 1, DATEADD(MONTH, 1, DateFrom)))
FROM Ranges
WHERE DateFrom < DATEADD(MONTH, -1, #To)
)
SELECT * FROM Ranges
OPTION(MAXRECURSION 0)
*/
END
GO
EXEC SplitDateRange '2011-01-02', '2012-06-06'
So that you can use the results in another SQL Query (which I assume is where you're going) I'd put that into a table valued function.
Assuming SQL Server 2005+ you could use this...
CREATE FUNCTION dbo.ufnMonthlyIntervals(
#from_date SMALLDATETIME,
#end_date SMALLDATETIME
)
RETURNS TABLE
WITH
intervals (
from_date,
end_date
)
AS
(
SELECT #from_date, DATEADD(MONTH, 1, #from_date ) - 1
UNION ALL
SELECT end_date + 1, DATEADD(MONTH, 1, end_date + 1) - 1 FROM intervals WHERE end_date < #end_date
)
RETURN
SELECT
from_date,
CASE WHEN end_date > #end_date THEN #end_date ELSE end_date END AS end_date
FROM
intervals
Then you just use SELECT * FROM dbo.ufnMonthlyIntervals('20110101', '20111201') AS intervals