473,804 Members | 4,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ordering YYYY MM DD in reverse chrono order

Hello,

I am trying to select distinct dates and order them in the reverse
chronological order. Although the column type is TIMESTAMP, in this
case I want only YYYY, MM, and DD back.

I am using the following query, but it's not returning dates back in
the reverse chronological order:

SELECT DISTINCT
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date)

FROM uus INNER JOIN ui ON uus.user_id=ui. id INNER JOIN uu ON
ui.id=uu.user_i d
WHERE uus.x_id=1

ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;
This is what the above query returns:

date_part | date_part | date_part
-----------+-----------+-----------
2004 | 2 | 6
2004 | 4 | 20
(2 rows)
I am trying to get back something like this:
2004 4 20
2004 4 19
2004 2 6
....

My query is obviously wrong, but I can't see the mistake. I was
wondering if anyone else can see it. Just changing DESC to ASC, did
not work.

Thank you!
Nov 23 '05 #1
10 2235
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;


You are sorting by three columns, only the last one is desc.

What you need is:

....
order by
date_part( 'year', uu.add_date ) desc,
date_part( 'month', uu.add_date ) desc,
date_part( 'day', uu.add_date ) desc
;

Mit freundlichem Gruß / With kind regards
Holger Klawitter
- --
lists <at> klawitter <dot> de
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAjTtF1Xd t0HKSwgYRAmaDAK CcSo5kEPkn4QJfs Fhg9EE0k/dmmwCfa7gB
cUjzCy/X0mJXW0Aooyb7pb E=
=0Fhk
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;


You are sorting by three columns, only the last one is desc.

What you need is:

....
order by
date_part( 'year', uu.add_date ) desc,
date_part( 'month', uu.add_date ) desc,
date_part( 'day', uu.add_date ) desc
;

Mit freundlichem Gruß / With kind regards
Holger Klawitter
- --
lists <at> klawitter <dot> de
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAjTtF1Xd t0HKSwgYRAmaDAK CcSo5kEPkn4QJfs Fhg9EE0k/dmmwCfa7gB
cUjzCy/X0mJXW0Aooyb7pb E=
=0Fhk
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #3
Did you try

ORDER BY
date_part('year ', uu.add_date) desc, date_part('mont h', uu.add_date) desc,
date_part('day' , uu.add_date) DESC;

Regards,
Clodoaldo
--- OtisUsenet <ot*********@ya hoo.com> escreveu: > Hello,

I am trying to select distinct dates and order them in the reverse
chronological order. Although the column type is TIMESTAMP, in this
case I want only YYYY, MM, and DD back.

I am using the following query, but it's not returning dates back in
the reverse chronological order:

SELECT DISTINCT
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date)

FROM uus INNER JOIN ui ON uus.user_id=ui. id INNER JOIN uu ON
ui.id=uu.user_i d
WHERE uus.x_id=1

ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;
This is what the above query returns:

date_part | date_part | date_part
-----------+-----------+-----------
2004 | 2 | 6
2004 | 4 | 20
(2 rows)
I am trying to get back something like this:
2004 4 20
2004 4 19
2004 2 6
...

My query is obviously wrong, but I can't see the mistake. I was
wondering if anyone else can see it. Just changing DESC to ASC, did
not work.

Thank you!

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


_______________ _______________ _______________ _______________ __________

Yahoo! Messenger - Fale com seus amigos online. Instale agora!
http://br.download.yahoo.com/messenger/

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #4
Did you try

ORDER BY
date_part('year ', uu.add_date) desc, date_part('mont h', uu.add_date) desc,
date_part('day' , uu.add_date) DESC;

Regards,
Clodoaldo
--- OtisUsenet <ot*********@ya hoo.com> escreveu: > Hello,

I am trying to select distinct dates and order them in the reverse
chronological order. Although the column type is TIMESTAMP, in this
case I want only YYYY, MM, and DD back.

I am using the following query, but it's not returning dates back in
the reverse chronological order:

SELECT DISTINCT
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date)

FROM uus INNER JOIN ui ON uus.user_id=ui. id INNER JOIN uu ON
ui.id=uu.user_i d
WHERE uus.x_id=1

ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;
This is what the above query returns:

date_part | date_part | date_part
-----------+-----------+-----------
2004 | 2 | 6
2004 | 4 | 20
(2 rows)
I am trying to get back something like this:
2004 4 20
2004 4 19
2004 2 6
...

My query is obviously wrong, but I can't see the mistake. I was
wondering if anyone else can see it. Just changing DESC to ASC, did
not work.

Thank you!

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


_______________ _______________ _______________ _______________ __________

Yahoo! Messenger - Fale com seus amigos online. Instale agora!
http://br.download.yahoo.com/messenger/

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #5
> ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;


You meant:

ORDER BY
date_part('year ', uu.add_date) DESC, date_part('mont h', uu.add_date) DESC,
date_part('day' , uu.add_date) DESC;
--
Scott Ribe
sc********@kill erbytes.com
http://www.killerbytes.com/
(303) 665-7007 voice
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #6
> ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;


You meant:

ORDER BY
date_part('year ', uu.add_date) DESC, date_part('mont h', uu.add_date) DESC,
date_part('day' , uu.add_date) DESC;
--
Scott Ribe
sc********@kill erbytes.com
http://www.killerbytes.com/
(303) 665-7007 voice
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #7
Try

SELECT DISTINCT
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date)
FROM uus INNER JOIN ui ON uus.user_id=ui. id INNER JOIN uu ON
ui.id=uu.user_i d
WHERE uus.x_id=1
ORDER BY uu.add_date DESC

The reason that your previous sort failed is that you need the DESC
keyword applied to each sort term.

John Sidney-Woollett

OtisUsenet said:
Hello,

I am trying to select distinct dates and order them in the reverse
chronological order. Although the column type is TIMESTAMP, in this
case I want only YYYY, MM, and DD back.

I am using the following query, but it's not returning dates back in
the reverse chronological order:

SELECT DISTINCT
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date)

FROM uus INNER JOIN ui ON uus.user_id=ui. id INNER JOIN uu ON
ui.id=uu.user_i d
WHERE uus.x_id=1

ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;
This is what the above query returns:

date_part | date_part | date_part
-----------+-----------+-----------
2004 | 2 | 6
2004 | 4 | 20
(2 rows)
I am trying to get back something like this:
2004 4 20
2004 4 19
2004 2 6
...

My query is obviously wrong, but I can't see the mistake. I was
wondering if anyone else can see it. Just changing DESC to ASC, did
not work.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #8
Try

SELECT DISTINCT
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date)
FROM uus INNER JOIN ui ON uus.user_id=ui. id INNER JOIN uu ON
ui.id=uu.user_i d
WHERE uus.x_id=1
ORDER BY uu.add_date DESC

The reason that your previous sort failed is that you need the DESC
keyword applied to each sort term.

John Sidney-Woollett

OtisUsenet said:
Hello,

I am trying to select distinct dates and order them in the reverse
chronological order. Although the column type is TIMESTAMP, in this
case I want only YYYY, MM, and DD back.

I am using the following query, but it's not returning dates back in
the reverse chronological order:

SELECT DISTINCT
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date)

FROM uus INNER JOIN ui ON uus.user_id=ui. id INNER JOIN uu ON
ui.id=uu.user_i d
WHERE uus.x_id=1

ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;
This is what the above query returns:

date_part | date_part | date_part
-----------+-----------+-----------
2004 | 2 | 6
2004 | 4 | 20
(2 rows)
I am trying to get back something like this:
2004 4 20
2004 4 19
2004 2 6
...

My query is obviously wrong, but I can't see the mistake. I was
wondering if anyone else can see it. Just changing DESC to ASC, did
not work.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #9
On Apr 21, 2004, at 5:19 AM, OtisUsenet wrote:
<snip>
I am using the following query, but it's not returning dates back in
the reverse chronological order:
<snip>
ORDER BY
date_part('year ', uu.add_date), date_part('mont h', uu.add_date),
date_part('day' , uu.add_date) DESC;


Maybe I'm missing something, but why not just sort by "uu.add_dat e
DESC", like so:

ORDER BY uu.add_date DESC;

Seems like it would be more efficient, and there'd be no issues about
forgetting to put DESC after each date_part() (like you have above).

eric
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #10

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

Similar topics

8
7622
by: Matt | last post by:
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.
3
15648
by: Laszlo Szijarto | last post by:
In C#, wha't the best way to reveser the bit order of a data type and then convert it back to that datatype? So, take a byte, reverse bits, convert it back to a byte. I tried to get a BitArray and then call Array.Reverse(), but how to convert back to byte? How about flipping an odd number of bits, say a group of 4 bits, then converting the 4 bits back to an int equivalent? Thanks you in advance for your help,
0
318
by: OtisUsenet | last post by:
Hello, I am trying to select distinct dates and order them in the reverse chronological order. Although the column type is TIMESTAMP, in this case I want only YYYY, MM, and DD back. I am using the following query, but it's not returning dates back in the reverse chronological order: SELECT DISTINCT
20
19801
by: mike7411 | last post by:
Is there any easy way to reverse the order of the bits in a byte in C++? (i.e. 00000001 becomes 10000000)
4
7803
by: eggie5 | last post by:
Hi, I have an XmlNodeList and I need to reverse it. Just like Array.Reverse(), but it has to stay as an XmlNodeList. Any ideas?
2
4428
by: Doug | last post by:
Hi I have a datagridview in a windows C# application and i am allowing a user to select items from the datagridview. I allow the user to copy the selected items to the clipboard and then if they chose, to past to the notepad application. But the items that are selected seem to be pasted in the reverse order than the user selected them - for example if the user selected items 1 3 5 then
2
4358
by: bassrider | last post by:
Hi There, I am trying to order my months in calendar format (Jan, Feb, Mar...), but I can not. It only shows in alphabetical order (Apr,Aug,Dec..). Does anyone now how I can change the order from alphabetic to calendar order? Thanks in advance...
0
10575
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10076
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7616
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.