473,785 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17981
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.c om> wrote in message
news:95******** ************@wi deopenwest.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.c om> wrote in message
news:K8******** ************@wi deopenwest.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.c om> wrote in message news:<95******* *************@w ideopenwest.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
1944
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 table contains sample # values taken from a device (SNMP) every 5
12
7701
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 million rows and if I do a select * then I do get 1 million rows. I want to be able to provide page navigation as google does, page
3
14561
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 clicks on a row and a main form (pop up) opens bound to a table with a continuous subform bound to another table. On the main form is a field called . It is vital that this is
11
1954
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 of ID's). I am looking to be able to call an ID and its Image from the database, but also have returned the previous ID and the next ID, even when those actual ID's are not necessarily +1 and -1 to the ID being called. For example, I want to...
2
9191
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
28107
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
4030
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 are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i tried to use a remove item code it worked fine, it did delete the item form the list but it added...
0
315
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 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...
6
7573
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 the problem that when I went to select the other drop down menus, the selections i made on the multiple select one would clear. Then I had tried putting the multiple select menu last so that the selections wouldn't clear but then after clicking the...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.