473,385 Members | 1,780 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,385 software developers and data experts.

How to Order these Records?

Hello,

i have this SQL code in my dataSet (I am working in ASP.Net / VB):

SELECT Author, COUNT(Author) AS totalDocuments
FROM documents
GROUP BY Author
ORDER BY COUNT(Author) DESC

What i want to do is this:
When there are 2 Authors which published the same number of documents, i want to sort them acording to the date they published their last documents. There is a "Date" field associated to each document. So i also need to load it. An Example:


Author totalDocuments Last Document Published - Most Recent
Document Date associated with that Author
John 4 10/05/2004
Michael 3 10/07/2004
Grace 2 25/06/2004
Mary 2 16/05/2004
Andrew 2 10/04/2004
Peter 1 18/05/2004

See, first the Authors are ordered by "totalDocuments" = Number of published
documents.
When 3 Authors published the same number of documents they are ordered
acording to each one published the most recent document.

Can you help me out?

Thank You Very Much,
Miguel

Nov 18 '05 #1
2 1214
ORDER BY COUNT(Document), MAX(DocDate) DESC ?

--

"Miguel Dias Moura" <we****@27NOSPAMlamps.com> a écrit dans le message de
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello,

i have this SQL code in my dataSet (I am working in ASP.Net / VB):

SELECT Author, COUNT(Author) AS totalDocuments
FROM documents
GROUP BY Author
ORDER BY COUNT(Author) DESC

What i want to do is this:
> When there are 2 Authors which published the same number of documents, i
want to sort them acording to the date they published their last documents. > There is a "Date" field associated to each document. So i also need to

load it.
> An Example:


Author totalDocuments Last Document Published - Most Recent
Document Date associated with that Author
John 4 10/05/2004
Michael 3 10/07/2004
Grace 2 25/06/2004
Mary 2 16/05/2004
Andrew 2 10/04/2004
Peter 1 18/05/2004

See, first the Authors are ordered by "totalDocuments" = Number of

published documents.
When 3 Authors published the same number of documents they are ordered
acording to each one published the most recent document.

Can you help me out?

Thank You Very Much,
Miguel

Nov 18 '05 #2
Hi Miguel:

You can specify more than one column in the ORDER BY clause to nest
the sorts.

I'm not sure what your exact schema looks like, but if you are using
SQL Server and have the sample pubs database, you can see how the
following query works the way you need:

SELECT a.au_lname, a.au_fname, COUNT(t.title_id), MAX(pubdate)
FROM authors a
INNER JOIN titleauthor ta ON
ta.au_id = a.au_id
INNER JOIN titles t ON
t.title_id = ta.title_id
GROUP BY
a.au_lname, a.au_fname
ORDER BY
COUNT(t.title_id) DESC,
MAX(pubdate) DESC

--
Scott
http://www.OdeToCode.com


On Tue, 13 Jul 2004 14:41:16 +0100, "Miguel Dias Moura"
<we****@27NOSPAMlamps.com> wrote:
Hello,

i have this SQL code in my dataSet (I am working in ASP.Net / VB):

SELECT Author, COUNT(Author) AS totalDocuments
FROM documents
GROUP BY Author
ORDER BY COUNT(Author) DESC

What i want to do is this:
> When there are 2 Authors which published the same number of documents, i

want to sort them acording to the date they published their last documents.
> There is a "Date" field associated to each document. So i also need to

load it.
> An Example:


Author totalDocuments Last Document Published - Most Recent
Document Date associated with that Author
John 4 10/05/2004
Michael 3 10/07/2004
Grace 2 25/06/2004
Mary 2 16/05/2004
Andrew 2 10/04/2004
Peter 1 18/05/2004

See, first the Authors are ordered by "totalDocuments" = Number of published
documents.
When 3 Authors published the same number of documents they are ordered
acording to each one published the most recent document.

Can you help me out?

Thank You Very Much,
Miguel


Nov 18 '05 #3

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

Similar topics

4
by: Bryan Harrington | last post by:
Can you use Select top 40 * from ... and ORDER BY... in the same query? I have the following 4 quearies that are.. for the most part, the same except the order by clause, all return a differnt...
2
by: Ryan Wagner | last post by:
I am doing many SQL insert statements to insert records into an oracle database. I need the order of the records, after inserting all records, to be the same as the order I inserted them in. Right...
5
by: tilak.negi | last post by:
We have one single hash (#) table, in which we insert data processing priority wise (after calculating priority). for. e.g. Company Product Priority Prod. Qty Prod_Plan_Date C1...
2
by: Deano | last post by:
Hi, I was just setting up a test system on a client's machine yesterday and as ever noticed a few problems. One that has thrown me is to do with the order of records in a subform (datasheet...
1
by: MJC | last post by:
Hi all, I have inheireted a database with no primary key and need to determine the order in which the records were entered. The table in question contains assessments and they are linked to an...
3
by: SJM | last post by:
I have a problem that occurs occasionally with a db for a undetermined reason which I would love to solve. I construct and append a series of 7 records to a table using ADO recordset. Each record...
8
by: kaosyeti | last post by:
i have a (hopefully) small problem. i have created a system where a user enters customer information into a table through a form. this table has no primary key. there are 9 fields on the form to...
9
by: John Rivers | last post by:
Hello, if you create this table: create table hello ( int a , int b constraint pk_hello primary key clustered ( a, b ) )
6
by: Index | last post by:
Hi, I have a file which I want to sort depending on multiple columns. Actually I want to implement the following query through programming: SELECT SUM(VALUES) GROUP BY x,y ORDER BY x,y; the...
4
by: fluff | last post by:
Hello. I want to display some data from a database in a certain order. I have a table with columns A, B, Date. I want to display the records that have data in column A first and I want those...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.