Solved

Grouping SQL Data

Posted on 2011-02-14
8
428 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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
 
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

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.

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.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

806 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