SQLCount基于另一列不同的一列

时间:2016-05-09 19:19:17

标签: sql oracle session count

我需要计算一次访问特定页面的会话和一次或多次访问同一页面的会话。例如:在4月1日至4日之间考虑这些会议:

Move disk 1 from 1 to 3.
Move disk 2 from 1 to 2.
Move disk 1 from 3 to 2.
Move disk 3 from 1 to 3.
Move disk 1 from 2 to 1.
Move disk 2 from 2 to 3.
Move disk 1 from 1 to 3.

我无法使用子查询,因为有数百万个会话。因此,像这样的查询对我来说不起作用:

Session_id| Date
----+---------
1| 01/04/2016
1| 02/04/2016
2| 01/04/2016
3| 01/04/2016
4| 01/04/2016
4| 03/04/2016
4| 04/04/2016

答案应该是这样的:

select case when no_of_visits=1 then 'single_visit'
            when no_of_visits>1 then 'multiple_visits' end as visit,
       count(distinct session_id) as sessions
FROM   (
  select session_id,
         count(distinct date) as no_of_visits
  from   my_table
  group by session_id
) a
group by case when no_of_visits=1 then 'single_visit'
              when no_of_visits>1 then 'multiple_visits' end

有什么方法我可以做这样的事情: Visit|Sessions single_visit|2 multiple_visit|2 没有子查询或自联接? 任何帮助将深表感谢。

3 个答案:

答案 0 :(得分:0)

似乎你想要这样的东西:

select   session_id, count(distinct dt) as no_of_visits,
         case when count(distinct dt) = 1 then "Single visit" 
                                          else "Multiple visits" end as visit
from     my_table
group by session_id;

注意:我使用dt而不是“date”作为列名; date是Oracle关键字,不应将此类字用作模式,表或列名称。

答案 1 :(得分:0)

Oracle安装程序

CREATE TABLE table_name ( Session_id, "Date" ) AS
SELECT 1, DATE'2016-04-01' FROM DUAL UNION ALL
SELECT 1, DATE'2016-04-02' FROM DUAL UNION ALL
SELECT 2, DATE'2016-04-01' FROM DUAL UNION ALL
SELECT 3, DATE'2016-04-01' FROM DUAL UNION ALL
SELECT 4, DATE'2016-04-01' FROM DUAL UNION ALL
SELECT 4, DATE'2016-04-03' FROM DUAL UNION ALL
SELECT 4, DATE'2016-04-04' FROM DUAL;

查询1

SELECT visit,
       COUNT(1) AS Sessions
FROM   (
  SELECT session_id,
         CASE WHEN COUNT(1) > 1
              THEN 'Multiple Visits'
              ELSE 'Single Visit'
              END AS visit
  FROM   table_name
  GROUP BY session_id
)
GROUP BY visit;

<强>输出

VISIT             SESSIONS
--------------- ----------
Multiple Visits          2 
Single Visit             2 

查询2

SELECT first_visit - multiple_visits AS single_visit,
       multiple_visits
FROM   (
  SELECT COUNT( CASE rn WHEN 1 THEN 1 END ) AS first_visit,
         COUNT( CASE rn WHEN 2 THEN 1 END ) AS multiple_visits
  FROM   (
    SELECT rn            --
    FROM   (             --
      SELECT ROW_NUMBER() OVER ( PARTITION BY session_id ORDER BY ROWNUM ) AS rn
      FROM   table_name
    )                    --
    WHERE  rn <= 2       -- These lines are optional
  )
);

<强>输出

SINGLE_VISIT MULTIPLE_VISITS
------------ ---------------
           2               2 

答案 2 :(得分:0)

你不能避免使用子查询来获得这个结果,但你可以摆脱count(distinct)(这可能是最昂贵的部分):

select no_of_visits,
       count(*) as sessions
FROM
 (
   select session_id,
          case when min(date) <> max(date) -- at least two different dates
               then 'multiple_visits'
               else 'single_visit'
          end  as no_of_visits
   from  table_name
   group by session_id
 ) a
group by no_of_visits