oracle: Count the classes start each month - sql

I am trying to count of classes to start each month.
select
to_char(START_DATE_TIME,'MON'),
count(START_DATE_TIME)
from
SECTION
having
count(START_DATE_TIME) > 1
group by
START_DATE_TIME
It give me this output
MAY 4
APR 3
MAY 2
JUN 2
APR 2
JUL 7
JUL 7
JUN 3
APR 4
MAY 2
APR 6
MAY 4
JUN 2
JUN 2
JUN 3
MAY 5
JUN 2
APR 3
MAY 3
JUN 3
MAY 2
APR 2
MAY 3
I need a output similar to this
Start_Month Count
July 14
June 17
April 21
May 26

Use "to_char(START_DATE_TIME,'MON')" in all of your count, group by, having and order by.
select
to_char(START_DATE_TIME,'MON') as Start_Month ,
count(to_char(START_DATE_TIME,'MON')) as Count
from
SECTION
having
count(to_char(START_DATE_TIME,'MON')) > 1
group by
to_char(START_DATE_TIME,'MON')
order by
count(to_char(START_DATE_TIME,'MON'));

With group by START_DATE_TIME you tell the DBMS you want one result row per START_DATE_TIME. But what you actually want is one result row per month, so group by month instead.
count(START_DATE_TIME) counts the rows for which START_DATE_TIME is not null. As you group by this date, this makes no sense. Count the rows unconditionally instead (COUNT(*)).
having count(START_DATE_TIME) > 1 occurs after GROUP BY of course and should hence be placed behind it. It looks strange to see it in the wrong place. Moreover: What are you trying to achieve with this condition? You get one result row per START_DATE_TIME, because there is at least one record for the date in the table. So of course this connition is true for all dates. (Except for null, if START_DATE_TIME is nullable. But then you'd merely apply WHERE START_DATE_TIME IS NOT NULL.)
The query corrected:
select
to_char(start_date_time, 'Month') as "Start_Month",
count(*) as "Count"
from section
group by to_char(start_date_time, 'Month')
order by "Count";
BTW: I guess you are aware that you are looking at months regardless of the year. If you want to change this, change the TO_CHAR format accordingly.

Related

Count total without duplicate records

I have a table that contains the following columns: TrackingStatus, Year, Month, Order, Notes
I need to calculate the total number of tracking status for each year and month.
For example, if the table contains the following orders:
TrackingStatus
Year
Month
Order
Notes
F
2020
1
33
F
2020
1
33
DFF
E
2020
2
36
xxx
A
2021
3
34
X1
A
2021
3
34
DD
A
2021
3
88
A
2021
2
45
The result should be:
• Tracking F , year 2020, month 1 the total will be one (because it's the same year, month, and order).
• Tracking A , year 2021, month 2 the total will be one. (because there is only one record with the same year, month, and order).
• Tracking A , year 2021, month 3 the total will be two. (because there are two orders within the same year and month).
So the expected SELECT output will be like that:
TrackingStatus
Year
Month
Total
F
2020
1
1
E
2020
2
1
A
2021
2
1
A
2021
3
2
I was trying to use group by but then it will count the number of records which in my scenario is wrong.
How can I get the total orders for each month without counting “duplicate” records?
Thank you
You can use a COUNT DISTINCT aggregation function, whereas the COUNT allows you to count the values, but the DISTINCT condition will allow each value only once.
SELECT TrackingStatus,
Year,
Month,
COUNT(DISTINCT Order) AS Total
FROM tab
GROUP BY TrackingStatus,
Year,
Month
ORDER BY Year,
Month
Here you can find a tested solution in a MySQL environment, though this should work with many DBMS.

Can I calculate an aggregate duration over multiple rows with a single row per day?

I'm creating an Absence Report for HR. The Absence Data is stored in the database as a single row per day (the columns are EmployeeId, Absence Date, Duration). So if I'm off work from Tuesday 11 February 2020 to Friday 21 February 2020 inclusive, there will be 9 rows in the table:
11 February 2020 - 1 day
12 February 2020 - 1 day
13 February 2020 - 1 day
14 February 2020 - 1 day
17 February 2020 - 1 day
18 February 2020 - 1 day
19 February 2020 - 1 day
20 February 2020 - 1 day
21 February 2020 - 1 day
(see screenshot below)
HR would like to see a single entry in the report for a contiguous period of absence:
My question is - without using a cursor, how can I calculate the is in SQL (even more complicated because I have to do this using Linq to SQL, but I might be able to swap this out for a stored procedure. Note that the criterion for contiguous data is adjacent working days EXCLUDING weekends and bank holidays. I hope I've made myself clear ... apologies if not.
This is a form of gaps-and-islands. In this case, use lag() to see if two vacations overlap and then a cumulative sum:
select employee, min(absent_from), max(absent_to)
from (select t.*,
sum(case when prev_absent_to = dateadd(day, -1, absent_from) then 0 else 1
end) over (partition by employee order by absent_to) as grp
from (select t.*,
lag(absent_to) over (partition by employee order by absent_from) as prev_absent_to
from t
) t
) t
group by employee, grp;
If you need to deal with holidays and weekends, then you need a calendar table.

SQL Carryover from previous month

I have some data that I am trying to get some counts on. There are dates for when the record was entered and when it was closed, if it has been closed yet. I want to be able to get a count of how many records were still open from the previous month as of the first of the month. Here is an example. First table is the data, second table is the results I am looking for. In the second table, ignore the parenthesis, they are just the IDs of the records that make up that count.
Position DateEntered DateClosed
1 12/15/2017 12/20/2017
11 12/20/2017 1/7/2018
2 1/23/2018 2/3/2018
3 1/24/2018
4 2/15/2018
5 2/20/2018 5/16/2018
6 3/3/2018 3/15/2018
7 3/23/2018 4/12/2018
8 4/11/2018 5/10/2018
9 4/12/2018 4/25/2018
10 5/4/2018
Year Month Carried Over
2018 January 1 (11)
2018 February 2 (2,3)
2018 March 3 (3,4,5)
2018 April 4 (3,4,5,7)
2018 May 4 (3,4,5,8)
2018 June 3 (3,4,10)
2018 July 3 (3,4,10)
2018 August 3 (3,4,10)
Is this possible, and if so, how? Been racking my brain on this one for a few hours.
For each month, you want the number of rows that start before that month and end after. I'm thinking:
with dates as (
select cast('2018-01-01' as date) as dte
union all
select dateadd(month, 1, dte)
from dates
where dte < '2018-08-01'
)
select d.dte,
(select count(*)
from t
where t.dateentered < d.dte and
(t.dateclosed > d.dte or t.dateClosed is null)
) as carriedover
from dates d;
Note that this puts the date in a single column, rather than splitting the year and month into separate columns. That is easily arranged, but I prefer to keep date components together.

Using sum function with a condition based on a returned value

I have a set of given month with a number of hours related to each of it
DATE HOURS
8/1/2013 3
9/1/2013 8
10/1/2013 2
11/1/2013 4
12/1/2013 1
I need to return the sum of hours for everything that is in the past including current month, in the example below, starting in august, sum would be august only. For september, I'd need august + september
DATE HOURS SUM
8/1/2013 3 3
9/1/2013 8 11
10/1/2013 2 13
11/1/2013 4 17
12/1/2013 1 18
I am not sure how to proceed, since the date condition is different for each line.
If anyone can help on this, it'd be greatly appreciated
You can do this in most SQL dialects using a correlated subquery (or a non-equijoin, but I find the subquery cleaner):
select date, hours,
(select sum(t2.hours)
from t t2
where t2.date <= t.date
) as cum
from t;
Many SQL engines also support the cumulative sum function, which would typically look like this:
select date, hours sum(hours) over (order by date) as cum
from t

How to inserting an intermediate row?

I have the following table:
Year Line January Febraury March .... December
2011 B1 5 10 20
2012 B1 10 15 25 ...
2011 A1 4 8 10 ...
And I want to insert a subtotal row each two lines (if exists), in particular each time year and Line changing: so
Year Line January Febraury March .... December
2011 B1 5 10 20
2012 B1 10 15 25 ...
--- B1 +100% +50% +25% ..
2011 A1 4 8 10 ...
How can I do this in T-SQL ?
Maybe using cursor ?
Are you certain that you want to insert a new row? Or just be able to calculate that subtotal when you query the data?
Query Version
SELECT
Year,
Line,
SUM(January) AS January,
SUM(February) AS February,
...
SUM(December) AS December
FROM
yourTable
GROUP BY
Year,
Line
WITH
ROLLUP
ORDER BY
Year,
Line
Insert Version
If you just one one level of summary, remove the WITH ROLLUP
INSERT INTO
yourTable
SELECT
Year,
NULL,
SUM(January) AS January,
SUM(February) AS February,
...
SUM(December) AS December
FROM
yourTable
GROUP BY
Year
WITH
ROLLUP
EDIT Follow question edit
I strongly suggest that you mean a query, not a change to the actual data. I also suggest that you either build these lines in your reporting environment, or you put the % values to the right of each record...
SELECT
this_year.Year,
this_year.Line,
this_year.January,
CAST(this_year.January AS DECIMAL(8,2)) / CAST(last_year.January AS DECIMAL(8,2)) AS January_Change,
...
FROM
yourTable AS this_year
LEFT JOIN
yourTable AS last_year
ON last_year.year = this_year.year-1
AND last_year.line = this_year.line