Wrox Programmer Forums
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 18th, 2004, 01:21 PM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default Query HELP!!!

How does distinct work? I need a query to return me the most recent replies by a specified User and well,
to put it simple, i can't do it!

lets take a simple table called Replies.

Table: Replies

Fields:
ReplyID
ArticleID
AccountID
PostDate
UpdateDate
NickName
Email

Well, i need a query like:

SELECT Distinct Top 5 ReplyID, ArticleID, AccountID, PostDate, NickName FROM
Replies WHERE AccountID=1 ORDER BY PostDate Desc

Well if you can already guess that when the person with accountid of 1 replies to an article
more than 1 time, then that entry with that articleID will show up twice in the top 5 query which i
don't need!! I need the top 5 most recent articles that have benn posted to with no duplicates entries, just
the top 5 articles they have posted to no matter how many times they responded to an article.

thanks

 
Old June 18th, 2004, 02:17 PM
Registered User
 
Join Date: Apr 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You will need to do a sub query within an other
First : Do a query that will return only the 5 last distinct ArticleID,max(PostedDate)
Second : Using last result, select the entries of Replies


Use something like this :
SELECT Replies.* from Replies
INNER JOIN
   (
   select distinct top 5 ArticleID, max(PostDate) as LastPostDate
   from replies
   where accountID = 1
   group by ArticleID
   order by max(PostDate) desc
   ) as LastPosted
on LastPosted.ArticleID = Replies.ArticleID
and LastPosted.LastPostDate = Replies.PostDate
and Replies.AccountID = 1

order by PostDate desc

 
Old June 18th, 2004, 04:10 PM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

that worked perfectly!!! thanks a bunch!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
how to make a query from an existing query raport SQL Language 3 November 13th, 2006 08:59 PM
I solved insert query.now see this Update Query. [email protected] VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.