SQL help again

Log in to stop seeing adverts
This page may contain links to companies such as eBay and Amazon. As an affiliate of these sites I may earn commission if you click the link and make a purchase

Status
Not open for further replies.

Joe_Fox

Well-Known Member
Hi,
Who can solve this problem?

I have a table of data that has four columns, one of which is a version number:

e.g.
Code:
Item  | Ref Number | Date     | Version
Hat   | 23         | 16/10/08 | 1
Hat   | 23         | 18/02/09 | 2
Socks | 53         | 18/02/09 | 1  
Socks | 53         | 20/03/09 | 2
Socks | 53         | 30/07/09 | 3
Pants | 76         | 17/11/08 | 1
Pants | 76         | 18/12/08 | 2
Pants | 76         | 18/03/09 | 3
I need to write a query that only returns the latest version of each item together with just the Item and Ref Number columns.

So, the result should be:
Code:
Hat   | 23 | 2
Socks | 53 | 3
Pants | 76 | 3

Can someone help? Prize to the person who gets the write answer first.
 
Code:
SELECT     item, ref, version
FROM         tbltest AS t
WHERE     (version IN
                          (SELECT     MAX(version) AS Expr1
                            FROM          tbltest
                            WHERE      (t.ref = ref)
                            GROUP BY ref))

Requires that the ref field is the same per product, so all socks must be 53 etc.
 
Last edited:
select item,[ref number],max(version) as Version
from tbltest
group by item,[ref number]
 
don't mention it joe
 
I'm still in the process of testing both your answers.
The prize will come in good time. Patience, patience.
 
Darth, who is your favourite actress, Beighton, same question to you?
(p.s. If either of you prefer 'actors' then say who your favourite actor is instead)
 
Well you missed your chance of a present there (both of you).

Any way, I need some more help now.

I've written some SQL that joins three tables.

Here is an example:
Code:
select a.po,
         b.bo,
         c.mo

from ano a, bno b, cno, c

where a.po = b.po and b.bo = c.bo

This returns the rows I expect but I'm only really interested in the data in column c.mo.
An example of what I get back is this:

Code:
543    6   Black
432    6   Black
732    3   White
991    1   Green
191    1   Green
291    1   Green
452    6   Black
462    6   Black
472    6   Black
482    6   Black

but all I want to see is a distinct value, no duplicates:

Code:
543    6   Black
732    3   White
991    1   Green

As the data in the first two columns is not important, is there a way to just show the distinct values returned in the third column?

Thanks,
Joe
 
Last edited:
Damn, I sussed it myself (although writing the problem out actually made me realise what I needed to do)

Here is what was needed:

Code:
select distinct c.mo

from ano a, bno b, cno, c

where a.po = b.po and b.bo = c.bo

simple as that leeeeeeeeeeeeeeeeeeeeeeeeek
 
joins in the where clause....old skool :)
 
Status
Not open for further replies.
Log in to stop seeing adverts

P Pld Pts
1Liverpool1639
2Chelsea1735
3Arsenal1733
4Nottm F1731
5Bournemouth1728
6Aston Villa1728
7Manchester C  1727
8Newcastle1726
9Fulham1725
10Brighton1725
11Tottenham 1723
12Brentford1723
13Manchester U1722
14West Ham1720
15Everton1616
16Palace1716
17Leicester1714
18Wolves1712
19Ipswich1712
20Southampton176

Latest posts

Back
Top