按日期分组存储为varchar2

时间:2017-08-25 07:53:50

标签: sql oracle date varchar2

我正在尝试运行以下查询:

SELECT TAB.week_end, 
    COUNT(TAB.id)
FROM (
        SELECT C.week_end,              
            C.ID   
        FROM my_table C     
        WHERE C.week_end IS NOT NULL
    ) TAB
GROUP BY TAB.week_end, TAB.id

预期结果:

week_end  id
2016-01   45000
2016-02   61000
2016-03   59000

我得到的是:

week_end  id
2016-01   1
2016-01   1
2016-02   1
2016-02   1

现在我知道按日期分组的困难。但在我的情况下,日期已存储为varchar2。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:3)

使用以下内容:

 SELECT TAB.week_end, COUNT(TAB.id)
 FROM (SELECT C.week_end,              
         C.ID   
 FROM my_table C     
 WHERE C.week_end IS NOT NULL) TAB
 GROUP BY TAB.week_end

如果两个表相同,则可以实现如下:

 SELECT C.week_end, COUNT(C.id)  
 FROM my_table C     
 WHERE C.week_end IS NOT NULL
 GROUP BY C.week_end

答案 1 :(得分:0)

通过解决方法从组中删除Tab.id