简体   繁体   English

使用SQL仅计算最近的行

[英]Using SQL to COUNT the most recent rows only

I'm trying to build an analytic page, and now I've encountered a problem; 我正在尝试建立一个分析页面,但是现在遇到了一个问题。 users visit pages based on their session, if that session expires then they are provided with a new one. 用户根据会话访问网页,如果该会话过期,则会为他们提供新的会话。

I'm trying to determine a way to COUNT the number of users where their last session was on a certain page using this query: 我正在尝试使用此查询确定一种方法来计算上一次会话在某个页面上的用户数:

SELECT DISTINCT (SELECT MAX(session) FROM analytics b WHERE a.session=b.session) as session,(SELECT MAX(DISTINCT location) FROM analytics c WHERE c.session=a.session) as locale FROM analytics a

That query will return results as follows: 该查询将返回结果,如下所示:

session |           location            
------------------------------------------
1       | http://random-page.io/index.html -- Same session, first entry          
1       | http://random-page.io/index.html -- Same session, second entry        
1       | http://random-page.io/index.html -- Same session, last entry   <- What We're trying to Count        
2       | http://random-page.io/index.html -- Same session, first entry        
2       | http://random-page.io/index.html -- Same session, last entry <- What We're trying to Count          
3       | http://random-page.io/index.html -- One session, presumably serves as last and first entry.. but last is what matters  <- What We're trying to Count
4       | http://random-page.io/drafts.html -- One Session  <- What We're trying to Count
5       | http://random-page.io/elements.html -- One session  <- What We're trying to Count

What I want to be able to do is be able to count the rows where their session ends only, and to truncate all duplicate results (by using GROUP BY and COUNT) so that my query returns the following: 我想要做的是能够计算仅会话结束的行,并截断所有重复的结果(通过使用GROUP BY和COUNT),以便我的查询返回以下内容:

count   |           location            
------------------------------------------
3       | http://random-page.io/index.html -- The count is 3 and not 5 because there are 3 sessions in which the LAST entry for their assigned session is http://...index.html
1       | http://random-page.io/drafts.html -- You catch my drift
1       | http://random-page.io/elements.html  -- Coolio <3

Is this at all possible? 这是可能吗?

Looks like you require a subselect for this...: 看起来您需要为此的子选择...:

SELECT count(session) AS count, location FROM ( 
    SELECT session, location from requests GROUP BY session
) AS I 
GROUP BY location;

Here is a sql fiddle to play around with: http://sqlfiddle.com/#!9/41f15/20 这是一个可玩的sql小提琴: http : //sqlfiddle.com/#!9/41f15/20

try this (well it's ugly but I can't figure out another way to do it) 试试这个(这很丑,但是我想不出另一种方法来做)

select
    grouped_analytics.location, count(grouped_analytics.session)
from
    (select 
        analytics.session,analytics.location 
    from 
        analytics 
    group by 
        analytics.session, analytics.location
    ) as grouped_analytics
group by
    grouped_analytics.location

You can give this a try: 您可以尝试一下:

SELECT
  COUNT(*) AS count,
  a.lastEntry AS location
FROM (
        SELECT 
          session, 
          SUBSTRING_INDEX(GROUP_CONCAT(location), ',', -1) AS lastEntry
        FROM analytics 
        GROUP BY session
    ) AS a
GROUP BY a.lastEntry;

Here is the sqlfiddle . 这是sqlfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM