2017-05-23 29 views
0

我正在使用PostgreSQL。我有兩個數據庫表,我想獲取存儲在table1 daterange列中的最小和最大日期,它是字符不同的字符類型。 table1和table2使用sid映射。我希望得到table1的最大和最小日期範圍,與table2的sid進行比較。請查看演示here。結果是錯誤的。檢索日期時出錯結果

表1:

sid daterange 

100 5/25/2017 
101 1/24/2017 
102 4/4/2014 
103 11/12/2007 
104 4/24/2012 
105 01/15/2017 
106 1/1/2017 
107 3/11/2016 
108 10/10/2001 
109 1/10/2016 
110 12/12/2016 
111 4/24/2017 
112 06/28/2015 
113 5/24/2017 
114 5/22/2017 

表2:

sid description 

100 success 
101 pending 
104 pending 
105 success 
106 success 
107 success 
110 success 
111 pending 
112 failed 
113 failed 
114 pending 

下面是我的查詢:

select min(daterange) as minDate,max(daterange) as maxDate from (SELECT to_date(table1.daterange, 'DD/MM/YYYY') as daterange FROM table1,table2 where 
     table1.sid = table2.sid) tt; 

結果如下這是錯誤的(MINDATE和顯示的maxDate是錯誤的日期)。

mindate  maxdate 
2013-12-07 2019-01-07 

請指教。表1中列日期範圍是字符類型varying.I不能使用::date轉換爲日期類型,因爲我需要在我的Java Hibernate代碼使用該查詢和Java代碼是不承認::

+0

爲什麼結果是錯誤的...你想每個sid有一個結果嗎? – Hackerman

+0

no..i表示最小和最大日期顯示是錯誤的。 – user3633028

回答

1

你日期和月份的混合在日期格式字符串中。 應該是 to_date(table1.daterange,'MM/DD/YYYY')