473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Showing record rows in group by SQL

I know I should know this but here goes...

I have a table with the following rows:
UniqueID int IDENTITY
ParentUniqueID int
RecordLabel nvarchar(50)
RecordText ntext
RecordDate DateTime

What I'm trying to do is to group all records by ParentUniqueID
and display the RecordLabel, RecordText and RecordDate from the most
recent row for each grouped ParentUniqueID.

In other words I need to GroupBy ParentUniqueID and show only the
RecordLabel, RecordText and RecordDate for the MAX of RecordDate:

Records in Table:
UniqueID ParentUniqueID Recordlabel RecordText RecordDate
1 1 ThreadOne Blah-Abc 1/1/2005
2 1 ThreadOne Blah-Def 1/2/2005
3 3 ThreadTwo Blah-Ghi 1/2/3005
4 3 ThreadTwo Blah-Jkl 1/3/2005
5 1 ThreadOne Blah-Mno 1/4/2005
6 6 ThreadThree Blah-Pqr 1/5/2005

What I want to return:
UniqueID ParentUniqueID Recordlabel RecordText RecordDate
4 3 ThreadTwo Blah-Jkl 1/3/2005
5 1 ThreadOne Blah-Mno 1/4/2005
6 6 ThreadThree Blah-Pqr 1/5/2005

The problem is I can't use a groupby clause with RecordLabel,
RecordText, RecordDate because then it just returns all records (plus
Recordtext is an ntext and I have to deal with that issue as well...)
Any help is appreciated!
lq

Jul 23 '05 #1
7 1750
On 22 Jan 2005 14:36:33 -0800, laurenq uantrell wrote:
I know I should know this but here goes...

I have a table with the following rows:
UniqueID int IDENTITY
ParentUniqueID int
RecordLabel nvarchar(50)
RecordText ntext
RecordDate DateTime

What I'm trying to do is to group all records by ParentUniqueID
and display the RecordLabel, RecordText and RecordDate from the most
recent row for each grouped ParentUniqueID.

In other words I need to GroupBy ParentUniqueID and show only the
RecordLabel, RecordText and RecordDate for the MAX of RecordDate:

(snip)

Hi lauren,

Try if this works:

SELECT a.UniqueID, a.ParentUniqueID,
a.Recordlabel, a.RecordText, a.RecordDate
FROM MyTable AS a
WHERE NOT EXSTS
(SELECT *
FROM MyTable AS b
WHERE b.ParentUniqueID = a.ParentUniqueID
AND b.RecordDate > a.RecordDate)

Or, as an alternative:

SELECT a.UniqueID, a.ParentUniqueID,
a.Recordlabel, a.RecordText, a.RecordDate
FROM MyTable AS a
LEFT JOIN MyTable AS a
ON b.ParentUniqueID = a.ParentUniqueID
AND b.RecordDate > a.RecordDate
WHERE b.UniqueID IS NULL

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
This should do the job nicely...

SELECT * FROM table1
JOIN (
SELECT ParentUniqueID, Max(RecordDate) RecordDate
FROM Table1
GROUP BY ParentUniqueID
) Sub
ON Sub.ParentUniqueID = table1.ParentUniqueID
AND Sub.RecordDate = table1.RecordDate

Mr Tea
http://mr-tea.blogspot.com

"laurenq uantrell" <la*************@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I know I should know this but here goes...

I have a table with the following rows:
UniqueID int IDENTITY
ParentUniqueID int
RecordLabel nvarchar(50)
RecordText ntext
RecordDate DateTime

What I'm trying to do is to group all records by ParentUniqueID
and display the RecordLabel, RecordText and RecordDate from the most
recent row for each grouped ParentUniqueID.

In other words I need to GroupBy ParentUniqueID and show only the
RecordLabel, RecordText and RecordDate for the MAX of RecordDate:

Records in Table:
UniqueID ParentUniqueID Recordlabel RecordText RecordDate
1 1 ThreadOne Blah-Abc 1/1/2005
2 1 ThreadOne Blah-Def 1/2/2005
3 3 ThreadTwo Blah-Ghi 1/2/3005
4 3 ThreadTwo Blah-Jkl 1/3/2005
5 1 ThreadOne Blah-Mno 1/4/2005
6 6 ThreadThree Blah-Pqr 1/5/2005

What I want to return:
UniqueID ParentUniqueID Recordlabel RecordText RecordDate
4 3 ThreadTwo Blah-Jkl 1/3/2005
5 1 ThreadOne Blah-Mno 1/4/2005
6 6 ThreadThree Blah-Pqr 1/5/2005

The problem is I can't use a groupby clause with RecordLabel,
RecordText, RecordDate because then it just returns all records (plus
Recordtext is an ntext and I have to deal with that issue as well...)
Any help is appreciated!
lq

Jul 23 '05 #3
laurenq uantrell (la*************@hotmail.com) writes:
What I'm trying to do is to group all records by ParentUniqueID
and display the RecordLabel, RecordText and RecordDate from the most
recent row for each grouped ParentUniqueID.

In other words I need to GroupBy ParentUniqueID and show only the
RecordLabel, RecordText and RecordDate for the MAX of RecordDate:


One more variation to att to Hugo's suggestions:

SELECT a.RecordLabel, a.RecordText, a.RecordDate
FROM tbl a
JOIN (SELECT ParentUniqueID, maxdate = MAX(RecordDate)
FROM tbl
GROUP BY ParentUniqueID) AS b
ON a.ParentUniqueID = b.ParentUniqueID
AND a.RecordDate = b.maxdate

Note that if there are more that one row for the latest RecordDate,
you will get two rows in the output, but that is per the spec you've
given us.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Q. When is a join not a join
A. When its a join to an aggregated self view on the group condition and
aggregate

performance wise, this version is far superior to the alternates due to the
query optimiser specifically checking for this type of query and having a
special way of dealing with it. Its a shame its not this clever across the
board. :)

Mr Tea
http://mr-tea.blogspot.com

"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn********************@127.0.0.1...
laurenq uantrell (la*************@hotmail.com) writes:
What I'm trying to do is to group all records by ParentUniqueID
and display the RecordLabel, RecordText and RecordDate from the most
recent row for each grouped ParentUniqueID.

In other words I need to GroupBy ParentUniqueID and show only the
RecordLabel, RecordText and RecordDate for the MAX of RecordDate:


One more variation to att to Hugo's suggestions:

SELECT a.RecordLabel, a.RecordText, a.RecordDate
FROM tbl a
JOIN (SELECT ParentUniqueID, maxdate = MAX(RecordDate)
FROM tbl
GROUP BY ParentUniqueID) AS b
ON a.ParentUniqueID = b.ParentUniqueID
AND a.RecordDate = b.maxdate

Note that if there are more that one row for the latest RecordDate,
you will get two rows in the output, but that is per the spec you've
given us.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 23 '05 #5
Erland,
Thanks for the response and the SQL. I know it had to be a subquery but
wasn't lucid enough to figure it out.
I am actually using thr MAX of ParentUniqueID not the datetime column -
I just set it up that way to make it easier to show what I was trying
to do, so I won't get duplicate rows.
What I need to fugure out know is how to sort it with an ORDER BY
clause...
Thanks again!
lq

Jul 23 '05 #6
I'm not sure which version you're telling me to use here...

Jul 23 '05 #7
erkland's (23:04)/my (22:58) suggestion (they are the same) offers the best
performance of the 3 different ones here,

Mr Tea

, "laurenq uantrell" <la*************@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I'm not sure which version you're telling me to use here...

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: laurenq uantrell | last post by:
I know I should know this but here goes... I have a table with the following rows: UniqueID int IDENTITY ParentUniqueID int RecordLabel nvarchar(50) RecordText ntext RecordDate DateTime ...
2
by: c.anandkumar | last post by:
Hi All - I have some problems getting a small piece of javascript working correctly for Firefox. Here is what I am trying to do - 1. I have a form (like a search form) 2. I have many groups...
5
by: Allan A via DotNetMonster.com | last post by:
I have a xml document created <?xml version="1.0" standalone="yes" ?> <Group> <Information> <ID>44</ID> <Detail>Hello World</Detail> </Information> <Information> <ID>45</ID> <Detail>My...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.