我很难搞清楚我正在处理的SQL查询的最后一部分。我有一对表需要从中提取数据。一个表包含无线接入点的UID及其可读名称(假设0.38.153.35.118.16具有人类可读的AP-9999-9-NW名称)。另一行包含数千行轮询数据,每行包含该轮询的时间戳,访问点的UID以及连接的客户端数。
原始数据如下所示:
DateTime AP Name Clients
2013-10-09 18:35:23.417 0.38.153.35.118.16.0 3
2013-10-09 18:35:23.417 0.38.153.35.118.16.1 14
2013-10-09 18:35:23.417 0.38.203.128.91.224.0 7
2013-10-09 18:35:23.417 0.38.203.128.91.224.1 1
但是,每个接入点都有两个我们要组合成一个结果的无线电,因此将.0和.1添加到每个UID。这引发了两个问题;我必须以某种方式结合每一行的两个结果。其次,我必须使用SUBSTRING删除最后两个字符,并通过连接将UID与另一个表结合,但这样做会丢弃最后两个字符。考虑到所有这些,我有以下查询:
SELECT
CAST([SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail].[DateTime] AS datetime) AS DateTime,
'AP Name' = SUBSTRING([SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail].[RowID], 1, LEN([SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail].[RowID]) - 2),
[SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail].[RawStatus] AS Clients
FROM
[SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail]
INNER JOIN
[SolarWindsOrion].[dbo].[CustomPollerStatus]
ON Left([SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail].[RowID], Len([SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail].[RowID]) - 2) = [SolarWindsOrion].[dbo].[CustomPollerStatus].[RowID]
WHERE
[SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail].[CustomPollerAssignmentID] = '6C4E621B-A7D3-439C-8402-D692BE67743A'
这就是我的思绪融化的地方。所有这一切都是从AP名称中删除两个尾随字符:
DateTime AP Name Clients
2013-10-09 18:35:23.417 0.38.153.35.118.16 3
2013-10-09 18:35:23.417 0.38.153.35.118.16 14
2013-10-09 18:35:23.417 0.38.203.128.91.224 7
2013-10-09 18:35:23.417 0.38.203.128.91.224 1
这让我更接近,但是连接什么都没做,我仍然希望将它们共享的行组合成AP名称。 Group By似乎是最有用的选项,但我似乎无法弄清楚如何在我专门为结果创建的列上使用它。最终的目标是得到这样的东西:
DateTime AP Name Clients
2013-10-09 18:35:23.417 AP-9999-9-NW 17
2013-10-09 18:35:23.417 AP-1234-5-SC 8
有什么想法吗?很抱歉可能提供太多信息。
答案 0 :(得分:1)
我通常不喜欢别名表名称,但你的代码只是尖叫它:)
有点难以理解您期望输出的内容,此解决方案假设您需要每个DateTime和AP组合的客户端总数。
SELECT
Detail.[DateTime],
-- you didn't tell us the name of this column
Status.[YOUR HUMAN READABLE NAME],
SUM(Detail.[RawStatus]) AS Clients
FROM
[SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail]
AS Detail
INNER JOIN [SolarWindsOrion].[dbo].[CustomPollerStatus]
AS Status
ON Left(Detail.[RowID], Len(Detail.[RowID]) - 2) = Status.[RowID]
WHERE
Detail.[CustomPollerAssignmentID] = '6C4E621B-A7D3-439C-8402-D692BE67743A'
GROUP BY
Detail.[DateTime],
Status.[YOUR HUMAN READABLE NAME]
答案 1 :(得分:1)
因此,首先,我建议您使用别名来简化代码。
from
[SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail] cpsd
join [SolarWindsOrion].[dbo].[CustomPollerStatus] cps on cps.[RowID] = left(cpsd.[RowID], len(cpsd.[RowID]) - 2)
一旦你这样做,你就会更容易看到你需要做的事情:
这是我提出的代码:
select
cast(cpsd.[DateTime] as datetime) as DateTime
, cps.[Whatever is the AP Name column name]
, cpsd.[RawStatus] as Clients
from
[SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail] cpsd
join [SolarWindsOrion].[dbo].[CustomPollerStatus] cps on cps.[RowID] = left(cpsd.[RowID], len(cpsd.[RowID]) - 2)
where
cpsd.[CustomPollerAssignmentID] = '6C4E621B-A7D3-439C-8402-D692BE67743A'
group by
cast(cpsd.[DateTime] as datetime)
, cps.[Whatever is the AP Name column name]
, cpsd.[RawStatus]