Closed . This question needs details or clarity . It is not currently accepting answers.
Something like this :
SELECT
SUBCATEGORY,
count( distinct case when EXTRACT(month FROM date_created) = 1 then ticket_no else null end) as JAN,
count( distinct case when EXTRACT(month FROM date_created) = 2 then ticket_no else null end) as FEB,
count( distinct case when EXTRACT(month FROM date_created) = 3 then ticket_no else null end) as MARCH,
count( distinct case when EXTRACT(month FROM date_created) = 4 then ticket_no else null end) as APRIL,
count( distinct case when EXTRACT(month FROM date_created) = 5 then ticket_no else null end) as MAY,
count( distinct case when EXTRACT(month FROM date_created) = 6 then ticket_no else null end) as JNE,
count( distinct case when EXTRACT(month FROM date_created) = 7 then ticket_no else null end) as JUL,
count( distinct case when EXTRACT(month FROM date_created) = 8 then ticket_no else null end) as AUG,
count( distinct case when EXTRACT(month FROM date_created) = 9 then ticket_no else null end) as SEPT,
count( distinct case when EXTRACT(month FROM date_created) = 10 then ticket_no else null end) as OCT,
count( distinct case when EXTRACT(month FROM date_created) = 11 then ticket_no else null end) as NOV,
count( distinct case when EXTRACT(month FROM date_created) = 12 then ticket_no else null end) as DEC
FROM
CNR_TICKET
WHERE
date_created >= to_date('1/01/2018','MM/DD/YYYY') and
date_created <= to_date('12/31/2018','MM/DD/YYYY')
GROUP BY
SUBCATEGORY
you can change your WHERE
clause using :
EXTRACT(year FROM date_created ) = 2018
Oracle / PLSQL: COUNT Function, How do I count the number of rows in PL SQL? COUNT returns the number of rows returned by the query. You can use it as an aggregate or analytic function. If you specify DISTINCT, then you can specify only the query_partition_clause of the analytic_clause. The order_by_clause and windowing_clause are not allowed. If you specify expr, then COUNT returns the number of rows where expr is not null.
You may try PIVOT
statement
select * from (
select SUBCATEGORY, month(date_created) mon
from CNR_TICKET
where date_created >= to_date('1/01/2018','MM/DD/YYYY') and date_created <= to_date('12/31/2018','MM/DD/YYYY')
)
pivot (
count(*)
for mon
in ( 1 Jan, 2 Feb, 3 MARCH, 4 APRIL, 5 MAY, 6 JNE, 7 JUL, 8 AUG, 9 SEPT, 10 OCT, 11 NOV, 12 DEC )
)
How to count no of records in table without count? - Ask Tom, How do I count the number of rows in SQL without counting? Count by month in a date columns Oracle Database Tips by Donald Burleson October 31, 2015 Question: I need a SQL statement to count the number of rows in a table that fall on the same month.
you can use Pivot
keyword by using for month
for the pivoting query as
select *
from
(
select subcategory, to_char(date_created,'mm') as month
from cnr_ticket
where to_char(date_created,'yyyy')='2018'
)
pivot(
count(*)
for (month)
in ('01' as jan ,'02' as feb, '03' as mar,
'04' as apr ,'05' as may, '06' as jun,
'07' as jul ,'08' as aug, '09' as sep,
'10' as oct ,'11' as nov, '12' as dec
)
)
or using conditional aggregation
select subcategory,
sum(case when to_char(date_created,'mm') = '01' then 1 else 0 end) as jan,
sum(case when to_char(date_created,'mm') = '02' then 1 else 0 end) as feb,
sum(case when to_char(date_created,'mm') = '03' then 1 else 0 end) as mar,
sum(case when to_char(date_created,'mm') = '04' then 1 else 0 end) as apr,
sum(case when to_char(date_created,'mm') = '05' then 1 else 0 end) as may,
sum(case when to_char(date_created,'mm') = '06' then 1 else 0 end) as jun,
sum(case when to_char(date_created,'mm') = '07' then 1 else 0 end) as jul,
sum(case when to_char(date_created,'mm') = '08' then 1 else 0 end) as aug,
sum(case when to_char(date_created,'mm') = '09' then 1 else 0 end) as sep,
sum(case when to_char(date_created,'mm') = '10' then 1 else 0 end) as oct,
sum(case when to_char(date_created,'mm') = '11' then 1 else 0 end) as nov,
sum(case when to_char(date_created,'mm') = '12' then 1 else 0 end) as dec
from cnr_ticket
where to_char(date_created,'yyyy')='2018'
group by subcategory
Rextester Demo
COUNT, How do you count the number of records in a table without counting in Oracle? Pivot tables have a built-in feature to group dates by year, month, and quarter. In the example shown, a pivot table is used to count colors per month for data that covers a 6-month period. The count displayed represents the number of records per month for each color. Fields. The source data contains three fields: Date, Sales, and Color.
[ORACLE] SQL to count total number of records per month for the , COUNT returns the number of rows returned by the query. You can use it as an aggregate or analytic function. If you specify DISTINCT , then you can specify COUNT(DISTINCT expr,[expr]) Example : To get unique number of rows from the 'orders' table with following conditions - 1. only unique cust_code will be counted, 2. result will appear with the heading "Number of employees", the following SQL statement can be used : SELECT COUNT ( DISTINCT cust_code ) AS "Number of employees" FROM orders;
Learn Oracle COUNT() Function By Practical Examples, [ORACLE] SQL to count total number of records per month for the past few years. select substr(TRANSACTION_DATE,1,1)||'/'||substr(TRANSACTION_DATE,-1,4), count(substr(TRANSACTION_DATE,1,1)||'/'||substr(TRANSACTION_DATE,-1,4)) from ORDERS. MONTH function The MONTH function returns the month part of a value. The argument must be a date, timestamp, or a valid character string representation of a date or timestamp that is not a CLOB, LONG VARCHAR, or XML value.
Query help (COUNT records for each month over last year), The following statement uses the COUNT(ALL val) function to return the number of non-null rows in the items table, considering duplicates. SELECT COUNT( ALL Starting in column F, we have a summary table that shows a total count per month, followed by a total count per month per priority. We are using the COUNTIFS function to generate a count. The first column of the summary table (F) is a date for the first of each month in 2015.
Comments Please post your table structure, some sample data and expected output for that data Thanks! The count is adding 1-4 but it's already close to the actual results. Thanks! but this doesn't work for me, I think month function cannot be used in oracle Thanks! this is near to the actual data that I need, the count is adding 1-4 rows, it's close so thanks! @PatrickBrianGarcia you'r welcome, take it easy.