Birt Report and Date Parameter between current year and previous year - oracle

I am using the following query that was suggested to me by a fellow from stackoverflow. The following query is using the dates in order to bring the current year (>2016,<2017) and the previous year (>2015, <2016). I want to use this in a Birt report - and particularly prompt the user to select the date (by default the current date will be selected) and automatically the query will subtract a year in order to calculate the current year's results and the previous year's results. Here's the code given to me
SELECT grade,
COUNT( DISTINCT CASE WHEN DATE '2015-01-01' >= date_column
AND date_column < DATE '2016-01-01'
THEN customer_id END
) AS number_of_unique_customers_in_2015,
COUNT( DISTINCT CASE WHEN DATE '2016-01-01' >= date_column
AND date_column < DATE '2017-01-01'
THEN customer_id END
) AS number_of_unique_customers_in_2016
FROM Customers
WHERE Date_Column >= DATE '2015-01-01'
AND Date_Column < DATE '2017-01-01'
GROUP BY grade;
Can anyone suggest how to do this in the birt report?

Have a look at my answer here to see how to add parameters to a SQL query in Birt. You should add your Report Parameter to input the date from the user first.
To substract a year from the input parameter you should use DATEADD with a negative value for one year inside your SQL query where you add the parameter.
your query should look like this for the Input-Parameter date 2016-01-01:
SELECT grade,
COUNT( DISTINCT CASE WHEN DATEADD(year, -1, ?) >= date_column
AND date_column < ?
THEN customer_id END
) AS number_of_unique_customers_in_last_year,
COUNT( DISTINCT CASE WHEN ? >= date_column
AND date_column < DATEADD(year, 1, ?)
THEN customer_id END
) AS number_of_unique_customers_in_current_year
FROM Customers
WHERE Date_Column >= DATEADD(year, -1, ?)
AND Date_Column < DATEADD(year, 1, ?)
GROUP BY grade;

Related

Applying case statement on date

I am working on reports where we have activity start date(Date when the activity has begun) and activity end date(When the activity ended). I have a requirement wherein if the activity has begun before 2021 then I have to set the start date as 1/1/21 and if it continues after 2021 then set the activity end date as 31/12/21.And if the start date and end date lie in the same year keeping them as it is.
How can I achieve this scenario.
You do not need a CASE expression; you can use GREATEST and LEAST:
UPDATE table_name
SET start_date = GREATEST(start_date, DATE '2021-01-01'),
end_date = LEAST(end_date, TIMESTAMP '2021-12-31 23:59:59')
WHERE start_date < DATE '2022-01-01'
AND ( start_date < DATE '2021-01-01'
OR end_date >= DATE '2022-01-01');
If you want to just select the values where the date range overlaps 2021, and limit the range to be from 2021, then:
SELECT column1,
column2,
GREATEST(start_date, DATE '2021-01-01') AS start_date,
LEAST(end_date, TIMESTAMP '2021-12-31 23:59:59') AS end_date
FROM table_name
WHERE start_date < DATE '2022-01-01'
AND end_date >= DATE '2021-01-01'
If you want a generic query for any given year, starting from the :year_start bind variable, then:
SELECT column1,
column2,
GREATEST(start_date, :year_start) AS start_date,
LEAST(end_date, ADD_MONTHS(:year_start, 12) - INTERVAL '1' SECOND) AS end_date
FROM table_name
WHERE start_date < ADD_MONTHS(:year_start, 12)
AND end_date >= :year_start;

how to get previous month or last two month data in oracle

how to get previous month or last two month data in oracle.
My date format is YYYY,MM,DD.
from google search i got those solution,
select * from IM_LAPTOP
where ADD_DATE >= add_months(sysdate, -12);
select *
from IM_LAPTOP
where ADD_DATE between add_months(trunc(sysdate,'mm'),-1) and last_day(add_months(trunc(sysdate,'mm'),-1));
But its showing not a valid month
My date format is YYYY,MM,DD
Unless you are using a string to store dates then, no, it is not; a DATE is a binary data type (consisting of 1-byte for each of: century, year-of-century, month, day, hour, minute and second and it always has those components) and it has no format.
how to get previous month or last two month data in oracle
To get the data from 2 months before this instant in time (i.e. if it is now 2021-04-05 16:39:24 and you want it from 2021-02-05 16:39:24, two months prior) then:
SELECT *
FROM your_table
WHERE date_column >= ADD_MONTHS( SYSDATE, -2 )
To get the data starting from midnight on the 1st day of last month:
SELECT *
FROM your_table
WHERE date_column >= ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), -1 )
If you only want the data from the preceding month then:
SELECT *
FROM your_table
WHERE date_column >= ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), -1 )
AND date_column < TRUNC( SYSDATE, 'MM' )
If your "date" is actually a VARCHAR2 column with the format YYYY,MM,DD then you should change it to a DATE column but if for some reason you cannot then at least the characters are in order of highest-to-least significance and you can perform an alphanumeric comparison and just wrap the right-hand side of the filters in TO_CHAR:
SELECT *
FROM your_table
WHERE date_column >= TO_CHAR( ADD_MONTHS( SYSDATE, -2 ), 'YYYY,MM,DD' )

Datepart function in oracle

I have some sample records in Oracle 12
Date_Time Item
10/1/2012 12:05:00 AM 3
12/3/2012 06:00:00 AM 2
11/8/2012 14:05:05 PM 10
12/9/2012 16:00:59 PM 5
I like to aggregate the Item field based on military time or in three different times: 00:00:00AM to 05:59:00AM, 06:00:00AM to 15:59:00PM, and 16:00:00PM to 23:59:00PM. I was able to use the Datepart function in SQL to do this. I was wondering what function in Oracle 12 that allows me to count the Item between these three different times.
My desired output would be:
Date_Time Count
00:00:00AM to 05:59:00AM = 3
06:00:00AM to 15:59:00PM = 12
16:00:00PM to 23:59:00PM = 5
In oracle, date datatype contains date+time ,so you just need just use group by
SELECT Date_Time, COUNT(*) item FROM YOUR_TABLES
GROUP BY Date_Time;
NEW Answer:
SELECT TO_CHAR(DATE_TIME,'HH24') time, count(*) FROM YOUR_TABLE
WHERE TO_CHAR(DATE_TIME,'HH24') >= '00'
AND TO_CHAR(DATE_TIME,'HH24') < '06'
GROUP BY TO_CHAR(DATE_TIME,'HH24')
UNION ALL
SELECT TO_CHAR(DATE_TIME,'HH24') time, count(*) FROM YOUR_TABLE
WHERE TO_CHAR(DATE_TIME,'HH24') >= '06'
AND TO_CHAR(DATE_TIME,'HH24') < '16'
GROUP BY TO_CHAR(DATE_TIME,'HH24')
UNION ALL
SELECT TO_CHAR(DATE_TIME,'HH24') time, count(*) FROM YOUR_TABLE
WHERE TO_CHAR(DATE_TIME,'HH24') >= '16'
AND TO_CHAR(DATE_TIME,'HH24') < '00'
GROUP BY TO_CHAR(DATE_TIME,'HH24');
and if your table is huge :
first :partition it
second: create local functional index on TO_CHAR(DATE_TIME,'HH24:MI:SS')
Assuming that date_time column is datatype DATE, we can use the TO_CHAR function to extract a two character representation... in the range 00 to 23.
(The selected answer demonstrates this approach to extracting the "hour" from an Oracle DATE.)
Assuming that we want every non-null time value to fall into one of three time ranges... that is, if we don't want any of time values to be omitted because of a crack/gap in between the ranges, and we don't want any overlap in the ranges...
We can use a simple "less than" tests in a CASE expression.
Consider a time close to a boundary: '05:59:33'. That's after 05:59:00 but before 06:00:00. If we want that included in the first range, we can just test for hour < '06'.
If was grouping the rows into three ranges, and I wanted a total of the item column, I'd do something like this:
SELECT CASE
WHEN TO_CHAR( t.date_time ,'HH24') < '06' THEN '00:00:00 to 05:59:59'
WHEN TO_CHAR( t.date_time ,'HH24') < '16' THEN '06:00:00 to 15:59:59'
WHEN TO_CHAR( t.date_time ,'HH24') < '24' THEN '16:00:00 to 23:59:59'
END AS time_range
, SUM(t.item)
FROM mytable t
GROUP
BY CASE
WHEN TO_CHAR( t.date_time ,'HH24') < '06' THEN '00:00:00 to 05:59:59'
WHEN TO_CHAR( t.date_time ,'HH24') < '16' THEN '06:00:00 to 15:59:59'
WHEN TO_CHAR( t.date_time ,'HH24') < '24' THEN '16:00:00 to 23:59:59'
END
and add an ORDER BY clause if I want the results returned in a particular order.
If the table contains any NULL values of date_time, the query above will also return a fourth time_range with a NULL value.
Here is how you would get the desired result in three columns (rather than three rows), which makes more sense for most applications. You can change this easily to get the result in rows instead.
Note that if dt is any date in Oracle, dt - trunc(dt) is the number of days (a fraction with value less than 1) since midnight.
select sum(case when dt-trunc(dt) < 6/24 then item else 0 end) as morning,
sum(case when dt-trunc(dt) >= 6/24
and dt-trunc(dt) < 16/24 then item else 0 end) as daytime,
sum(case when dt-trunc(dt) >= 16/24 then item else 0 end) as evening
from your_table
;

Grouping by Fiscal Year (Oracle)

Is there a way in Oracle that can pull the FY? I used the script below to pull just two FY. Mytable date range is from FY1998 to FY2009.
SELECT 'FY2008' as FY,
Site,
COUNT(*)
FROM mytable
WHERE date >='10-OCT-2007'
AND date <'10-OCT-2008'
GROUP BY site
SELECT 'FY2008' as FY,
Site,
COUNT(*)
FROM mytable
WHERE date >='10-OCT-2008'
AND date <'10-OCT-2009'
GROUP BY site
Pulling two FY is OK but it's too much repeatative when pulling more than 10 FY.
Add 83 days to your date and truncate it to whole year:
select 'FY'||TRUNC(date + 83, 'YYYY') as FY, Site, count(*)
from mytable
group by 'FY'||TRUNC(date + 83, 'YYYY'), site
Assuming Oracle 9i+, use a CASE expression:
SELECT CASE
WHEN TO_CHAR(t.date, ) = 10 AND EXTRACT(DAY FROM t.date) >= 10 THEN
'FY' || EXTRACT(YEAR FROM t.date) + 1
WHEN TO_CHAR(t.date, ) > 10 THEN
'FY' || EXTRACT(YEAR FROM t.date) + 1
ELSE
'FY' || EXTRACT(YEAR FROM t.date)
END AS FY,
t.site,
COUNT(*)
FROM YOUR_TABLE t
GROUP BY t.site, FY
And for completeness, in addition to #eumiro answer. In countries (such as Australia) which have a financial year running from 1 July to 30 June, you can replace the 83 with 184.
A few options:
You can use the to_char function here. Check this link for an explanation:
http://www.techonthenet.com/oracle/functions/to_char.php
You may also try using a case statement
select case when date >='10-OCT-2007' and date <'10-OCT-2008' then 'FY08'
when date >='10-OCT-2008' and date <'10-OCT-2009' then 'FY09'
else 'Other' end as fiscal_year, count(*)
from mytable
group by case when date >='10-OCT-2007' and date <'10-OCT-2008' then 'FY08'
when date >='10-OCT-2008' and date <'10-OCT-2009' then 'FY09'
else 'Other' end
Ultimately, if you have create table privileges you may want to consider making a date lookup table. Search for "date dimension" in data warehousing guides.
For example:
Your table would have
date, date_desc, fiscal_year, etc....
then you could just join and group by fiscal year, or whatever else you want.
Here is another way to easily determine the Fiscal Year of a date for those who's Fiscal Year runs from July to June:
SELECT 'FY'||TO_CHAR(ROUND(your_date_here,'YEAR'),'YY') AS FY

How to optimize an Oracle query that has to_char in where clause for date

I have a table that contains about 49403459 records.
I want to query the table on a date range. say 04/10/2010 to 04/10/2010. However, the dates are stored in the table as format 10-APR-10 10.15.06.000000 AM (time stamp).
As a result when I do
SELECT bunch,of,stuff,create_date
FROM myTable
WHERE TO_CHAR (create_date,'MM/DD/YYYY)' >= '04/10/2010'
AND TO_CHAR (create_date, 'MM/DD/YYYY' <= '04/10/2010'
I get 529 rows but in 255.59 seconds! Which is because I guess I am doing TO_CHAR on EACH record.
However, when I do
SELECT bunch,of,stuff,create_date
FROM myTable
WHERE create_date >= to_date('04/10/2010','MM/DD/YYYY')
AND create_date <= to_date('04/10/2010','MM/DD/YYYY')
then I get 0 results in 0.14 seconds.
How can I make this query fast and still get valid (529) results?
At this point I can not change indexes. Right now I think index is created on create_date column.
How can I convert the two date ranges so that first date range gets converted to time stamp with all 0's and the second one gets converted to time stamp that is the last time stamp of the date. If that makes sense...?
The following where clause fetches no results either:
WHERE
create_date >= to_timestamp('04/10/2010 00:00:00.000000','MM/DD/YYYY HH24:MI:SS.FF')
AND
create_date <= to_timestamp('04/10/2010 00:00:00.000000','MM/DD/YYYY HH24:MI:SS.FF')
I get 529 rows but in 255.59 seconds!
Which is because I guess I am doing
TO_CHAR on EACH record.
If you were to generate an execution plan for your first query ...
explain plan for
SELECT bunch,of,stuff,create_date
FROM myTable
WHERE TO_CHAR (create_date,'MM/DD/YYYY)' >= '04/10/2010'
AND TO_CHAR (create_date, 'MM/DD/YYYY') <= '04/10/2010'
/
... you would see that it does a full table scan. That's because the to_char() prevents the use of your index on CREATE DATE.
You don't say how long it took to return the results when you ran ...
SELECT bunch,of,stuff,create_date
FROM myTable
WHERE
create_date >= to_timestamp('04/10/2010 00:00:00.000000','MM/DD/YYYY HH24:MI:SS.FF')
AND
create_date <= to_timestamp('04/10/2010 23:59:59:123000','MM/DD/YYYY HH24:MI:SS.FF')
/
... but I expect it was way closer to 0.14 seconds than 4 minutes.
Of course this doesn't work:
WHERE
create_date >= to_timestamp('04/10/2010 00:00:00.000000','MM/DD/YYYY HH24:MI:SS.FF')
AND
create_date <= to_timestamp('04/10/2010 00:00:00.000000','MM/DD/YYYY HH24:MI:SS.FF')
Because that would only return rows where the create_date is 4/10/2010 12:00 AM exactly!
If you want to get all rows where create_date occurs any time on the day of 4/10/2010, use this:
WHERE
create_date >= to_timestamp('04/10/2010 00:00:00.000000','MM/DD/YYYY HH24:MI:SS.FF')
AND
create_date < to_timestamp('04/11/2010 00:00:00.000000','MM/DD/YYYY HH24:MI:SS.FF')
OR if you prefer:
WHERE create_date BETWEEN to_timestamp('04/10/2010 00:00:00.000000','MM/DD/YYYY HH24:MI:SS.FF')
AND to_timestamp('04/10/2010 23:59:59.999999','MM/DD/YYYY HH24:MI:SS.FF')
By the way, when you want to represent midnight, you can leave all the other parts out. So you could just say:
WHERE
create_date >= to_timestamp('04/10/2010','MM/DD/YYYY')
AND
create_date < to_timestamp('04/11/2010','MM/DD/YYYY')
In your first query, you are doing a character comparison rather than a date comparison, which should not be producing correct results.
For example, using your logic, 01/02/2009 will be greater than 01/01/2010 because the day component '02' is greater than the day component '01' when comparing characters and the year will never be evaluated.
This works:
WHERE
create_date >= to_timestamp('04/10/2010 00:00:00.000000','MM/DD/YYYY HH24:MI:SS.FF')
AND
create_date <= to_timestamp('04/10/2010 23:59:59:123000','MM/DD/YYYY HH24:MI:SS.FF')
SELECT bunch,of,stuff,create_date
FROM myTable
WHERE create_date >= to_date('04/10/2010','MM/DD/YYYY')
AND create_date < to_date('04/11/2010','MM/DD/YYYY')
The date 04/10/2010 includes all the date values from midnight on the 10th until 11:59:59 PM, so getting everything less than the 11th will cover all the bases. An alternative is to ensure data in myTable has the CREATE_DATE field truncated on data entry; I prefer to do that for DATE fields, and if I care about the time components, I use TIMESTAMPs.
Your 1st query is doing a string comparison with wrong results.
Your 2nd query needs to be:
WHERE create_date >= TRUNC(to_date('04/10/2010','MM/DD/YYYY'))
or add hh:mi:ss to the predicate. It's not working simply 'cos you're formatting the date in a different way to what Oracle expects.

Resources