I have a table having attributes: ticket#, closed date & resolve time.
I need to write a SQL query to calculate the average resolve time in each quarter.
eg in quarter1: 5 tickets are closed (10 days,1 day,3, day,1day.10 days) are resolved time for each ticket
then average resolve time is 5 days
output should be as below
Quarter days
Q1 5
Q2 2 (similarly)
Q3 7
Q4 9
sample data
I really stuck in this query
** This code is tested on Oracle Database. For any syntax related error, you can replace according to you database.
Also, update your column name,date,timestamp,timezone and table name accordingly.
The sample query is like this ...
select temp.quarter,avg(resolve_time)
from
(
select resolve_time,
CASE
WHEN (close_date between TO_DATE('2019-APR-01 00:00','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN') and TO_DATE('2019-JUN-30 23:59','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN')) THEN 1
WHEN (close_date between TO_DATE('2019-JUL-01 00:00','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN') and TO_DATE('2019-SEP-30 23:59','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN')) THEN 2
WHEN (close_date between TO_DATE('2019-OCT-01 00:00','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN') and TO_DATE('2019-DEC-31 23:59','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN')) THEN 3
WHEN (close_date between TO_DATE('2020-JAN-01 00:00','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN') and TO_DATE('2020-MAR-31 23:59','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN')) THEN 4
ELSE 0 END quarter
from TestStack
) temp
group by temp.quarter;
You can write something like this.
SELECT AVG(resolvetime) AS 'Average Resolve Time'
FROM [TableName]
WHERE closeddate BETWEEN [Start date of Quarter] AND [End date of Quarter]
Related
Historization means we show add data of previous months to upcoming months.
For eg: Input:
ID STATUS YEAR MONTH
A OPEN 2017-01
A CLOSED 2017-03
B OPEN 2017-01
B Closed 2017-05
My output:
YEAR MONTH COUNT-OPEN COUNT-CLOSED
2017-01 2(both A & B OPEN) 0
2017-02 2(FROM 201701) 0
2017-03 2(from 2017-02)-1(A Closed now)=1 1
2017-04 1(from 201703)=1 1(from prev month)
2017-05 1-1(b closed)=0 1+1(b closed)=2
Values are the actual output I have just written the formula to make you understand the logic.
I need to add data to open/closed from previous months to upcoming months, is it possible in SAP HANA without use of Cursors?
As I can do it with Cursors, If any other logic exist please help me with it!
for the solution of this requirement, I can suggest you to use a numbers table on HANA database. In the following part, I've shared the SQLScript codes in which I've used this numbers table function for creating the series of dates in a specific range
with input as (
select
id,
status,
year,
cast(month as integer) month,
case when status = 'OPEN' then 1 else -1 end as COUNT_OPEN,
case when status = 'CLOSED' then 1 else 0 end as COUNT_CLOSED
from Historization
)
SELECT
y.rownum as year,
m.rownum as month,
(select sum(COUNT_OPEN) from input where year <= y.rownum and month <= m.rownum) as COUNT_OPEN,
(select sum(COUNT_CLOSED) from input where year <= y.rownum and month <= m.rownum) as COUNT_CLOSED
FROM Numbers_Table(2017) as y
CROSS JOIN Numbers_Table(12) as m
where y.rownum = 2017
order by y.rownum, m.rownum
You see I used the numbers function "Numbers_Table" in the outer SELECT statement which creates the FROM part of the join clause
For simulating the COUNTs, I assume that a record cannot be CLOSED before it is OPENED so, when it is in OPEN status I add 1 to OPEN state count.
While the record is closing, I add 1 to CLOSED state. But the trick here is, I use -1 for OPEN state for a closing item.
SUM() aggregation function helps me to get the final row data
The output is as follows,
How do I average the last 6 months of sales within SQL?
Here are my tables and fields:
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR,
And I need to average these fields
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
The hard part I'm having is understanding how to average the last whole 6 months, ie. fsicalcalperiod 2-6(inside fiscalcalyear 2017).
I'm hoping for some help on what the SQL command text should look like since I'm very new to manipulating SQL outside of the UI.
Sample Data
My Existing SQL String:
SELECT IM_ItemWhseHistoryByPeriod.ITEMCODE,
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR
FROM MAS_AME.dbo.IM_ItemWhseHistoryByPeriod
IM_ItemWhseHistoryByPeriod
ScaisEdge Attempt #1
if fiscalyear and fiscalperiod are number you could use
select avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
or for each item code
select itemcode, avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
group by itemcode
Try the following solution and see if it works for you:
select avg(DOLLARSSOLD) as AvgDollarSod,
avg(QUANTITYSOLD) as AvgQtySold
from IM_ItemWhseHistoryByPeriod
where FISCALCALYEAR = '2017
and FISCALCALPERIOD between 2 and 6
I have one SQL output table like this
ITEM,LOC,PERIOD,QUANTITY
101,US,07/22/2015,500
101,US,07/02/2015,0
102,LON,07/22/2015,0
102,LON,07/02/2015,1000
But I want the output table as follows,
ITEM LOC 07/22/2015 07/02/2015
101 US 500 0
102 LON 0 1000
Please find the code which I have used below,
select * from
(
select item, loc, period, quantity
from example
)
pivot
(
sum (quantity) for period in ('22/JUL/2015','02/JUL/2015'));
If it is for 2 dates, then no issue in mentionning the 'IN' clause
If it is 1000 dates like weekly, monthly and daily. Then how ?
Below command is not working in 'IN' clause.
SELECT PERIOD FROM EXAMPLE WHERE PERIOD < TO_DATE(22/JUL/2015);
Can you please help me to solve this issue ?
Thanks for your time.
Your issue may be incompatible data types. If the period column on your table is DATE type, you are trying to compare strings/VARCHAR with DATE type.
If period column is a DATE try changing your IN to
SELECT period FROM example WHERE period < DATE '2015-07-22';
or
SELECT period FROM example WHERE period < TO_DATE('22/JUL/2015', 'DD/MON/YYYY');
I have a table AreaValues with columns ID, Name, Value, startDate, EndDate.
I need to view a report that select the values of every month with month name or number
Example: this is my table AreaValues:
ID Name Value StartDate EndDate
-------------------------------------------
1 Test 200 05-06-2012 07-08-2016
I need report get the value using SQL Server Reporting Services or query or any way:
month value = (200 / count of months from startdate to end date)
ID Name Value year2012 year2013 year2014 year2015 year2016
1 test 200 6,7,8,9,10,11,12 1,2,3,....12 1,2,3,....12 1,2,3,....12 1,2,3,4,5,6,7
and do that for every record in the table
Please, I need help
It Solved
I used a Loop in SQl that get all months between two dates
Then
Create report with Matrix get the months above and ID left
Goal:
Combine two queries I currently run.
Have the WEEK from query 1 be a filtering criteria for query 2.
Query 1:
----------------------------------------------------
-- ************************************************
-- Accounts Recieveable (WEEKLY) snapshot
-- ************************************************
----------------------------------------------------
SELECT
TRUNC(TX.ORIG_POST_DATE,'WW') AS WEEK,
SUM(TX.AMOUNT) AS OUTSTANDING
FROM
TX
WHERE
--Transaction types
(TX.DETAIL_TYPE = "Charges" OR
TX.DETAIL_TYPE = "Payments" OR
TX.DETAIL_TYPE = "Adjustments")
GROUP BY
TRUNC(tx.ORIG_POST_DATE,'WW')
ORDER BY
TRUNC(tx.ORIG_POST_DATE,'WW')
Output Query 1:
WEEK OUTSTANDING
1/1/2012 18203.95
1/8/2012 17605
1/15/2012 19402.33
1/22/2012 18693.45
1/29/2012 19100
Query 2:
----------------------------------------------------
-- ************************************************
-- Weekly Charge AVG over previous 13 weeks based on WEEK above
-- ************************************************
----------------------------------------------------
SELECT
sum(tx.AMOUNT)/91
FROM
TX
WHERE
--Post date
TX.ORIG_POST_DATE <= WEEK AND
TX.ORIG_POST_DATE >= WEEK-91 AND
--Charges
(TX.DETAIL_TYPE = "Charge")
Output Query 2:
thirteen_Week_Avg
1890.15626
Desired Output
WEEK OUTSTANDING Thirteen_Week_Avg
1/1/2012 18203.95 1890.15626
1/8/2012 17605 1900.15626
1/15/2012 19402.33 1888.65132
1/22/2012 18693.45 1905.654
1/29/2012 19100 1900.564
Note the Thirteen_Week_Avg is 13 weeks prior to the "WEEK" Field. So it changes each week as the window of the average moves forward.
Also what tutorials do you guys know of that I could read to better understand the solution this type of question?
Try using an analytic function such as:
select WEEK, sum(OUTSTANDING) as OUTSTANDING, THIRTEEN_WEEK_AVG
from (select trunc(TX.ORIG_POST_DATE, 'WW') as WEEK
,AMOUNT as OUTSTANDING
,avg(
TX.AMOUNT)
over (order by trunc(TX.ORIG_POST_DATE, 'WW')
range numtodsinterval(7 * 13, 'day') preceding)
as THIRTEEN_WEEK_AVG
from TX
where (TX.DETAIL_TYPE = 'Charges'
or TX.DETAIL_TYPE = 'Payments'
or TX.DETAIL_TYPE = 'Adjustments'))
group by WEEK, THIRTEEN_WEEK_AVG
order by WEEK
An introduction to analytic functions can be found here. And how NUMTODSINTERVAL works is here.
My first thought is this is best handled by a stored procedure that sets two cursors, one for each of the queries and each cursor takes in a week parameter. You could call the first cursor that outputs the week and outstanding and have this loop through however many times and move back 1 week each time through. then pass that week to the thirteen week avg cursor and let it output the avg amount.
If you just want it on the screen you can use dbms_output.put_line. If you want to write it to a file such as a csv then you need to set a filehandler and all the associated plumbing to create/open/write/save a file.
O'reilly has a pretty good pl/sql book that explains procs and cursors well.