聚合查询:按多个动态选择的列进行分组和计数

时间:2014-11-18 15:57:44

标签: sql postgresql aggregation

我有下表logs

session_id | event_type   | event_value
-----------+--------------+------------
   505     | user_profile | /admin
   505     | page_viewed  | /profile/18
   506     | user_profile | /member
   506     | page_viewed  | /home
   507     | user_profile | /admin
   507     | page_viewed  | /profile/18
   508     | user_profile | /member
   508     | page_viewed  | /profile/18

我想在event_type(user_profile和page_viewed)中按所有可能的值进行分组,并计算每个可能的组合。

结果应如下所示:

user_profile | page_viewed | count
-------------+-------------+------
   admin     | /profile/18 |  2    # session_ids 505 and 507
   member    | /home       |  1    # session_ids 506
   member    | /profile/18 |  1    # session_ids 508

请注意,情侣user_profile, page_viewed可能会发生变化。例如,它可能是user_profile, user_gender, session_duration

可以对多列进行计数/分组like that,但我不知道如何动态选择行...

是否可以使用postgresql进行此类查询?

Sql fiddle

2 个答案:

答案 0 :(得分:1)

select user_profile, page_viewed, 
       count(*) as cnt,
       '# session_ids ' ||
       STRING_AGG(session_id, ' and ' order by session_id) as sess
from (
  select max(case when event_type = 'user_profile' 
              then event_value else null end) as user_profile,
         max(case when event_type = 'page_viewed' 
              then event_value else null end) as page_viewed,
         session_id
  from logs
  group by session_id
) sub group by user_profile, page_viewed;

有关STRING_AGG的一些信息:
http://www.postgresonline.com/journal/archives/191-String-Aggregation-in-PostgreSQL,-SQL-Server,-and-MySQL.html

答案 1 :(得分:0)

你的意思是这样的事情:(这是在Ms Sql Server中执行的,你可能需要将它改为Postgresql)

DECLARE @selectColumns AS VARCHAR(MAX)

SET @selectColumns='user_profile,page_viewed'

EXEC('SELECT '+@selectColumns+',COUNT(*) FROM tbltest GROUP BY event_type,'+ @selectColumns)