So i have a table that looks like this:
As you can see some of the dates are the same however the time of day is different.
i have the following select statement (i have deleted and altered some data since this is rather sensitive):
SELECT LAST_UPD AS PERIOD,
COUNT(CASE WHEN SOLVED_SECONDS /60 /60 <= 2 THEN 1 END) as completed_within_2hours,
COUNT(CASE WHEN STATUS ='Færdig' THEN +1 END) as completed_callbacks,
COUNT(CASE WHEN SOLVED_SECONDS /60 /60 <= 2 THEN 1 END) / COUNT(CASE WHEN STATUS ='Færdig' THEN 1 END) * 100 as Percentage
FROM TABLE
WHERE LAST_UPD BETWEEN '2013-07-01' AND '2013-07-03'
AND STATUS ='Færdig'
AND AGENTGROUP IN ('Hovednumre','Forsikring','Hotline','Kunder')
GROUP BY LAST_UPD
Now i want my query to count when the DATES are the same so forexample if the date is 2013-07-01 i want it to count x amount for that day regardless of time of day.
I have tried the following:
SELECT DISTINCT TO_CHAR(LAST_UPD, 'YYYY/MM/DD') AS PERIOD,
Sadly this returns each of days but doesnt count them correctly (it returns 1 for each day).
Perhaps what you want is to select and group by "Trunc(last_upd)"
Related
This is the table and all its fields and I am trying to get the number of executed, scheduled, and cancelled bookings per month per year in SQLite. Can anyone help, please? Thanks
Table fields: {id (INT), starttime(DATESTAMP), duration_hours(INT), user_id(INT), status(TEXT)}
id (booking id) is unique, starttime ranges between 2020 and 2022 (format: yyyy-mm-dd), user_id is not unique, status are either Executed, Scheduled or Cancelled.
You may use the strftime function to extract the month and year from the date value, then use conditional count aggregated by the value of strftime.
Select strftime('%Y-%m', starttime) AS 'Month_Of_Year',
COUNT(Case status When 'Executed' Then 1 Else Null End) AS 'Executed',
COUNT(Case status When 'Scheduled' Then 1 Else Null End) AS 'Scheduled',
COUNT(Case status When 'Cancelled' Then 1 Else Null End) AS 'Cancelled'
From Bookings
Group By strftime('%Y-%m', starttime)
Order By strftime('%Y-%m', starttime)
See a demo from db<>fiddle.
Currently, I am executing four queries to project the results based last 24hours, last week, last month and last month. Is it possible to project a four results in a single query?
If you need last one week, last one month, last one year compared to current date then you can do it using conditional aggregation in oracle as following:
Select sum(case when trunc(date_col) = trunc(sysdate-1) then 1 end) as last_one_day,
sum(case when trunc(date_col) between trunc(sysdate-7) and trunc(sysdate-1) then 1 end) as last_one_week,
sum(case when trunc(date_col) between trunc(add_months(sysdate,-1)) and trunc(sysdate-1) then 1 end) as last_one_month,
sum(case when trunc(date_col) between trunc(add_months(sysdate,-12)) and trunc(sysdate-1) then 1 end) as last_one_year
From your_table
Where type = ...
Cheers!!
I currently have a query that reads from a table which is written to daily, so I have totals for every day.
What I'm trying to do is modify this query so that I can use days of the week as an aggregate, if that makes sense.
THe totals I get right now are correct, but I want to use Sunday through Saturday as a week and effectively say ``'If today is wednesday, sum totals for weeklyData for Sunday, MOnday and Tuesday from tableOne```
I have this query:
SELECT employee,
sum(case when category = 'Brown' then daily_total else 0 end) as DailyBrownNumbers,
sum(case when category = 'Brown' then weekly_quota else 0 end) as WeeklyBrownNumbers,
CURRENT_DATE as DATE_OF_REPORT
from dailyRecords
where date_of_report >= current_date
group by employee
which reads from that table for the daily records and that's what I need still, but I want to add a sum for daily_total based on those days of the week if possible.
Can this be done with DB2?
You can use dayofweek function. SQL gets all records starting from Sunday including current date. Second column "DailyBrownNumbers" uses case statement to restrict totals to current date records. Third column "WeeklyTotal" has totals for all records from Sunday.
SELECT employee,
sum(case when category = 'Brown' and date_of_report >= current date
then daily_total
else 0 end) as DailyBrownNumbers,
sum(case when category = 'Brown'
then daily_total
else 0 end) as WeeklyTotal,
sum(case when category = 'Brown'
then weekly_quota
else 0 end) as WeeklyBrownNumbers,
CURRENT_DATE as DATE_OF_REPORT
from dailyRecords
where date_of_report >= ( current date - ( dayofweek(current date) - 1 ) days )
group by employee
I'm trying to display aggregate counts of open and closed IT tickets by date and category.
Parent table consists of the following columns:
Alert_ID Alert_Open_Date Alert_Closed_Date
I'd like my end result to resemble the following, where I have
A. A date within any specified date range,
B. total number of alerts that still showed open as of that date (Outstanding_Alerts),
C. total number of alerts that were opened on that date (New_Alerts),
D. total number of the new alerts that were closed on that date (Closed_New_Alerts), and
E. combined number of alerts, both new and old, that were closed on that date (Closed_Total):
Date Outstanding_Alerts New_Alerts Closed_New_Alerts Closed_Total
6/1/2018 20 10 5 7
6/2/2018 23 20 8 10
6/3/2018 33 13 10 15
etc. # # # #
I was thinking of something like the following conceptual query to accomplish this, but I'm stumbling over the logic to get the results I'd like. Regardless of wording I can't seem to get the buckets correct. Some columns remain blank when they should be populated, for example. Any help is appreciated.
SELECT DISTINCT
alert_date
, SUM(OOA) AS Outstanding_Open_Alerts
, SUM(NOA) AS New_Open_Alerts
, SUM(CNA*NOA) AS Closed_New_Alerts
, SUM(CT) AS Total_Closed_Alerts
, SUM(CNA+NOA-CT) AS Remaining_Alerts --optional column
FROM
(SELECT
TRUNC(open_date) AS Alert_Date
, CASE WHEN alert_date < trunc(SYSDATE)-1 AND closed_date IS NULL THEN 1 ELSE 0
END AS OOA --old open alerts
, CASE WHEN alert_date > trunc(SYSDATE)-1 THEN 1 ELSE 0
END AS NOA --new open alerts
, CASE WHEN closed_date >= trunc(SYSDATE)-1 THEN 1 ELSE 0
END AS CNA --closed new alerts
, CASE WHEN closed_date < trunc(SYSDATE)-1 THEN 1 ELSE 0
END AS CT --closed total
FROM sys_alerts)
GROUP BY alert_date;
If I'm following your description, to get the numbers for an arbitrary date supplied as some_date you want something like:
SELECT
some_date AS Alert_Date
, COUNT(CASE WHEN open_date < some_date
AND (closed_date IS NULL OR closed_date > some_date
THEN alert_id END) AS Current_Open_Alerts
, COUNT(CASE WHEN open_date >= some_date
AND open_date < some_date + 1
THEN alert_id END AS New_Alerts
, COUNT(CASE WHEN open_date >= some_date
AND open_date < some_date + 1
AND closed_date < some_date + 1
THEN alert_id END AS Closed_New_Alerts
, COUNT(CASE WHEN closed_date >= some_date
AND closed_date < some_date + 1
THEN alert_id END AS Closed_Total
, COUNT(CASE WHEN open_date < some_date + 1
AND (closed_date IS NULL OR closed_date >= some_date + 1
THEN alert_id END) AS Alerts_Remaining
FROM sys_alerts
GROUP BY some_date
I've used a count rather than a sum, which relies on the fact that count ignores nulls - and left the default from the case expresssions as null. It doesn't need a sunquery really so I've removed that.
It seems like you want all data for a range of dates, so you can generate that in an inline view or CTE and then join to your real table, so some_date becomes wach individual date from that generated range. E.g. to get the last 30 days something like:
FROM (
SELECT sysdate - level AS some_date
FROM dual
CONNECT BY level <= 30
LEFT JOIN sys_alerts
ON closed_date IS NULL OR closed_date >= some_date
GROUP BY some_date
ORDER BY some_date
or you could do something with cross apply or a partitioned outer join. Hopefully this gives you a starting point though.
I have a date field names "dts" in string format. I want to find out all the records based on their time differences (in hours). The run event time should be greater than or equal to eat event.
The output should be:
Convert timestamps to seconds, then subtract, divide result by 3600 to get hours, use case+count to count by ranges, something like this:
select count(case when diff_hrs >24 then 1 end) as more_24,
count(case when diff_hrs <=24 then 1 end) as less_than_24,
...
count(case when diff_hrs >=2 and diff_hrs <=3 then 1 end) as hrs_2_to_3,
...
from
(
select
abs(unix_timestamp(dts) - unix_timestamp(dts-eat)))/60/60 as diff_hrs
from table
)s;