[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to get previous record before the last one?

Posted on 2013-12-26
5
Medium Priority
?
332 Views
Last Modified: 2013-12-27
HI

I have a table like this named Profiles:

!dMain, IdProfile, Qty
1                17        20
2                17        33
3                18        25
4                18        30
5                17        10
6                17         5

I want to get the previuous record regarding the last added one per each IdProfile using SQL, something like this:

!dMain, IdProfile, Qty
3                18        25
5                17        10

Thanks in advance.
0
Comment
Question by:dimensionav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 39740314
The idMain values are not necessarily sequential, correct?  In other words, can the previous row may be several ids back?
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1200 total points
ID: 39740349
Here is one way to accomplish this.  It works whether or not the previous idMain is close or not.

SELECT idMain, idProfile, Qty
FROM (

SELECT idMain, Qty
     , /* if profile is same as row above,
          increment row number; else, start at 1. */
       if(idProfile = @lastprofile,
          @rownum:=@rownum+1,
          @rownum:=1) as ranking
     , /* since we are using @lastprofile in @rownum,
          we have to set @lastprofile 2nd;
          otherwise, the current and last always would equal.
        */
       @lastprofile:=idProfile as idProfile
FROM eePrvTest, 
(SELECT @rownum:=0, @lastprofile:=null) r
ORDER BY idProfile, idMain DESC

) derived
WHERE ranking = 2
;

Open in new window


The ranking approach is from an article I wrote, so you can read for further explanation:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_1555-Analytical-SQL-Where-do-you-rank.html

The key is the solution ranks the records in reverse idMain order; therefore, the outer SELECT gets the second ranked row with the first being the last entry.

I hope that makes sense.

Best regards and happy coding,

Kevin
0
 

Author Comment

by:dimensionav
ID: 39740486
The idMain values are not necessarily sequential, correct?  In other words, can the previous row may be several ids back?
 YES, can be several ids back.

Kevin, just a question, does your solution works for MySQL?
Thanks for everything.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 39740611
The solution I gave is specific to MySQL.  It would be different syntax for other languages.  The article I wrote explains the different implementations across different SQL platforms in case you need to create something similar in other systems.
0
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 800 total points
ID: 39740624
Here's another way, that does not require variables or subqueries.
SELECT p1.* 
FROM profiles p1 
JOIN profiles p2 ON p1.idMain < p2.idMain AND p1.idProfile = p2.idProfile 
GROUP BY p1.idMain 
HAVING COUNT(*) = 1;

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question