我有一个包含数据的数据库表,如下所示。我试图列出作为最大并发用户数的成员的用户条件。 我能够通过使用Itzik Ben_Gan提供的解决方案获得最大并发性,在Google上进行简单查找“计算并发会话,第3部分”将帮助您实现目标。对于此数据集,答案是3(会话行结束174,175,176) 我研究过网络,但到目前为止只找到了简单计算并发会话数的方法。我想进一步扩展它并获得并发的实际用户。
我也尝试过这些方法。
SELECT L.VISIT_ID,L.PARTY_ID,L.FROM_DATE,
(SELECT COUNT(*)FROM VISIT2 L2 WHERE L2.PARTY_ID!= L.PARTY_ID AND L2.FROM_DATE< = L.FROM_DATE)AS COMEINS,
(SELECT COUNT(*)FROM VISIT2 L2 WHERE L2.PARTY_ID = L.PARTY_ID AND L2.THRU_DATE< = L.THRU_DATE)AS COMEOUTS
FROM VISIT2 L
我的预期结果是
下面是表格以及创建表格的查询。
感谢大家随时可以花时间提出建议。
<style type="text/css">
table.tableizer-table {
font-size: 12px;
border: 1px solid #CCC;
font-family: Arial, Helvetica, sans-serif;
}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #ccc;
}
.tableizer-table th {
background-color: #104E8B;
color: #FFF;
font-weight: bold;
}
</style>
<table class="tableizer-table">
<tr class="tableizer-firstrow">
<th>VISIT_ID</th>
<th>PARTY_ID</th>
<th>FROM_DATE</th>
<th>THRU_DATE</th>
</tr>
<tr>
<td>1000019158</td>
<td>1000009015</td>
<td>2014-02-18 00:57:04.837</td>
<td>2014-02-18 02:08:22.003</td>
</tr>
<tr>
<td>1000019159</td>
<td>1000006000</td>
<td>2014-02-18 08:21:04.227</td>
<td>2014-02-18 10:29:28.477</td>
</tr>
<tr>
<td>1000019160</td>
<td>1000008018</td>
<td>2014-02-18 08:49:21.937</td>
<td>2014-02-18 09:13:10.937</td>
</tr>
<tr>
<td>1000019161</td>
<td>1000006019</td>
<td>2014-02-18 09:27:43.657</td>
<td>2014-02-18 10:30:17.437</td>
</tr>
<tr>
<td>1000019162</td>
<td>1000007007</td>
<td>2014-02-18 10:13:14.520</td>
<td>2014-02-18 10:29:07.733</td>
</tr>
<tr>
<td>1000019163</td>
<td>1000008011</td>
<td>2014-02-18 11:01:14.217</td>
<td>2014-02-18 11:07:20.047</td>
</tr>
<tr>
<td>1000019164</td>
<td>1000007011</td>
<td>2014-02-18 11:05:19.570</td>
<td>2014-02-18 12:48:20.787</td>
</tr>
<tr>
<td>1000019165</td>
<td>1000008018</td>
<td>2014-02-18 11:37:52.123</td>
<td>2014-02-18 14:55:55.593</td>
</tr>
<tr>
<td>1000019166</td>
<td>NULL</td>
<td>2014-02-18 11:40:33.583</td>
<td>2014-02-18 11:40:33.583</td>
</tr>
<tr>
<td>1000019167</td>
<td>NULL</td>
<td>2014-02-18 11:40:47.573</td>
<td>2014-02-18 11:40:47.573</td>
</tr>
<tr>
<td>1000019168</td>
<td>NULL</td>
<td>2014-02-18 11:40:54.653</td>
<td>2014-02-18 11:40:54.653</td>
</tr>
<tr>
<td>1000019169</td>
<td>1000007030</td>
<td>2014-02-18 12:15:24.957</td>
<td>2014-02-18 14:09:14.417</td>
</tr>
<tr>
<td>1000019170</td>
<td>NULL</td>
<td>2014-02-18 15:32:53.630</td>
<td>2014-02-18 15:35:02.327</td>
</tr>
<tr>
<td>1000019171</td>
<td>1000008018</td>
<td>2014-02-18 15:34:05.667</td>
<td>2014-02-18 16:23:50.257</td>
</tr>
<tr>
<td>1000019172</td>
<td>1000000002</td>
<td>2014-02-18 16:31:04.303</td>
<td>2014-02-18 17:09:37.147</td>
</tr>
<tr>
<td>1000019173</td>
<td>1000006008</td>
<td>2014-02-18 16:41:35.013</td>
<td>2014-02-18 17:14:53.093</td>
</tr>
<tr>
<td>1000019174</td>
<td>1000008021</td>
<td>2014-02-18 17:40:07.073</td>
<td>2014-02-18 20:10:40.720</td>
</tr>
<tr>
<td>1000019175</td>
<td>1000006018</td>
<td>2014-02-18 18:43:06.783</td>
<td>2014-02-18 18:49:16.310</td>
</tr>
<tr>
<td>1000019176</td>
<td>1000000002</td>
<td>2014-02-18 19:57:35.370</td>
<td>2014-02-18 19:57:49.297</td>
</tr>
<tr>
<td>1000019177</td>
<td>1000008021</td>
<td>2014-02-18 20:10:43.693</td>
<td>2014-02-18 20:32:34.370</td>
</tr>
<tr>
<td>1000019178</td>
<td>1000006018</td>
<td>2014-02-18 22:26:35.477</td>
<td>2014-02-18 22:26:49.773</td>
</tr>
<tr>
<td>1000019179</td>
<td>1000000002</td>
<td>2014-02-18 22:27:37.017</td>
<td>2014-02-18 22:31:14.750</td>
</tr>
<tr>
<td>1000019180</td>
<td>1000009016</td>
<td>2014-02-18 22:40:17.093</td>
<td>2014-02-18 22:40:32.523</td>
</tr>
<tr>
<td>1000019181</td>
<td>1000008021</td>
<td>2014-02-18 23:12:04.840</td>
<td>2014-02-18 23:23:27.990</td>
</tr>
<tr>
<td>1000019182</td>
<td>1000000002</td>
<td>2014-02-18 23:20:09.737</td>
<td>2014-02-18 23:26:29.827</td>
</tr>
</table>
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[VISIT2](
[VISIT_ID] [nvarchar](20) NOT NULL,
[USER_LOGIN_ID] [nvarchar](255) NULL,
[PARTY_ID] [nvarchar](20) NULL,
[FROM_DATE] [datetime] NULL,
[THRU_DATE] [datetime] NULL
) ON [PRIMARY]
GO
答案 0 :(得分:0)
我只是猜测了你的数据。我继续将所有与其他用户重叠的用户交叉应用。这会告诉你什么时候还有其他人同时在那里。您也可以先按日期对其进行分组,这样您就可以知道时间范围而不是同时在那里的用户。我也给了你一个小提琴。 http://sqlfiddle.com/#!3/20fa29/2
SELECT *
FROM VISIT2 src
CROSS APPLY(
SELECT USER_LOGIN_ID +','
FROM VISIT2 ins
where src.party_id = ins.party_id
and ins.from_date between src.from_date and src.thru_Date
for xml path('')
) otherguests(ids)