计算每个国家/地区的不同 IP 地址

Count distinct IP addresses per country

我有一个多维数据集,其中包含有关访问具有 IP 地址和国家/地区的网站的信息。

我想检索每个国家/地区的唯一 IP 地址数,如下表所示。

+-------------+----------+
| country     | count IP |
+-------------+----------+
| germany     | 2        |
| netherlands | 3        |
+-------------+----------+

但到目前为止我只想到了这个:

+-------------+---------------+--------+
| country     | IP            | visits |
+-------------+---------------+--------+
| germany     | 65.49.14.152  | 5      |
|             | 66.55.144.187 | 12     |
| netherlands | 93.114.46.11  | 2      |
|             | 93.115.94.85  | 5      |
|             | 141.105.1.7   | 1      |
+-------------+---------------+--------+

由此查询生成:

SELECT 
  NON EMPTY 
    {Hierarchize({[Measures].[Visits]})} ON COLUMNS
 ,NON EMPTY 
    CrossJoin
    (
      [Geografy.Localizacion].[Country].MEMBERS
     ,[RemoteClient].[IP].MEMBERS
    ) ON ROWS
FROM [VisitsCube];

如何修改此查询以生成第一个表格中的结果?

创建一个计算成员,用于保存每个唯一 IP 的计数。从行轴中删除 [RemoteClient].[IP].Members.

WITH MEMBER Measures.[Count IP] AS
SUM(Distinct(EXISTING [RemoteClient].[IP].Members), [Measures].[Visits])

SELECT NON EMPTY [Geografy.Localizacion].[Country].Members ON 1,
NON EMPTY Measures.[Count IP] ON 0
FROM [VisitsCube]

AdvWrks中我测试了这个方法:

WITH 
  MEMBER [Measures].[CountX] AS 
    Sum
    (
      [Customer].[Customer Geography].[City]
     ,IIF
      (
       NOT isempty([Measures].[Internet Sales Amount])
       ,1
       ,null
      )
    ) 
SELECT 
  {[Measures].[CountX]} ON 0
 ,NON EMPTY 
      [Product].[Product Categories].[Category]
 ON 1
FROM [Adventure Works]
WHERE 
  [Geography].[Geography].[Country].&[Canada];

它给出了每个产品类别有互联网销售金额的城市数量:

应用到你的情况我觉得是这样的:

WITH 
  MEMBER [Measures].[CountX] AS 
    Sum
    (
      [RemoteClient].[IP].MEMBERS
     ,IIF
      (
       NOT isempty([Measures].[Visits])
       ,1
       ,null
      )
    ) 
SELECT 
  NON EMPTY 
    [Geografy.Localizacion].[Country].MEMBERS ON 1
 ,NON EMPTY 
    [Measures].[CountX] ON 0
FROM [VisitsCube];