473,320 Members | 1,958 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,320 software developers and data experts.

getting the last result

Hello,

I have following table:
product| ordered_by | date
n1 | A | 2006-01-01
n1 | A | 2006-02-01
n1 | D | 2006-03-01
n1 | B | 2006-05-01
n2 | B | 2006-01-01
n2 | C | 2006-04-01
....

As a result I only want one set per product with the latest "ordered_by".
In this example:
n1, B and
n2, C

So far I solved it within the executing program but I was wondering if there
is an efficient query to do the same.

Thanks,
James
May 26 '06 #1
5 4613
James Scott wrote:
As a result I only want one set per product with the latest "ordered_by".
In this example:
n1, B and
n2, C


SELECT t1.*
FROM tablename t1
LEFT OUTER JOIN tablename t2 ON t1.product = t2.product AND t1.`date` <
t2.`date`
WHERE t2.`date` IS NULL

In other words, "show me the row where there is no other row with the
same product and a greater date."

Regards,
Bill K.
May 26 '06 #2
or...

select product,order_by,max(date) from tablea group by
product,order_by;

May 26 '06 #3
or...

select product,order_by,max(date) from tablea group by
product,order_by;

May 26 '06 #4

<on*******@firstdbasource.com> wrote in message
news:11*********************@j55g2000cwa.googlegro ups.com...
or...

select product,order_by,max(date) from tablea group by
product,order_by;


This won't work. read the OPs requirements.

Rich
May 26 '06 #5

"James Scott" <js************@nospmahoo.com> wrote in message
news:IM*****************@twister.nyroc.rr.com...
Hello,

I have following table:
product| ordered_by | date
n1 | A | 2006-01-01
n1 | A | 2006-02-01
n1 | D | 2006-03-01
n1 | B | 2006-05-01
n2 | B | 2006-01-01
n2 | C | 2006-04-01
...

As a result I only want one set per product with the latest "ordered_by".
In this example:
n1, B and
n2, C

So far I solved it within the executing program but I was wondering if there is an efficient query to do the same.

Thanks,
James


If you have subqueries, this will do it:

select product
,orderedby
from [order] as o1
where orderdate = (
select max(orderdate)
from [order] as o2
where o2.product = o1.product)

Rich
May 26 '06 #6

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

Similar topics

3
by: Kali K E | last post by:
Hi, I could not understand how I can do the following things in Python. Please help me. 1. First I have to find the current directory from where the script is invoked. 2. Next I have to form a...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
8
by: Franz Steinhaeusler | last post by:
Hello NG, I want to retrieve the members of a class with a baseclass. But the problem is, how to get the non derived members. class a: def who(self): print "who"
0
by: Kenneth Keeley | last post by:
Hi, I am looking for a sample of how to get the password last set for a user in active directory in a format that we can read. I am using ASP.Net and C# I have got as far as get the value. but I...
5
by: henrycortezwu | last post by:
Hi All, I'm trying to get the "groups" of a user that belongs to a domain by just passing the user's userID & domain. Is this possible? What I tried, and is working is the ff code ( i can get...
36
by: Chuck Faranda | last post by:
I'm trying to debug my first C program (firmware for PIC MCU). The problem is getting serial data back from my device. My get commands have to be sent twice for the PIC to respond properly with...
2
by: DavidPr | last post by:
I'm getting - parse error, unexpected T_LNUMBER - error messages referring to the last "if" lines of this code. What I'm trying to do is check the database and if this user's article row isn't...
8
Alireza355
by: Alireza355 | last post by:
I have two tables: table1: number explanation 10 something here 11 something here 12 something here 13 something...
8
by: Sundhas | last post by:
I made a Webservice Operation whose return type is STRING Following is the code @WebMethod(operationName = "authorize") public String authorize(@WebParam(name = "Username") String Username) { ...
8
by: twomcfly | last post by:
Hi I have a SQL Server 2005 database with a list of results for a variety of people over time. i would like to be able to look at any row and from that row know the last 3 results for that person...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.