sql查询来排序时间戳

时间:2011-02-09 11:08:03

标签: sql

select  b.id as st,a.appname as name_st ,
b.no_clicks as clicks, 
b.first_usage as ft , 
b.last_usage as lu from 
(select appname, appid from oneclick_apps) a ,
(SELECT   application_id AS id, COUNT(*) AS NO_CLICKS,
TO_CHAR( min(accessed_time),'DD-MM-YYYY hh:mm:ss') 
FIRST_USAGE,
TO_CHAR( max(accessed_time),'DD-MM-YYYY hh:mm:ss') 
LAST_USAGE 
FROM  user_db 
WHERE  upper(user_id) like upper('e243378')  
GROUP BY user_application ) b 
where b.id = a.appid  order by lu asc

是我的查询....按升序排序...仅以年份为单位...我应该做出哪些修改以便对整个dd进行排序:mm:yyyy hh:mm:ss?

2 个答案:

答案 0 :(得分:1)

select b.id as st, a.appname as name_st, 
       b.no_clicks as clicks, b.first_usage as ft , b.last_usage as lu 
from (select appname, appid from oneclick_apps) a, 
     (SELECT application_id AS id, COUNT(*) AS NO_CLICKS, 
             TO_CHAR( min(accessed_time),'DD-MM-YYYY hh:mm:ss') FIRST_USAGE, 
             TO_CHAR( max(accessed_time),'DD-MM-YYYY hh:mm:ss') LAST_USAGE 
      FROM user_db 
      WHERE upper(user_id) like upper('e243378')
      GROUP BY user_application ) b 
where b.id = a.appid 
order by lu asc

我看到了三个选项。

  1. 请勿将时间戳转换为 字符串。
  2. 将时间戳转换为字符串 正确排序为字符串。 (YYYY-MM-DD HH:MM:SS)
  3. 包括转换和 未转换的时间戳。显示转换后的;对未转变的人进行排序。 (一种变化 在#1)
  4. 我更喜欢#1。如果客户端应用程序需要不同的显示,请让他们处理它。

答案 1 :(得分:0)

首先,我不愿意维护你的代码。没有格式化会导致严重的头痛。

好的,让我们看看我们是否可以提供帮助。

为什么不把格式化移到外部范围。

这样的东西
select  b.id as st,
        a.appname as name_st ,
        b.no_clicks as clicks, 
        TO_CHAR( b.first_usage,'DD-MM-YYYY hh:mm:ss') as ft , 
        TO_CHAR( b.last_usage,'DD-MM-YYYY hh:mm:ss') as lu 
from    (
            select  appname, 
                    appid   
            from    oneclick_apps
        ) a ,
        (
            SELECT  application_id AS id, 
                    COUNT(*) AS NO_CLICKS,
                    min(accessed_time) FIRST_USAGE,
                    max(accessed_time) LAST_USAGE 
            FROM    user_db 
            WHERE  upper(user_id) like upper('e243378')  
            GROUP BY user_application 
        ) b 
where b.id = a.appid  
order by b.last_usage asc