473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

select next highest salary or last login date ...

good spam subject ;).

anyway, i'm alittle stumped. i'm in need of putting together a query
that gets the next highest salary ( select max ( sal ) - 1?, from an
emp_sal type table. another example is if i have a login_history table,
and i'm logged in, but i want to display the date of my previous login.
if the login_history table has user_id and login_date which makes up
the primary key, how can i get the most recent login (minus the most
recent one?).

the max function comes to mind and use of the rowid/rownum colums, but
is there an easier, more obvious way to do this perhaps?

Jul 19 '05 #1
4 17955
logically, you're asking for

the (sort of) maximum value of (XXX) within group ZZZZZ
where the value is less than the actual maximum value of (XXX) within group
ZZZZ

figure out the individual steps need to solve the problem, then figure out
how to plug them together into one SQL
approach 1:
------------
select max(xxxx) from yyyyy where the_grouping = 'ZZZZZ'

select max(xxxx) from yyyyy where the_grouping = 'ZZZZZ' and xxxx < the
value i just got in my previous query

:: uses as subquery in the WHERE clause
approach 2:
------------
select the top two values of xxxx from yyyyy where the_grouping = 'ZZZZZ',
sorted in descending order; when reading the results, ignore the first row
and keep the second one

:: uses a subquery in the FROM clause and the ROWNUM pseudo column -- less
intuitive but better performance than the first approach; you'll limit the
subquery rows with ROWNUM, and return ROWNUM (with an alias) so that you can
reference it in the outer query
let me know if that helps you figure out the query structure, or if you'd
like me to post examples

--
Mark C. Stock
email mcstock -> enquery(dot)com
www.enquery.com
"Mark" <ma**@nowhere.com> wrote in message
news:95********************@wideopenwest.com...
| good spam subject ;).
|
| anyway, i'm alittle stumped. i'm in need of putting together a query
| that gets the next highest salary ( select max ( sal ) - 1?, from an
| emp_sal type table. another example is if i have a login_history table,
| and i'm logged in, but i want to display the date of my previous login.
| if the login_history table has user_id and login_date which makes up
| the primary key, how can i get the most recent login (minus the most
| recent one?).
|
| the max function comes to mind and use of the rowid/rownum colums, but
| is there an easier, more obvious way to do this perhaps?
|
Jul 19 '05 #2
Thanks!!! This'll work.

mcstock wrote:
logically, you're asking for

the (sort of) maximum value of (XXX) within group ZZZZZ
where the value is less than the actual maximum value of (XXX) within group
ZZZZ

figure out the individual steps need to solve the problem, then figure out
how to plug them together into one SQL
approach 1:
------------
select max(xxxx) from yyyyy where the_grouping = 'ZZZZZ'

select max(xxxx) from yyyyy where the_grouping = 'ZZZZZ' and xxxx < the
value i just got in my previous query

:: uses as subquery in the WHERE clause
approach 2:
------------
select the top two values of xxxx from yyyyy where the_grouping = 'ZZZZZ',
sorted in descending order; when reading the results, ignore the first row
and keep the second one

:: uses a subquery in the FROM clause and the ROWNUM pseudo column -- less
intuitive but better performance than the first approach; you'll limit the
subquery rows with ROWNUM, and return ROWNUM (with an alias) so that you can
reference it in the outer query
let me know if that helps you figure out the query structure, or if you'd
like me to post examples


Jul 19 '05 #3
good...

couple comments on approach 2:

it actually requires a double in-line view (from clause sub-query), the
inner one is sorted, the next outer grabs the first 2 rows and returns the
aliased ROWNUM, the outer throws away the first row

also, the innermost query must select distinct salaries, so that it returns
the 2nd highest value, not the highest salary from the 2nd row in which the
highest salary was found

-- mcs

"Mark" <ma**@nowhere.com> wrote in message
news:K8********************@wideopenwest.com...
| Thanks!!! This'll work.
|
| mcstock wrote:
| > logically, you're asking for
| >
| > the (sort of) maximum value of (XXX) within group ZZZZZ
| > where the value is less than the actual maximum value of (XXX) within
group
| > ZZZZ
| >
| > figure out the individual steps need to solve the problem, then figure
out
| > how to plug them together into one SQL
| >
| >
| > approach 1:
| > ------------
| > select max(xxxx) from yyyyy where the_grouping = 'ZZZZZ'
| >
| > select max(xxxx) from yyyyy where the_grouping = 'ZZZZZ' and xxxx < the
| > value i just got in my previous query
| >
| > :: uses as subquery in the WHERE clause
| >
| >
| > approach 2:
| > ------------
| > select the top two values of xxxx from yyyyy where the_grouping =
'ZZZZZ',
| > sorted in descending order; when reading the results, ignore the first
row
| > and keep the second one
| >
| > :: uses a subquery in the FROM clause and the ROWNUM pseudo column --
less
| > intuitive but better performance than the first approach; you'll limit
the
| > subquery rows with ROWNUM, and return ROWNUM (with an alias) so that you
can
| > reference it in the outer query
| >
| >
| > let me know if that helps you figure out the query structure, or if
you'd
| > like me to post examples
| >
|
Jul 19 '05 #4
Problem just calls for analytic functions:

select * from
(
select e.*, rank() over (order by sal desc) rn
from emp e )
where rn = 2


Mark <ma**@nowhere.com> wrote in message news:<95********************@wideopenwest.com>...
good spam subject ;).

anyway, i'm alittle stumped. i'm in need of putting together a query
that gets the next highest salary ( select max ( sal ) - 1?, from an
emp_sal type table. another example is if i have a login_history table,
and i'm logged in, but i want to display the date of my previous login.
if the login_history table has user_id and login_date which makes up
the primary key, how can i get the most recent login (minus the most
recent one?).

the max function comes to mind and use of the rowid/rownum colums, but
is there an easier, more obvious way to do this perhaps?

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Got2Go | last post by:
Hello Group, I have a table that has 3 columns: ID (int), datetime, Value(varchar) ID = ID for the SNMP device datetime = time record was added value = value added for that device. This...
12
by: TP | last post by:
Here is my problem. I need to display a table about which I have no information except the table name. Using metadata I can somehow show the column names and record values. But my table has 1...
3
by: William Wisnieski | last post by:
Hello Again, I'm really stuck on this one.....so I'm going to try a different approach to this problem. I have a query by form that returns a record set in a datasheet. The user double...
11
by: Neo Geshel | last post by:
I have an Access DB, from which I am going to pull images. Each image has an associated ID, but the ID's are not necessarily sequential (some images may have been deleted, leaving gaps in the list...
2
by: partha das | last post by:
sir i need a single sql query it may be sub query need to retrive the 5th highest salary from a employee table
6
by: apking | last post by:
please write the programe in c language for this Accept 5 Employee details to find highest salary employe name using for loop and arrays Thanks in advance
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
0
by: Mark | last post by:
good spam subject ;). anyway, i'm alittle stumped. i'm in need of putting together a query that gets the next highest salary ( select max ( sal ) - 1?, from an emp_sal type table. another...
6
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.