我有一个我一直在研究的查询,无法拼凑最后一部分。我希望下面的查询返回基于给定的StartTime范围的每小时总计数,对于列[Meta_ID],并且对于那个小时唯一的总计数[Meta_ID]没有欺骗。提前感谢任何建议!
SELECT [Detail_ID]
,[Meta_ID] as PlayerID
,p.FirstName
,p.LastName
,u.FirstName as Host
,[StartTime]
,CAST(StartTime as date) AS ForDate,
DATEPART(hour,StartTime) AS OnHour,
COUNT(*) AS Totals
FROM [SDIDW].[dbo].[CDS_StatDetail] (nolock)
join CDS_Player p on p.Player_ID = Meta_ID
join CDS_User u on u.User_ID = p.HostUser_ID
--join dbo.PlayerDAP d on d.PlayerId = Meta_ID
WHERE StartTime >= '2017-06-04 00:00:00.000'
AND StartTime <= '2017-06-10 23:59:59.999'
AND StatType like '%SLOT%'
AND Meta_ID in (
10,
111,
112,
126,
127,
147,
155,
189,
234,
237,
271,
273,
287,
321,
404)
GROUP BY CAST(StartTime as date),
DATEPART(hour,StartTime),
[Detail_ID]
,[Meta_ID]
,p.FirstName
,p.LastName
,u.FirstName
,[StartTime]
这是我目前所得到的。我已删除此表中的名称。
+-----------+----------+-----------------+----------+--------+--------+
| Detail_ID | PlayerID | StartTime | ForDate | OnHour | Totals |
+-----------+----------+-----------------+----------+--------+--------+
| 209381040 | 1115 | 6/4/17 12:08 AM | 6/4/2017 | 0 | 1 |
| 209381317 | 1115 | 6/4/17 12:15 AM | 6/4/2017 | 0 | 1 |
| 209381453 | 492 | 6/4/17 12:10 AM | 6/4/2017 | 0 | 1 |
| 209381800 | 1891 | 6/4/17 12:36 AM | 6/4/2017 | 0 | 1 |
| 209381805 | 1200 | 6/4/17 12:37 AM | 6/4/2017 | 0 | 1 |
| 209382181 | 1200 | 6/4/17 12:48 AM | 6/4/2017 | 0 | 1 |
| 209382753 | 1069 | 6/4/17 12:13 AM | 6/4/2017 | 0 | 1 |
| 209382581 | 1200 | 6/4/17 1:02 AM | 6/4/2017 | 1 | 1 |
| 209383570 | 1069 | 6/4/17 1:10 AM | 6/4/2017 | 1 | 1 |
| 209383752 | 1069 | 6/4/17 1:47 AM | 6/4/2017 | 1 | 1 |
| 209386313 | 126 | 6/4/17 5:10 AM | 6/4/2017 | 5 | 1 |
| 209386352 | 126 | 6/4/17 5:22 AM | 6/4/2017 | 5 | 1 |
+-----------+----------+-----------------+----------+--------+--------+
但是我想要得到的更多的是右侧添加的列,如下所示。我试图获得每小时唯一的Meta_ID计数。
+-----------+----------+-----------------+----------+--------+--------+------+--------+
| Detail_ID | PlayerID | StartTime | ForDate | OnHour | Totals | Hour | Hosted |
+-----------+----------+-----------------+----------+--------+--------+------+--------+
| 209381040 | 1115 | 6/4/17 12:08 AM | 6/4/2017 | 0 | 1 | 0 | 5 |
| 209381317 | 1115 | 6/4/17 12:15 AM | 6/4/2017 | 0 | 1 | 1 | 2 |
| 209381453 | 492 | 6/4/17 12:10 AM | 6/4/2017 | 0 | 1 | 5 | 1 |
| 209381800 | 1891 | 6/4/17 12:36 AM | 6/4/2017 | 0 | 1 | | |
| 209381805 | 1200 | 6/4/17 12:37 AM | 6/4/2017 | 0 | 1 | | |
| 209382181 | 1200 | 6/4/17 12:48 AM | 6/4/2017 | 0 | 1 | | |
| 209382753 | 1069 | 6/4/17 12:13 AM | 6/4/2017 | 0 | 1 | | |
| 209382581 | 1200 | 6/4/17 1:02 AM | 6/4/2017 | 1 | 1 | | |
| 209383570 | 1069 | 6/4/17 1:10 AM | 6/4/2017 | 1 | 1 | | |
| 209383752 | 1069 | 6/4/17 1:47 AM | 6/4/2017 | 1 | 1 | | |
| 209386313 | 126 | 6/4/17 5:10 AM | 6/4/2017 | 5 | 1 | | |
| 209386352 | 126 | 6/4/17 5:22 AM | 6/4/2017 | 5 | 1 | | |
+-----------+----------+-----------------+----------+--------+--------+------+--------+
答案 0 :(得分:0)
尝试此查询
SELECT [Detail_ID]
,[Meta_ID] as PlayerID
,p.FirstName
,p.LastName
,u.FirstName as Host
,[StartTime]
FROM [SDIDW].[dbo].[CDS_StatDetail] (nolock)
join CDS_Player p on p.Player_ID = Meta_ID
join CDS_User u on u.User_ID = p.HostUser_ID
--join dbo.PlayerDAP d on d.PlayerId = Meta_ID
WHERE convert(varchar(8),StartTime,106) >= convert(varchar(8) ,'2017-06-04 00:00:00.000',106)
AND WHERE convert(varchar(8),StartTime,106) <= WHERE convert(varchar(8),'2017-06-10 23:59:59.999',106)
AND StatType like '%SLOT%'
AND Meta_ID in (
10,
111,
112,
126,
127,
147,
155,
189,
234,
237,
271,
273,
287,
321,
404)a