日期:2014-05-17  浏览次数:20775 次

求大神帮忙,求SQL语句
id   company      time     
01    公司1      2013-1-1
02    公司2      2013-1-2
03    公司3      2013-1-3
04    公司2      2013-1-4
05    公司3      2013-1-5
06    公司3      2013-1-6
07    公司2      2013-1-7
08    公司3      2013-1-8
09    公司1      2013-1-9
10    公司3      2013-1-9
11    公司1      2013-1-10
12    公司3      2013-1-11
13    公司2      2013-1-12
14    公司1      2013-1-13

现在我想知道 每间公司每周有多少条记录,请各位大神帮忙一下
------解决方案--------------------

SELECT COUNT(1),to_char(time,'IW'),company FROM TABLE
GROUP BY company, to_char(time,'IW')
ORDER BY to_char(time,'IW')

------解决方案--------------------
SELECT a.company, sum(1) zs,a.iw_start, a.iw_end
          FROM (select company,trunc(time, 'd') iw_start, (trunc(time, 'd')+6) iw_end,fdate from table_name order by time) a
         GROUP BY a.company,a.iw_start,a.iw_end
         ORDER BY a.iw_start,a.company


周日是一周的第一天,不知道这样行不行
------解决方案--------------------
SELECT a.company, sum(1) zs,a.iw_start, a.iw_end
          FROM (select company,trunc(time, 'd') iw_start, (trunc(time, 'd')+6) iw_end,time from table_name order by time) a
         GROUP BY a.company,a.iw_start,a.iw_end
         ORDER BY a.iw_start,a.company

------解决方案--------------------
SELECT company, zs, min_date, max_date
  FROM (select b.iw_start, min(time) min_date, max(time) max_date
          from (select company, trunc(time, 'd') iw_start, time
                  from table_name) b
         group by b.iw_start) a,
       (select c.company, sum(1) zs, c.iw_start
          from (select company, trunc(time, 'd') iw_start, time
                  from table_name) c
         group by c.company, c.iw_start)