TOUGH QUERY [message #186933] |
Thu, 10 August 2006 03:39  |
neeraj_kaun
Messages: 2 Registered: August 2006
|
Junior Member |
|
|
i have a table
t1(name varchar2(4),month number,sal number)
table consist of name of employee month in number from 1 to 12 and in sal column whenever they get a hike in salary that month salry eneterd
for ex
name month sal
aa 1 1500
bb 1 2000
cc 1 3000
aa 4 2000
aa 8 3000
aa 11 4500
bb 3 2800
bb 6 3700
bb 9 4200
bb 11 5300
cc 6 5700
cc 10 6200
i want to know the sal of employee when i eneterd the month and name
for ex employeee aa salary in month 6 is 2000
how i write this query
[mod-ed]Removed blank pages
[Updated on: Thu, 10 August 2006 04:59] by Moderator Report message to a moderator
|
|
|
|
Re: TOUGH QUERY [message #186949 is a reply to message #186933] |
Thu, 10 August 2006 04:45   |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
Were the 3 pages of blank lines at the end of the post absolutely necessary?
Littlefoot is right in a sense - the only way to solve this in straight SQL would be to have some sort of row generator function that you could use to fill in the missing gaps to let you build a complete picture of the salary.
Fortunately, there is a solution if you're willing to use a little pl/sql, and a really neat idea that I learned from AskTom
This lets you pass a parameter into your sql, allowing you to work round the missing data.
create table temp_sal (emp varchar2(10), sal_month number, salary number);
insert into temp_sal values ('aa', 1, 1500);
insert into temp_sal values ('bb', 1, 2000);
insert into temp_sal values ('cc', 1, 3000);
insert into temp_sal values ('aa', 4, 2000);
insert into temp_sal values ('aa', 8, 3000);
insert into temp_sal values ('aa', 11, 4500);
insert into temp_sal values ('bb', 3, 2800);
insert into temp_sal values ('bb', 6, 3700);
insert into temp_sal values ('bb', 9, 4200);
insert into temp_sal values ('bb', 11, 5300);
insert into temp_sal values ('cc', 6, 5700);
insert into temp_sal values ('cc', 10, 6200);
begin
dbms_application_info.set_client_info('6');
end;
/
PL/SQL procedure successfully completed.
select distinct emp
,first_value(salary) over (partition by emp order by sal_month desc) salary
from temp_sal
where sal_month <= sys_context('userenv','client_info');
EMP | SALARY
----------|----------
aa | 2000
bb | 3700
cc | 5700
begin
dbms_application_info.set_client_info('5');
end;
/
PL/SQL procedure successfully completed.
select distinct emp
,first_value(salary) over (partition by emp order by sal_month desc) salary
from temp_sal
where sal_month <= sys_context('userenv','client_info');
EMP | SALARY
----------|----------
aa | 2000
bb | 2800
cc | 3000
|
|
|
|
|
|
Re: TOUGH QUERY [message #186981 is a reply to message #186979] |
Thu, 10 August 2006 07:35   |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
I think you need to replace the < with a <=, but that looks like a workable solution, and somewhat less complex.
In retrospect, the sys_context wasn't needed unless the OP wanted to use this in a view. I've just been doing a load of application tracing with this sort of thing lately, so it leapt to mind.
Realistically, the OP is going to have some way of passing the month and Emp into this query, so eith your solution or
select distinct emp
,first_value(salary) over (partition by emp order by sal_month desc) salary
from temp_sal
where sal_month = :b1
and emp = :b2 would do the trick.
You might have misread my example - I'm not interpolating the salary between the months, I was just bringing back the data for all the employees (it looks similar).
|
|
|
Re: TOUGH QUERY [message #187413 is a reply to message #186933] |
Sun, 13 August 2006 15:22   |
ab_trivedi
Messages: 460 Registered: August 2006 Location: Pune, India
|
Senior Member |
|
|
I think you need to replace the < with a <=, but that looks like a workable solution, and somewhat less complex.
In retrospect, the sys_context wasn't needed unless the OP wanted to use this in a view. I've just been doing a load of application tracing with this sort of thing lately, so it leapt to mind.
Realistically, the OP is going to have some way of passing the month and Emp into this query, so eith your solution or
select distinct emp
,first_value(salary) over (partition by emp order by sal_month desc) salary
from temp_sal
where sal_month = :b1
and emp = :b2
would do the trick.
You might have misread my example - I'm not interpolating the salary between the months, I was just bringing back the data for all the employees (it looks similar).
|
|
|
|
|
|
Re: TOUGH QUERY [message #192936 is a reply to message #192928] |
Thu, 14 September 2006 05:05   |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
The statementQuote: | the row that is inserted latest will have a rowid greater than the one that is inserted earlier
| is untrue.
If there are blocks in the table that can accept new records, then these will be used instead of extending the table.
|
|
|
|
Re: TOUGH QUERY [message #193117 is a reply to message #186933] |
Thu, 14 September 2006 23:21   |
tmlaio
Messages: 6 Registered: September 2006
|
Junior Member |
|
|
Is this what you require:
select sal
from t1
where name = 'aa'
and month = (
select min(month)
from t1
where name = 'aa'
and month <= 6)
|
|
|
Re: TOUGH QUERY [message #193125 is a reply to message #192982] |
Fri, 15 September 2006 00:01  |
anithalakshmi
Messages: 9 Registered: August 2006
|
Junior Member |
|
|
Hi,
Thank you ..
I was in a view that whichever row is inserted recently will be having a rowid greater than the one that is inserted earlier
now I am clear.
|
|
|