Solved

Grouping SQL Data

Posted on 2011-02-14
8
427 Views
Last Modified: 2012-05-11
I have a problem I can't get my head round I have a datatable containing the following fields Region, id, loaddescription , dateadded and I need to create a results set that gives me the latest added item for each region, the region order is not important but if it can be added great..

Typical data
id Region  dateadded            Description
1    2           02/02/11 12:00      xyz
2    1           02/02/11 11:00      abc
3    3           10/02/11 10:00      xyz2
4    3           10/02/11 11:00      grt
5    4           11/02/11  05:00      xyz1
6    2           11/02/11  06:00      xyzabc
7    1           11/02/11  09:00      qwerty
8    1           11/02/11 11:00


So what I'm after is a resultsset that based on the above data looks like this (I've ommitted the description  column for clarity.)

Region   id  dateadded description
1            8   11/02/11 11:00
2            6   11/02/11 06:00
3            4   10/02/11 11:00
4            5   11/02/11 05:00

There are in total 17 regions and potentiallly thousands of records

HELP!

Mark
0
Comment
Question by:markej
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34886592
select *
from (
select region, id, dateadded ,
row_number() over ( partition by region order by dateadded desc ) rr
from your_table )
where rr = 1;
0
 
LVL 28

Assisted Solution

by:Naveen Kumar
Naveen Kumar earned 167 total points
ID: 34886598
try this if you want description as well :

select *
from (
select region, id, dateadded , description,
row_number() over ( partition by region order by dateadded desc ) rr
from your_table )
where rr = 1;
0
 
LVL 10

Assisted Solution

by:John Claes
John Claes earned 167 total points
ID: 34886843


This would by my answer


select * from [TBL] where id in (select max(id) from [TBL] group by Region) order by region

What I do is to take the max Id for each Region and then select the records for witch the Id's are returned.
0
 

Author Comment

by:markej
ID: 34887011
I tried  using the following
select *
from (
select region, id, dateadded , description,
row_number() over ( partition by region order by dateadded desc ) rr
from your_table )
where rr = 1;

BUT I get an error SQL Server doesn't recognise the "Over" command.

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 50

Accepted Solution

by:
Lowfatspread earned 166 total points
ID: 34887254
which version /edition of sql server are you using?

OVER is only available from sql 2005 onwards....

why have you included oracle as a zone?

select a.*
  from yourtable as A
 where not exists (select id from yourtable as x
                           where a.region=x.region
                            and a.dateadded<x.dateadded
                            and a.id < x.id)
 order by a.region,a.id
     
0
 

Author Comment

by:markej
ID: 34887541
I thought I had deselected Oracle, missclick rather than intentional., as for OVER I thought my ISP's SQL Server was running 2005, how do I check?

That works as well thanks.

0
 

Author Closing Comment

by:markej
ID: 34887556
Partial hicup over the OVER cmd and Selecting ORACLE but apart from that 3 answers that were good and presented quickly
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34887653
select @@version

even if they are running sql server 2005 , your database may be ruynning at at lower compatability level

You can check the compability of a database simply by running the below sql command.

sp_dbcmptlevel 'DatabaseName'

60 = SQL Server 6.0
65 = SQL Server 6.5
70 = SQL Server 7.0
80 = SQL Server 2000
90 = SQL Server 2005
100= sql server 2008
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now