Solved

Grouping SQL Data

Posted on 2011-02-14
8
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Progress

691 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