Solved

Grouping SQL Data

Posted on 2011-02-14
8
426 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
Comment Utility
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
Comment Utility
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
Comment Utility


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
Comment Utility
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 50

Accepted Solution

by:
Lowfatspread earned 166 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 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

16 Experts available now in Live!

Get 1:1 Help Now