久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在一條 SQL 記錄中查找并發(fā)用戶數(shù)

Find number of concurrent users in a SQL records(在一條 SQL 記錄中查找并發(fā)用戶數(shù))
本文介紹了在一條 SQL 記錄中查找并發(fā)用戶數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有以下結構的表格:

UserID   StartedOn          EndedOn
1        2009-7-12T14:01    2009-7-12T15:01 
2        2009-7-12T14:30    2009-7-12T14:45
3        2009-7-12T14:47    2009-7-12T15:30
4        2009-7-12T13:01    2009-7-12T17:01
5        2009-7-12T14:15    2009-7-12T18:01
6        2009-7-12T11:01    2009-7-12T19:01
1        2009-7-12T16:07    2009-7-12T19:01

我需要找到在線的最大并發(fā)用戶數(shù).在上表中,結果為 5,因為用戶 set1={1,2,4,5,6} 和 set2={1,3,4,5,6} 在同一時期在線.

I need to find the maximal number of concurrent users that were on line. In the above table the result would be 5 because users set1={1,2,4,5,6} and set2={1,3,4,5,6} were online in the same period.

你知道如何僅使用 T-SQL 來計算這個嗎?

Do you have an idea how one could calculate this using T-SQL only?

推薦答案

顯然并發(fā)用戶數(shù)只會在用戶開始或結束一個時期時發(fā)生變化,因此確定開始和結束期間的并發(fā)用戶數(shù)就足夠了.所以,重用 Remus 提供的測試數(shù)據(jù)(謝謝 Remus):

Clearly the number of concurrent users only changes when a user either starts or ends a period, so it is enough to determine the number of concurrent users during starts and ends. So, reusing test data provided by Remus (thank you Remus):

DECLARE @Table TABLE 
(
  UserId int, 
  StartedOn datetime,
  EndedOn datetime
);

insert into @table (UserId, startedOn, EndedOn)
select 1, '2009-7-12 14:01', '2009-7-12 15:01'
union all select 2, '2009-7-12 14:30', '2009-7-12 14:45'
union all select 3, '2009-7-12 14:47', '2009-7-12 15:30'
union all select 4, '2009-7-12 13:01', '2009-7-12 17:01'
union all select 5, '2009-7-12 14:15', '2009-7-12 18:01'
union all select 6, '2009-7-12 11:01', '2009-7-12 19:01'
union all select 1, '2009-7-12 16:07', '2009-7-12 19:01';

SELECT MAX(ConcurrentUsers) FROM(
SELECT COUNT(*) AS ConcurrentUsers FROM @table AS Sessions 
JOIN 
(SELECT DISTINCT StartedOn AS ChangeTime FROM @table
) AS ChangeTimes
ON ChangeTime >= StartedOn AND ChangeTime < EndedOn 
GROUP BY ChangeTime
) AS ConcurrencyAtChangeTimes
-------
5

順便說一句,使用 DISTINCT 本身并不是一個錯誤——只有濫用 DISTINCT 才是.DISTINCT 只是一個工具,在這種情況下使用它是完全正確的.

BTW using DISTINCT per se is not a mistake - only abusing DISTINCT is. DISTINCT is just a tool, using it in this context is perfectly correct.

我正在回答 OP 的問題:如何僅使用 T-SQL 來計算".請注意,該問題并未提及性能.

I was answering the OP's question: "how one could calculate this using T-SQL only". Note that the question does not mention performance.

如果問題是這樣的:如果數(shù)據(jù)存儲在 SQL Server 中,確定最大并發(fā)的最快方法是什么",我會提供不同的答案,如下所示:

If the questions was this: "what is the fastest way to determine maximum concurrency if the data is stored in SQL Server", I would provide a different answer, something like this:

考慮以下替代方案

  1. 寫游標
  2. 編寫一個 CLR 游標
  3. 在客戶端寫一個循環(huán)
  4. 使用具有合適游標的 RDBMS,例如 Oracle 或 PostgreSql
  5. 為了獲得最佳性能,請以不同的方式設計您的表格,以便您可以在一次索引查找中檢索答案.如果我需要提供最佳性能,這就是我在我的系統(tǒng)中所做的.

如果問題是使用 T-SQL 查詢確定最大并發(fā)的最快方法是什么",我可能根本不會回答.原因是:如果我需要非常好的性能,我不會在 T-SQL 查詢中解決這個問題.

If the question was "what is the fastest way to determine maximum concurrency using a T-SQL query", I would probably not answer at all. The reason: if I needed really good performance, I would not solve this problem in a T-SQL query.

這篇關于在一條 SQL 記錄中查找并發(fā)用戶數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

SQL trigger on Truncate(截斷時的 SQL 觸發(fā)器)
sql search query with multiple optional search parameters(具有多個可選搜索參數(shù)的 sql 搜索查詢)
SQL Efficiency: WHERE IN Subquery vs. JOIN then GROUP(SQL 效率:WHERE IN 子查詢 vs. JOIN 然后 GROUP)
Retrieving XML element name using t-SQL(使用 t-SQL 檢索 XML 元素名稱)
Insert double quotes into SQL output(在 SQL 輸出中插入雙引號)
Delete rows from CTE in SQL SERVER(從 SQL SERVER 中的 CTE 中刪除行)
主站蜘蛛池模板: 夜夜草导航 | 久久精品一区二区三区不卡牛牛 | 日韩中文字幕一区二区三区 | 亚洲精品成人在线 | 午夜tv| 精品伊人久久 | 成年人黄色 | 久久国产精品一区二区 | a在线观看 | 欧美在线性爱视频 | 国产成人免费在线视频 | 国产成人在线视频 | 欧美亚洲国产精品 | av激情小说 | 国产伦精品一区二区三区视频黑人 | 国产精品一 | 一级毛片在线播放 | 亚洲激情偷拍 | 亚洲在线 | 亚洲精品在线看 | 三年中文在线观看免费大全中国 | 欧美精品xxx| 国产又粗又猛视频免费 | 中文字幕国产精品 | 黄色一级片黄色一级片 | 亚洲国产91 | 欧美在线视频观看 | 99久久久国产精品免费蜜臀 | 五月天综合网 | 日韩国产精品一区二区 | 欧美黄色片在线观看 | 成人综合网站 | 国内精品一区二区三区 | 日韩成人在线播放 | 欧美专区在线 | 国产精品99久久久久久久久 | 98久久| 在线观看日韩av | 久久久网 | 久草福利资源 | 午夜影院在线观看视频 |