2

我很难弄清楚我正在处理的 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

有任何想法吗?很抱歉可能提供了太多信息。

4

2 回答 2

1

I'm normally not a fan of aliasing table names, but your code just screams for it :)

It is a little hard to understand what you are expecting as output, this solution assumes that you want the total number of Clients per combination of a DateTime and 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]
于 2013-10-11T16:08:57.190 回答
1

因此,首先,我建议您使用别名来简化代码。

from
    [SolarWindsOrion].[dbo].[CustomPollerStatistics_Detail] cpsd
    join [SolarWindsOrion].[dbo].[CustomPollerStatus] cps on cps.[RowID] = left(cpsd.[RowID], len(cpsd.[RowID]) - 2)

一旦你这样做了,就很容易看到你需要做什么:

  1. 做你已经在做的“左”操作
  2. 使用结果值加入 CustomPollerStatus 表
  3. AP 名称上的组

这是我想出的代码:

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]
于 2013-10-11T16:15:06.610 回答