?
Solved

How to get correct data from a query?

Posted on 2013-11-26
4
Medium Priority
?
261 Views
Last Modified: 2013-12-26
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 just want to have the last added record per each IdProfile using SQL, something like this:

!dMain, IdProfile, Qty
4                18        30
6                17          5

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
4 Comments
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39677977
do you have a timestamp column, if not add one and query against max(timestamp) for each IdProfile
0
 
LVL 3

Expert Comment

by:Jerry_Justice
ID: 39678024
SELECT Last(Profiles.IDMain) AS LastOfIDMain, Profiles.IDProfile, Last(Profiles.Qty) AS LastOfQty
FROM Profiles
GROUP BY Profiles.IDProfile

LastOfIDMain      IDProfile      LastOfQty
     6                          17             5
     4                          18           30

I don't use MySQL, so I am not sure if this syntax would work in MySQL.
0
 
LVL 11

Accepted Solution

by:
Amar Bardoliwala earned 2000 total points
ID: 39678071
Hello dimensionav,

if idMain is your primary key you can do something like following

SELECT * FROM profiles p1
WHERE
p1.idMain IN(SELECT MAX(p2.idMain) FROM profiles p2 group by p2.idProfile);

Hope this will help you.

Thank you.

Amar Bardoliwala
0
 

Author Comment

by:dimensionav
ID: 39740273
Guys, this is a new situation based on this question, maybe you could be interested:
http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_28325810.html
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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

764 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