PC Review


Reply
Thread Tools Rate Thread

ORDER BY column name doesn't work

 
 
Martin
Guest
Posts: n/a
 
      14th Nov 2003
I'm trying to sort the following query by 'latest_id', but the outcome
is wrong.

SELECT id,
(SELECT Max(id) FROM forum_posts WHERE id = t1.id OR refid = t1.id) AS
latest_id
FROM forum_posts t1
WHERE catid = 1
AND refid = 0
ORDER BY 2 DESC

the result is:
id latest_id
35 39
33 34
31 40

while it should be:
id latest_id
31 40
35 39
33 34

How come? And is there another way except sorting by column id? I've
tried the following without success (generates errors):
ORDER BY (SELECT Max(id) FROM forum_posts WHERE id = t1.id OR refid =
t1.id) DESC
and
ORDER BY latest_id DESC

Thanks for help!
/Martin
 
Reply With Quote
 
 
 
 
Martin
Guest
Posts: n/a
 
      16th Nov 2003
Nobody seen this before?

/Martin
 
Reply With Quote
 
 
 
 
Tom Ellison
Guest
Posts: n/a
 
      17th Nov 2003
Dear Martin:

I think you should make your query, without the ORDER BY clause, into
a subquery, then select and sort that in the outer query:

SELECT id, latest_id FROM (
SELECT id, Max(id) FROM forum_posts
WHERE id = t1.id OR refid = t1.id)
AS latest_id
FROM forum_posts t1
WHERE catid = 1 AND refid = 0) x
ORDER BY latest_id

Please let me know if this helped.

Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts

>SELECT id,
>(SELECT Max(id) FROM forum_posts WHERE id = t1.id OR refid = t1.id) AS
>latest_id
>FROM forum_posts t1
>WHERE catid = 1
>AND refid = 0
>ORDER BY 2 DESC



On 14 Nov 2003 07:24:32 -0800, (E-Mail Removed) (Martin)
wrote:

>I'm trying to sort the following query by 'latest_id', but the outcome
>is wrong.
>
>SELECT id,
>(SELECT Max(id) FROM forum_posts WHERE id = t1.id OR refid = t1.id) AS
>latest_id
>FROM forum_posts t1
>WHERE catid = 1
>AND refid = 0
>ORDER BY 2 DESC
>
>the result is:
>id latest_id
>35 39
>33 34
>31 40
>
>while it should be:
>id latest_id
>31 40
>35 39
>33 34
>
>How come? And is there another way except sorting by column id? I've
>tried the following without success (generates errors):
>ORDER BY (SELECT Max(id) FROM forum_posts WHERE id = t1.id OR refid =
>t1.id) DESC
>and
>ORDER BY latest_id DESC
>
>Thanks for help!
>/Martin


 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      17th Nov 2003
Thank you for helping me out, Tom! Unfortunately, the query you
provided didn't generate more than errors. I'm currently trying to
bone it out to see what you tried to do, but so far, no luck...

/Martin

Tom Ellison <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Dear Martin:
>
> I think you should make your query, without the ORDER BY clause, into
> a subquery, then select and sort that in the outer query:
>
> SELECT id, latest_id FROM (
> SELECT id, Max(id) FROM forum_posts
> WHERE id = t1.id OR refid = t1.id)
> AS latest_id
> FROM forum_posts t1
> WHERE catid = 1 AND refid = 0) x
> ORDER BY latest_id
>
> Please let me know if this helped.
>
> Tom Ellison
> Microsoft Access MVP
> Ellison Enterprises - Your One Stop IT Experts

 
Reply With Quote
 
Tom Ellison
Guest
Posts: n/a
 
      17th Nov 2003
Dear Martin:

Well, I usually get pretty close. When I don't have your database I
can't see what the error is so readily. We could play ping-pong,
batting this back and forth as you tell me the error and I try to fix
it. Or maybe you'll get it by yourself.

Good luck!

Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts

On 17 Nov 2003 04:34:01 -0800, (E-Mail Removed) (Martin)
wrote:

>Thank you for helping me out, Tom! Unfortunately, the query you
>provided didn't generate more than errors. I'm currently trying to
>bone it out to see what you tried to do, but so far, no luck...
>
>/Martin
>
>Tom Ellison <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>> Dear Martin:
>>
>> I think you should make your query, without the ORDER BY clause, into
>> a subquery, then select and sort that in the outer query:
>>
>> SELECT id, latest_id FROM (
>> SELECT id, Max(id) FROM forum_posts
>> WHERE id = t1.id OR refid = t1.id)
>> AS latest_id
>> FROM forum_posts t1
>> WHERE catid = 1 AND refid = 0) x
>> ORDER BY latest_id
>>
>> Please let me know if this helped.
>>
>> Tom Ellison
>> Microsoft Access MVP
>> Ellison Enterprises - Your One Stop IT Experts


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: creating last name first, first name last from a column of first name last name data Rick Rothstein Microsoft Excel Programming 0 27th Jul 2009 07:25 AM
Name order in Contact list different from name order when sending mail resonator80 Microsoft Outlook Discussion 2 8th Feb 2007 07:34 PM
Re: how to have design column order be same as data sheet column order John Spencer Microsoft Access Queries 1 5th Dec 2006 10:43 PM
how do I reveerse name order last name, first name to first name, last name Don Smith Microsoft Excel Discussion 9 29th Nov 2006 10:55 PM
How to get Address cards to be in order of First name and then last name (and not in "file as" order)? ship Microsoft Outlook 2 26th Sep 2006 08:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:40 AM.