how to display individual resolvers within a team on a monthly basis - sql-server-2008

I've currently got an SQL view that shows how many members of each team have made a resolution on each day since April 2017 using the below code. How would I amend this so I could do this by month rather than on a daily basis?
SELECT CAST(stat_datetimeresolved as date) as DateResolved, ResolvedByTeam, COUNT(DISTINCT resolvedby) as ResolvedByCnt
FROM [dbo].[Incident]
WHERE Stat_DateTimeResolved >= '20170401'
GROUP BY CAST(Stat_DateTimeResolved as DATE), ResolvedByTeam
ORDER BY CAST(stat_datetimeresolved as DATE) asc, ResolvedByTeam

You can format the date to be shown as year and month only:
select format(getdate(), 'yyyyMM')
And then group by this value. So your query should become something like this:
SELECT FORMAT(stat_datetimeresolved, 'yyyyMM') as MonthResolved, ResolvedByTeam, COUNT(DISTINCT resolvedby) as ResolvedByCnt
FROM [dbo].[Incident]
WHERE Stat_DateTimeResolved >= '20170401'
GROUP BY FORMAT(stat_datetimeresolved, 'yyyyMM'), ResolvedByTeam
ORDER BY MonthResolved asc, ResolvedByTeam
UPDATE: For pre-2012 versions, the year-month part can be constructed using DATEPART function. In this case the query could look like this:
SELECT CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2) as MonthResolved, ResolvedByTeam, COUNT(DISTINCT resolvedby) as ResolvedByCnt
FROM [dbo].[Incident]
WHERE stat_datetimeresolved >= '20170401'
GROUP BY CAST(DATEPART(year, stat_datetimeresolved) as varchar(4)) + '-' + RIGHT('0' + CAST(DATEPART(month, stat_datetimeresolved) as varchar(2)), 2), ResolvedByTeam
ORDER BY MonthResolved asc, ResolvedByTeam
or
SELECT convert(char(6), Stat_DateTimeResolved , 112) as MonthResolved, ResolvedByTeam, COUNT(DISTINCT resolvedby) as ResolvedByCnt
FROM [dbo].[Incident]
WHERE Stat_DateTimeResolved >= '20170401'
GROUP BY convert(char(6), Stat_DateTimeResolved , 112), ResolvedByTeam
ORDER BY MonthResolved asc, ResolvedByTeam

Related

Redshift SQL Query Between Current Date and 7 days ago

I've been trying to filter the data for the last X number of days.
All these columns work as standalone results when I remove the time filter from the where clause.
I keep on getting the error of no results when I join and add a time filter in where clause.
SELECT x.datex, Signups, Page_load FROM (SELECT CAST (mp_date AS DATE) AS datex, mp_event_name, COUNT(DISTINCT mp_device_id) AS Signups
FROM mp_master_event
WHERE mp_event_name = 'email_page_submit' AND datex between CURRENT_DATE AND CURRENT_DATE - INTERVAL '7 DAY'
GROUP BY mp_event_name, datex
ORDER BY datex DESC) x JOIN (SELECT CAST(mp_date AS DATE) AS datex, mp_event_name, COUNT(DISTINCT mp_device_id) AS Page_load
FROM mp_master_event
WHERE mp_event_name = 'home_page_load_confirm' AND datex between CURRENT_DATE AND CURRENT_DATE - INTERVAL '7 DAY'
GROUP BY datex, mp_event_name
ORDER BY datex DESC) y ON x.datex = y.datex
What should I do? Also since the table is huge, it's taking a lot of time to query and this is just 10% of what I want to achieve with the whole query. Any suggestions are appreciated.
Got it :)
datex >= DATE(dateadd(DAY,-7, current_date))
SELECT x.datex, Signups, Page_load FROM (SELECT CAST (mp_date AS DATE) AS datex, mp_event_name, COUNT(DISTINCT mp_device_id) AS Signups
FROM mp_master_event
WHERE mp_event_name = 'email_page_submit' AND datex >= DATE(dateadd(DAY,-7, current_date))
GROUP BY mp_event_name, datex
ORDER BY datex DESC) x JOIN (SELECT CAST(mp_date AS DATE) AS datex, mp_event_name, COUNT(DISTINCT mp_device_id) AS Page_load
FROM mp_master_event
WHERE mp_event_name = 'home_page_load_confirm' AND datex >= DATE(dateadd(DAY,-7, current_date))
GROUP BY datex, mp_event_name
ORDER BY datex DESC) y ON x.datex = y.datex
datex between CURRENT_DATE AND CURRENT_DATE - INTERVAL '7 DAY'
should be
datex between CURRENT_DATE - INTERVAL '7 DAY' AND CURRENT_DATE
earlier date should be first

SQL select date range by month and year

How do I select date range from this query?
E.g : From Month('2017-05-22') And Year(date1) = Year('2017-05-22')
to Month('2018-05-22') And Year(date1) = Year('2018-05-22')
My current query :
SELECT
date1
FROM
data
WHERE
Month(date1) = Month('2018-05-22') And Year(date1) = Year('2018-05-22')
I'd convert those literals to dates using str_to_date:
SELECT MONTH(date1)
FROM data
WHERE date1 BETWEEN STR_TO_DATE('2017-05-22', '%Y-%m-%d') AND STR_TO_DATE('2018-05-23', '%Y-%m-%d')
Note that between is inclusive on the first argument and exclusive on the second, so I bumped the day to the 23rd.
BETWEEN condition to retrieve values within a date range.
For example:
SELECT *
FROM order_details
WHERE order_date BETWEEN CAST('2014-02-01' AS DATE) AND CAST('2014-02-28' AS DATE);
This MySQL BETWEEN condition example would return all records from the order_details table where the order_date is between Feb 1, 2014 and Feb 28, 2014 (inclusive). It would be equivalent to the following SELECT statement:
SELECT *
FROM order_details
WHERE order_date >= CAST('2014-02-01' AS DATE)
AND order_date <= CAST('2014-02-28' AS DATE);
SELECT TO_CHAR(systemts, 'yyyy-mm') as yyyymm,
max(notenum) - min(notenum) + 1 as notenum_range
FROM your_table_name
WHERE systemts >= to_date('2018-01-01', 'yyyy-mm-dd') AND
systemts < to_date('2018-07-17', 'yyyy-mm-dd')
GROUP BY TO_CHAR(systemts, 'yyyy-mm');
DECLARE #monthfrom int=null,
#yearfrom int=null,
#monthto int=null,
#yearto int=null
/**Region For Create Date on Behalf of Month & Year**/
DECLARE #FromDate DATE=NULL
DECLARE #ToDate DATE=NULL
SET #FromDate=DateAdd(day,0, DateAdd(month, #monthfrom - 1,DateAdd(Year, #yearfrom-1900, 0)))
SET #ToDate=DateAdd(day,-1, DateAdd(month, #monthto - 0,DateAdd(Year, #yearto-1900, 0)))
/**Region For Create Date on Behalf of Month & Year**/
SELECT DISTINCT month ,Year FROM tbl_Att
WHERE (DATEADD(yy, year - 1900, DATEADD(m, Month - 1, 1 - 1)) BETWEEN CONVERT(DATETIME, #FromDate, 102) AND
CONVERT(DATETIME, #ToDate, 102))

Teradata date difference

Hi i have used the following query for finding date difference ,but i am getting one hour difference between source and target, but when i checked manually for the dates in excel value is matching.
select a.CASE_ID as CASE_ID,a.FST_QUE_TIME_IN_SECStest as
FST_QUE_TIME_IN_SECS from (select
distinct CC.CASE_ID as CASE_ID,
CC.CASE_STS_CD,
CC.CRT_DTTM_PST,
CC.REC_DTTM_PST,
MIN(case when CC.CASE_STS_CD ='Open' then CC.CRT_DTTM_PST end )as
FST_QUE_TIME_IN_SECS1,
MIN(case when CC.CASE_STS_CD ='Open' then CC.REC_DTTM_PST end ) as
FST_QUE_TIME_IN_SECS2,
(FST_QUE_TIME_IN_SECS1-FST_QUE_TIME_IN_SECS2 DAY(4) to SECOND) as
FST_QUE_TIME_IN_SECS,
cast(extract (day from FST_QUE_TIME_IN_SECS) * 86400 + extract(hour from
FST_QUE_TIME_IN_SECS) * 3600 + extract(minute from
FST_QUE_TIME_IN_SECS)
*60 + extract(second from FST_QUE_TIME_IN_SECS) as INTEGER)as
FST_QUE_TIME_IN_SECStest
FROM EDW_KATAMARI_T.CNTCT_CASE CC
where CC.CASE_ID='14424461'
group by 1,2,3,4
)
a
where a.FST_QUE_TIME_IN_SECStest is not NULL
minus
select
CASE_ID,
FST_QUE_TIME_IN_SECS
from EDW_KATAMARI_T.CNTCT_CASE_SUMRY
where CASE_ID='14424461'

sql table: sum up over intervals of 10 seconds

I'm looking for a solution for my statistical problem:
I have a table with one date column in it. Now what I want to do is count the number of rows in a certain time interval of 10 seconds:
SELECT count(id), ... AS interval WHERE ... GROUP BY interval
Anybody an idea how I can perform this?
Thanks,
Markus
SELECT
CONCATENATE(
SUBSTR(
DATE_FORMAT(yourtimestamp, '%Y%m%d%H%i%s')
, 1, -1)
, 0) AS time_bin,
COUNT(*)
FROM yourtable
WHERE ...
GROUP BY CONCATENATE(
SUBSTR(
DATE_FORMAT(yourtimestamp, '%Y%m%d%H%i%s')
, 1, -1)
, 0)
(use str_to_date() to convert back to a timestamp)
or...
SELECT FROM_UNIXTIME(
10*FLOOR(UNIX_TIMESTAMP(yourtimestamp)/10)
) AS time_bin,
COUNT(*)
FROM yourtable
WHERE ...
GROUP BY FROM_UNIXTIME(
10*FLOOR(UNIX_TIMESTAMP(yourtimestamp)/10)
)

Sort a Grouped query

I am using the following query to display a list of months for which my database has data.
SELECT DATENAME(MONTH, EVENT_DATE) + ' ' + DATENAME(YEAR, EVENT_DATE) AS MonthYear
FROM [ODA].[dbo].[REPORT_CASE_EXT]
GROUP BY DATENAME(MONTH, EVENT_DATE) + ' ' + DATENAME(YEAR, EVENT_DATE)
If I add the following sort to the query, I get an error because they don't appear in the groupby
SORT BY DATENAME(YEAR, EVENT_DATE), DATENAME(MONTH, EVENT_DATE)
Is there a way to accomplish this sort?
It would "work" if you would use the same in the ORDER BY as in the GROUP BY.
ORDER BY
DATENAME(MONTH, EVENT_DATE) + ' ' + DATENAME(YEAR, EVENT_DATE)
But you need to add more informations to the GROUP BY to be able to order in chronological order instead of alphabetically(otherwise February is before January):
GROUP BY
datename(YEAR, EVENT_DATE),
datename(MONTH, EVENT_DATE),
datepart(YEAR, EVENT_DATE),
datepart(MONTH, EVENT_DATE)
ORDER BY
datepart(YEAR, EVENT_DATE),
datepart(MONTH, EVENT_DATE)