我試圖構建分析頁面,現在我遇到了問題;用戶根據他們的會話訪問頁面,如果該會話過期,那麼他們被提供一個新的會話。僅使用SQL計算最近的行
我試圖確定一個方法來計算用戶,他們的最後一次會議是使用此查詢某個頁面上的號碼:
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
這查詢將返回結果如下:
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
我希望能夠做的是能夠統計其會話僅結束的行,並截斷所有重複結果(通過使用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
這是可能的嗎?
請提供關於表架構的信息 –
您想要使用「group by」和「count(*)」的組合。 – arkascha
@AbdoAdel你是什麼意思關於表模式的信息? – Brad