IReport Creation Issue- Get all Months of Year - oracle

I want to create a crosstab for LeaveCount for Employees for respective Months with iReport (JasperReports). I'm using Oracle database.
The problem is, I'm getting only the months where the measure exists, I want to display all months of the year whether the measure(Leave of Employee) exists for this month or not.

You can create a table
CREATE TABLE ALLMONTHS
(
MONTHS_MM varchar2(2)
)
;
And insert all 12 months in this table(01,02,03,04...12).
Now using this table form query as below
SELECT a.MONTHS_MM,b.leavecount
FROM ALLMONTHS a
,(SELECT to_char(leavedate,'MM') AS MONTH,leavecount..."your query")b
WHERE a.MONTHS_MM=b.MONTH(+)

That solved the issue with the answer suggested by #Pu297 . Later I got an even better method which involves no table creation and saves trouble of creating a table every time i need to run report on a new database.
select a.mnth,b.leavecount from
(
SELECT to_char(to_date(LVL,'MM'),'MM') mnth
FROM (select level lvl from dual CONNECT BY LEVEL <=12)
) a
,(SELECT to_char(leavedate,'MM') AS MONTH,leavecount..."your query")b
WHERE a.mnth=b.MONTH(+)
This is better way according to me for this issue.
Cheers!!!

Related

How to query for Inactive Employees using BI Publisher in Oracle Fusion?

I'm new to BI Publisher and I'm using it through Oracle Fusion Applications.
I am trying to make a report relating to the Inactive Employees in an organization. However I am unable to figure out how to query for an inactive or terminated employee.
I initially used this query:
SELECT PERSON_ID, PERSON_NUMBER, EFFECTIVE_START_DATE, EFFECTIVE_END_DATE
FROM PER_ALL_PEOPLE_F
WHERE TRUNC(SYSDATE) NOT BETWEEN TRUNC(EFFECTIVE_START_DATE) AND TRUNC(EFFECTIVE_END_DATE)
My other considerations were attributes from the PER_ALL_ASSIGNMENTS_M table including PRIMARY_WORK_RELATION_FLAG, PRIMARY_ASSIGNMENT_FLAG and ASSIGNMENT_TYPE considering that the employee's assignment details would help somehow. However I was unsuccessful.
I wanted to know if there was any other proper way to query for inactive employees. Is there any particular attribute in any table which would tell me for certain that an employee is active or terminated? When an employee is terminated in Oracle Fusion, which all table attributes get affected?
Thank you for your help.
The easiest way to do this is simply :
SELECT * FROM YourTable t
WHERE TRUNC(t.END_DATE) <= trunc(sysdate)
Some times there is also an indication column like IS_ACTIVE or something. You can also consider adding it, and simply updating it to 1 for all the records returned from the above query.
Other then that, we can't really help you. We don't know your table structures, we don't know what data you store in them and which column indicates what .
I have found what i was looking for. ASSIGNMENT_STATUS_TYPE='INACTIVE' was what I needed (As mentioned in the question, this solution is without considering 'EFFECTIVE_END_DATE') Getting the 'latest' assignment status of an employee was what I needed to find. The following query works if the employee has only one Assignment assigned.
SELECT PAPF.PERSON_ID, PAPF.PERSON_NUMBER
FROM PER_ALL_PEOPLE_F PAPF, PER_ALL_ASSIGNMENTS_M PAAM
WHERE 1=1
AND TRUNC(PAAM.EFFECTIVE_START_DATE) = (SELECT MAX(TRUNC(PAAM_INNER.EFFECTIVE_START_DATE))
FROM PER_ALL_ASSIGNMENTS_M PAAM_INNER
WHERE PAAM_INNER.PERSON_ID=PAAM.PERSON_ID
GROUP BY PAAM_INNER.PERSON_ID)
AND PAPF.PERSON_ID=PAAM.PERSON_ID
AND PAAM.PRIMARY_FLAG='Y'
AND PAAM.ASSIGNMENT_STATUS_TYPE='INACTIVE'
AND TRUNC(SYSDATE) BETWEEN PAAM.EFFECTIVE_START_DATE AND PAAM.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
ORDER BY 1 ASC
I had help from the Oracle Support Community to get to an answer.
Link: https://community.oracle.com/message/14000136#14000136
However in a case where an employee was given an assignment say starting from year 2000 and ending at 20015, then another assignment starting from 2016 till present, the above query will return one record of the said employee as 'Inactive' if the Max Effective_start_date condition is not checked. (Since one became Inactive on 2015), even though her current Assignment status is 'Active' and she is currently not terminated.
In such a case, it is wise to retrieve the record with the greatest 'EFFECTIVE_START_DATE' from the PER_ALL_ASSIGNMENTS_M table, ie, checking if EFFECTIVE_START_DATE = MAX(EFFECTIVE_START_DATE)

Insert timestamp into Hive

Hi i'm new to Hive and I want to insert the current timestamp into my table along with a row of data.
Here is an example of my team table :
team_id int
fname string
lname string
time timestamp
I have looked at some other examples, How to insert timestamp into a Hive table?, How can I add a timestamp column in hive and can't seem to get it to work.
This is what I am trying:
insert into team values('101','jim','joe',from_unixtime(unix_timestamp()));
The error I get is:
FAILED: SemanticException [Error 10293]: Unable to create temp file for insert values Expression of type TOK_FUNCTION not supported in insert/values
If anyone could help, that would be great, many thanks frostie
Can be achieved through current_timestamp() , but only via select clause. don't even require from clause in select statment.
insert into team select '101','jim','joe',current_timestamp();
or if your hive version doesn't support leaving from in select statment
insert into team select '101','jim','joe',current_timestamp() from team limit 1;
If you don't already have a table with at least one row, you can accomplish the desired result as such.
insert into team select '101','jim','joe',current_timestamp() from (select '123') x;

Creating a plsql for backlog history

Hi I'm trying to create a procedure for calculating the backlog for each day. For eg: if i have a ticketsubmitdate on 12-sep-2015 and it is resolved on 15-sep-2015, then it should show in the backlog for 13-sep-2015 and 14-sep-2015. How do i do that for each day?
please help...
You can try this query. It will give you all the dates between two date range -
select trunc(date_col2+di) from
(select level di from dual connect by level < ((date_col1)-(date_col2)) )
order by 1

INSERT..SELECT in Oracle always fails with "SQL command not properly ended"

I am proficient in SQL-Server and other forms of SQL, but am trying to learn Oracle SQL. For some reason I cannot get even the simplest form of INSERT INTO .. SELECT .. to work, it always fails with "SQL command not properly ended."
Here is my current example:
CREATE TABLE table1 (year INT, id INT, dat DATE, categ VARCHAR(99));
INSERT INTO table1
(year, id, dat, categ)
select year, id, dat, categ from table1 where id=5000 and year=2013;
Here's a SqlFiddle of it: http://sqlfiddle.com/#!4/c4d34/1
I cannot seem to figure out what's wrong here. I have checked about a dozen other related question here at SO and more than another dozen on Google but all of the answers either don't apply, or don't work. I have also tried about a million variations of the commands above, nothing seems to work.
Any help greatly appreciated.
FWIW, I now think that this is just a SQLFiddle problem, as many had contended.
The Oracle User who reported the problem to me with my code, was of course using the full SQL statement, before I had stripped it down to try to isolate the problem. That query had a completely different problem that just happened to report the same error in SQLFiddle. Specifically, its problem was that I was using As for table aliases, which apparently are invalid in Oracle (or perhaps, just in the query I had written).
In any event, sincere thanks to all who tried to help me.
CREATE TABLE table1 (year INT, id INT, dat DATE, categ VARCHAR(99))
/
INSERT INTO table1
(year, id, dat, categ)
select year, id, dat, categ from table1 where id=5000 and year=2013
This works, that is, if you paste both statements in the left (schema) window in SQL fiddle. I dont' think SQL Fiddle allows insert..select in the SQL window at all.
SQL Fiddle
CREATE TABLE table1 (year INT, id INT, dat DATE, categ VARCHAR(99))
//
INSERT INTO table1 (year, id, dat, categ)
SELECT year, id, dat, categ
FROM table1
WHERE id = 5000 AND year=2013
//
I don't know why you are facing this problem but there is no issue with syntax
I think it is just how you are executing the query on fiddle i just changed the execution flow and moved Insert statement in schema build section then the whole thing worked fine without changing a word (but i have inserted some sample data to show the exact working)
see this http://sqlfiddle.com/#!4/38e62/1

connected by months

Ok, I'm new using this connect by thing. But its always quite useful. I have this small problem you guys might be able to help me...
Given start month (say to_char(sysdate,'YYYYMM')) and end month (say, to_char(add_months(sysdate, 6),'YYYYMM')), want to get the list of months in between, in the same format.
Well, I want to use this into a partitions automation script. My best shot so far (pretty pitiful) yields invalid months e.g.'201034'... (and yea, I know, incredibly inefficient)
Follows the code:
SELECT id
from
(select to_char(add_months(sysdate, 6),'YYYYMM') as tn_end, to_char(sysdate,'YYYYMM') as tn_start from dual) tabla,
(select * from
(Select Level as Id from dual connect by Level <= (Select to_char(add_months(sysdate, 1),'YYYYMM')from dual)) where id > to_char(sysdate,'YYYYMM')) t
Where
t.Id between tabla.tn_start and tabla.tn_end
how do I do to make this query return only valid months? Any tips?
cheers mates,
f.
Best way might be to separate out the row generator from the date function. So generate a list from 0 to 6 and calculate months from that. If you want to pass the months in then do that in the with clause
with my_counter as (
Select Level-1 as id
from dual
connect by Level <= 7
)
select to_char(add_months(sysdate, id),'YYYYMM') from my_counter
The example below will allow you to plug in the dates you require to work out the difference.
with my_counter as (
Select Level-1 as id
from dual
connect by level <= months_between(add_months(trunc(sysdate,'MM'), 6),
trunc(sysdate,'MM')) + 1
)
select to_char(add_months(trunc(sysdate, 'MM'), id),'YYYYMM') from my_counter
For generating dates and date ranges, I strongly suggest you create a permanent calendar table with one row for each day. Even if you keep 20 years in this table it will be small ~7500 rows. Having such a table lets you attach additional (potentially non-standard) information to a date. For example your company may use a 6-week reporting period which you cannot extract using TO_CHAR / TO_DATE. Pre-compute it and store it in this table.
Oh, and Oracle 11g has automatic partition management. If you are stuck with 10g, then this article may be of interest to you? Automatic Partition Management for Oracle 10g
Try this:
with numbers as
( select level as n from dual
connect by level <= 7
)
select to_char (add_months (trunc(sysdate,'MM'), n-1), 'YYYYMM') id
from numbers;
ID
------
201012
201101
201102
201103
201104
201105
201106

Resources