sort data by date.. pls help


 
Thread Tools Search this Thread
Top Forums Programming sort data by date.. pls help
# 1  
Old 08-19-2010
sort data by date.. pls help

hi all,
could anyone help me?
I need to query output by compare dates from 2 table and i'm using a UNION query..and wanted to sort the output by date..

My query like this:
Code:
 SELECT
TO_CHAR(DATE)  DATE1,
INVOICE
FROM ACCOUNT1
WHERE DATE < (to_date('122003','MMYYYY'))         
UNION 
SELECT 
TO_CHAR(DATE)  DATE1,
INVOICE
FROM ACCOUNT2
WHERE DATE >= (to_date('122003','MMYYYY')) AND              
 ORDER BY 1 ASC;

but the output like below:
Code:
DATE1               INVOICE
--------------- ---------------- 
28-APR-08                 412.82 
28-APR-09                 363.72 
28-APR-10                 578.82 
28-AUG-07                    111 
28-AUG-08                 241.05 
28-AUG-09                 363.72 
28-DEC-06                    111 
28-DEC-09                 363.72 
28-FEB-07                    111 
28-FEB-10                 294.42 
28-JAN-07                    296 
28-JAN-08                 372.83

If there any query that can sort the output by date in ascending order like below;
Code:
DATE1               INVOICE
--------------- ---------------- 
28-FEB-07                    111 
28-AUG-07                    111 
28-APR-08                 412.82 
28-APR-09                 363.72 
28-APR-10                 578.82

Pls help,
thanks a lot
Kate

Last edited by jim mcnamara; 08-20-2010 at 06:10 AM.. Reason: code tags
# 2  
Old 08-20-2010
Try this one
Code:
#!/bin/sh

sed 's/\(..\)-\(...\)-\(..\).*/\3\2\1 &/' | \
	sed 's/JAN/01/; s/FEB/02/; s/MAR/03/; s/APR/04/; s/MAY/05/; s/JUN/06/; s/JUL/07/; s/AUG/08/; s/SEP/09/; s/OCT/10/; s/NOV/11/; s/DEC/12/;' | \
		sort | \
			cut -f 2- -d ' '

sh script.sh < input_file.txt
# 3  
Old 08-20-2010
Quote:
Originally Posted by kate katherine
...
If there any query that can sort the output by date in ascending order like below;
DATE1 INVOICE
--------------- ----------------
28-FEB-07 111
28-AUG-07 111
28-APR-08 412.82
28-APR-09 363.72
28-APR-10 578.82
...
Since you haven't mentioned your database, I'll assume that it's Oracle.

The problem with the query is that it sorts by the 1st column, which is a string (you used TO_CHAR function). So, the output is ordered by strings and not by dates.

The remedy is to either leave the dates unformatted, or format+sort in an outer SELECT query.

The testcase here shows both techniques -

Code:
SQL> 
SQL> -- Check the data in my tables
SQL> --
SQL> select * from account1;

DT         INVOICE
--------- ----------
28-SEP-02     111.22
28-NOV-02     222.33

2 rows selected.

SQL> 
SQL> select * from account2;

DT         INVOICE
--------- ----------
28-FEB-08     333.44
28-AUG-09     444.55

2 rows selected.

SQL> 
SQL> --
SQL> -- The problem is that sorting is done on character strings, and
SQL> -- not on dates. ORDER BY 1 ASC means "order by 1st column in the
SQL> -- SELECT column-list", and the 1st column is a string!
SQL> --
SQL> select to_char(dt) date1, invoice
  2    from account1
  3   where dt < to_date('122003','mmyyyy')
  4  union
  5  select to_char(dt) date1, invoice
  6    from account2
  7   where dt >= to_date('122003','mmyyyy')
  8   order by 1 asc;

DATE1              INVOICE
------------------ ----------
28-AUG-09           444.55
28-FEB-08           333.44
28-NOV-02           222.33
28-SEP-02           111.22

4 rows selected.

SQL> 
SQL> --
SQL> -- SOLUTION 1: Select *dates* and sort on *dates*
SQL> --
SQL> select dt date1, invoice
  2    from account1
  3   where dt < to_date('122003','mmyyyy')
  4  union
  5  select dt, invoice
  6    from account2
  7   where dt >= to_date('122003','mmyyyy')
  8   order by 1 asc;

DATE1         INVOICE
--------- ----------
28-SEP-02     111.22
28-NOV-02     222.33
28-FEB-08     333.44
28-AUG-09     444.55

4 rows selected.

SQL> 
SQL> --
SQL> -- The only problem with SOLUTION 1 is that the dates
SQL> -- will be displayed in the default format of the client.
SQL> -- My client is sqlplus, and its default date format is
SQL> -- "DD-MON-RR", so that's how the dates are formatted for me.
SQL> 
SQL> -- Now let's say you want to sort by dates, and want to *display*
SQL> -- those dates in some non-default character format, say, "MM/DD/YYYY".
SQL> -- For that, you'll have to be explicit.
SQL> 
SQL> -- SOLUTION 2: Format *dates* as per your preference in an outer
SQL> --         SELECT query and mention the *date column* explicitly
SQL> --         in the ORDER BY clause
SQL> --
SQL> select to_char(dt, 'mm/dd/yyyy') date1, invoice
  2    from ( select dt, invoice
  3          from account1
  4             where dt < to_date('122003','mmyyyy')
  5             union
  6            select dt, invoice
  7          from account2
  8             where dt >= to_date('122003','mmyyyy')
  9          )
 10   order by dt asc;

DATE1          INVOICE
---------- ----------
09/28/2002     111.22
11/28/2002     222.33
02/28/2008     333.44
08/28/2009     444.55

4 rows selected.

SQL> 
SQL> 
SQL> 

HTH,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed --> sort data by date

Hi, i "tried" to sort data by date. So far, i used sed to take the data from the last and the actual month. Now, after changing the year it is not working properly. i use: GNU bash, version 4.2.45(1)-release (x86_64-suse-linux-gnu) sed -n '/\//p' $Home/../scripte/pd_0.txt y is a... (6 Replies)
Discussion started by: IMPe
6 Replies

2. Shell Programming and Scripting

Sort data by date first and then remove duplicates

Hi , I have below data inside a file named ref.psv . I want to create a shell script which will do the below 2 points : (1) sort the file content first based on the latest date which is the last column in the file (actual file its the 175th column) (2)after sorting the file based on latest date... (3 Replies)
Discussion started by: samrat dutta
3 Replies

3. Shell Programming and Scripting

Sort data by date and then search by column

Hi, I have a file where data is pipe separated.First i want to sort the file content by date . Then i want to pick up the records based on the first column which should be unique and not have duplicates. NYSE|yyyrrrddd|toronto|isin|ticker|2013-05-15... (2 Replies)
Discussion started by: samrat dutta
2 Replies

4. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

6. Shell Programming and Scripting

sort the date

Hi All, Please help me to sort the date field which is in the format 2012-02-03 16:09:37.388... Platform: Red Hat linux Thanks in advance (2 Replies)
Discussion started by: jesu
2 Replies

7. Shell Programming and Scripting

How to delete already existing data in a file using perl? Pls help me!!

Dear Friends, I need urgent help from u.. I have two files,file1 & file 2.. file1 have a existing data of file2.So i want to delete those existing datas from file1 (which contain the data from file1) My file1 like this rs39348 1 1045729 A G 0.1791 0.2054 0.84 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

8. Shell Programming and Scripting

how to convert the result of the select query to comma seperated data - urgent pls

how to convert the result of the select query to comma seperated data and put in a .csv file using korn shell. Pls help me as its very urgent. Thanks, Hema. (1 Reply)
Discussion started by: Hemamalini
1 Replies

9. Shell Programming and Scripting

How to Sort?? Pls help!!

The segment titled "Before" shows how it is now and the segment "After" (shown below) is how I want it to be sorted. Probably as a CSV format so that I can export it to a spreadsheet. Need your help and suggestions!! Before: 1500.100: by the old run: -- --- --- ---- neitqodo <... (1 Reply)
Discussion started by: kn.naresh
1 Replies

10. Shell Programming and Scripting

Formatting Date Variable (Pls help)

I have a situation where I am writing a shell script that will accept a date value it will then pass this date value to an Oracle stored procedure for processing. I want to format the date into (01-SEP-08) before passing to the proc. I also want to make sure the value passed in is a date value... (1 Reply)
Discussion started by: gmoth
1 Replies
Login or Register to Ask a Question