获取特定时间的不同用户数 MDX 查询
Get Distinct User Count Over Particular Time MDX QUERY
我需要一些 mdx 帮助。
立方体详细信息:
Measures.Users -> 不同的用户计数。
我想找到与此查询等效的 mdx:
Select a.shopId , Month(TransactionDate) Month_Transaction,
Year(TransactionDate) Year_Transaction,
count(distinct b.UserID) UniqueUserCount
FROM [dbo].[shop] a
JOIN users b ON a.UserID = b.UserID
where TransactionDate >= '2018-01-01'
Group by a.shopId ,Month(TransactionDate), Year(TransactionDate)
这是我目前所拥有的,无论日期如何,它都会产生唯一的计数。我想要日期范围内的唯一计数。请让我知道如何实现这一点?
SELECT {
[Date].[Month].&[2020]&[2020-Q3]&[2020-09],
[Date].[Month].&[2020]&[2020-Q4]&[2020-10],
[Date].[Month].&[2020]&[2020-Q4]&[2020-11],
[Date].[Month].&[2020]&[2020-Q4]&[2020-12]
} ON COLUMNS, NON EMPTY
{
[ShopLocations].[Hierarchy].[Shop]
} ON ROWS
FROM [ShopperCube]
where (Measures.Users)
您可以使用 DistinctCount MDX 函数。
官方文档不是很清楚,但是,总的原则是:您将“Set”传递给此函数以获取不同的值。
MDX 示例
WITH SET MySet AS
{ [Dim User].[User Id].Children }
MEMBER Measures.SetDistinctCount AS
DISTINCTCOUNT(MySet)
SELECT { Measures.SetDistinctCount, Measures.Amount } ON 0
, { [Dim Date].[Date Key].AllMembers } ON 1
FROM [Mine]
为了验证这一点,以下是我的设置:
查询结果
此外,给定示例 sql 来创建 table 并处理不同的数据:
IF OBJECT_ID('FactTransaction') IS NOT NULL
DROP TABLE FactTransaction
GO
CREATE TABLE FactTransaction (ShopId INT, TransactionDateKey INT, UserId INT, Amount INT)
GO
IF OBJECT_ID('DimDate') IS NOT NULL
DROP TABLE DimDate
GO
CREATE TABLE DimDate(DateKey INT, FullDate DATE)
GO
IF OBJECT_ID('DimUser') IS NOT NULL
DROP TABLE DimUser
GO
CREATE TABLE DimUser(UserId INT, UserName VARCHAR(50))
GO
IF OBJECT_ID('DimShop') IS NOT NULL
DROP TABLE DimShop
GO
CREATE TABLE DimShop(ShopId INT, ShopName VARCHAR(50))
GO
--Shop 1
INSERT INTO FactTransaction values(1, 20210101, 1, 10)
INSERT INTO FactTransaction values(1, 20210101, 2, 5)
INSERT INTO FactTransaction values(1, 20210101, 3, 20)
INSERT INTO FactTransaction values(1, 20210102, 2, 10)
INSERT INTO FactTransaction values(1, 20210102, 4, 15)
INSERT INTO FactTransaction values(1, 20210103, 3, 5)
INSERT INTO FactTransaction values(1, 20210103, 4, 10)
INSERT INTO FactTransaction values(1, 20210103, 5, 20)
INSERT INTO FactTransaction values(1, 20210103, 1, 20)
--Shop 2
INSERT INTO FactTransaction values(2, 20210103, 2, 10)
INSERT INTO FactTransaction values(2, 20210103, 2, 5)
INSERT INTO FactTransaction values(2, 20210103, 2, 20)
GO
INSERT INTO DimDate VALUES(20210101, '2021-01-01')
INSERT INTO DimDate VALUES(20210102, '2021-01-02')
INSERT INTO DimDate VALUES(20210103, '2021-01-03')
GO
INSERT INTO DimUser VALUES(1, 'First')
INSERT INTO DimUser VALUES(2, 'Second')
INSERT INTO DimUser VALUES(3, 'Third')
INSERT INTO DimUser VALUES(4, 'Fourth')
INSERT INTO DimUser VALUES(5, 'Fifth')
GO
INSERT INTO DimShop VALUES(1, 'Shop 1')
INSERT INTO DimShop VALUES(2, 'Shop 2')
GO
内置的非重复计数度量提供了最大的灵活性。听起来你已经有了 Measure.Users? Users 的度量值组是否连接到 Date 和 ShopLocations 的维度?
为了帮助实现这一点,我将回顾多对多革命论文中的 Distinct Count 模式。这种方法提供了一种无代码解决方案,它更灵活并且可能更快 运行:
我需要一些 mdx 帮助。
立方体详细信息:
Measures.Users -> 不同的用户计数。
我想找到与此查询等效的 mdx:
Select a.shopId , Month(TransactionDate) Month_Transaction,
Year(TransactionDate) Year_Transaction,
count(distinct b.UserID) UniqueUserCount
FROM [dbo].[shop] a
JOIN users b ON a.UserID = b.UserID
where TransactionDate >= '2018-01-01'
Group by a.shopId ,Month(TransactionDate), Year(TransactionDate)
这是我目前所拥有的,无论日期如何,它都会产生唯一的计数。我想要日期范围内的唯一计数。请让我知道如何实现这一点?
SELECT {
[Date].[Month].&[2020]&[2020-Q3]&[2020-09],
[Date].[Month].&[2020]&[2020-Q4]&[2020-10],
[Date].[Month].&[2020]&[2020-Q4]&[2020-11],
[Date].[Month].&[2020]&[2020-Q4]&[2020-12]
} ON COLUMNS, NON EMPTY
{
[ShopLocations].[Hierarchy].[Shop]
} ON ROWS
FROM [ShopperCube]
where (Measures.Users)
您可以使用 DistinctCount MDX 函数。
官方文档不是很清楚,但是,总的原则是:您将“Set”传递给此函数以获取不同的值。
MDX 示例
WITH SET MySet AS
{ [Dim User].[User Id].Children }
MEMBER Measures.SetDistinctCount AS
DISTINCTCOUNT(MySet)
SELECT { Measures.SetDistinctCount, Measures.Amount } ON 0
, { [Dim Date].[Date Key].AllMembers } ON 1
FROM [Mine]
为了验证这一点,以下是我的设置:
查询结果
此外,给定示例 sql 来创建 table 并处理不同的数据:
IF OBJECT_ID('FactTransaction') IS NOT NULL
DROP TABLE FactTransaction
GO
CREATE TABLE FactTransaction (ShopId INT, TransactionDateKey INT, UserId INT, Amount INT)
GO
IF OBJECT_ID('DimDate') IS NOT NULL
DROP TABLE DimDate
GO
CREATE TABLE DimDate(DateKey INT, FullDate DATE)
GO
IF OBJECT_ID('DimUser') IS NOT NULL
DROP TABLE DimUser
GO
CREATE TABLE DimUser(UserId INT, UserName VARCHAR(50))
GO
IF OBJECT_ID('DimShop') IS NOT NULL
DROP TABLE DimShop
GO
CREATE TABLE DimShop(ShopId INT, ShopName VARCHAR(50))
GO
--Shop 1
INSERT INTO FactTransaction values(1, 20210101, 1, 10)
INSERT INTO FactTransaction values(1, 20210101, 2, 5)
INSERT INTO FactTransaction values(1, 20210101, 3, 20)
INSERT INTO FactTransaction values(1, 20210102, 2, 10)
INSERT INTO FactTransaction values(1, 20210102, 4, 15)
INSERT INTO FactTransaction values(1, 20210103, 3, 5)
INSERT INTO FactTransaction values(1, 20210103, 4, 10)
INSERT INTO FactTransaction values(1, 20210103, 5, 20)
INSERT INTO FactTransaction values(1, 20210103, 1, 20)
--Shop 2
INSERT INTO FactTransaction values(2, 20210103, 2, 10)
INSERT INTO FactTransaction values(2, 20210103, 2, 5)
INSERT INTO FactTransaction values(2, 20210103, 2, 20)
GO
INSERT INTO DimDate VALUES(20210101, '2021-01-01')
INSERT INTO DimDate VALUES(20210102, '2021-01-02')
INSERT INTO DimDate VALUES(20210103, '2021-01-03')
GO
INSERT INTO DimUser VALUES(1, 'First')
INSERT INTO DimUser VALUES(2, 'Second')
INSERT INTO DimUser VALUES(3, 'Third')
INSERT INTO DimUser VALUES(4, 'Fourth')
INSERT INTO DimUser VALUES(5, 'Fifth')
GO
INSERT INTO DimShop VALUES(1, 'Shop 1')
INSERT INTO DimShop VALUES(2, 'Shop 2')
GO
内置的非重复计数度量提供了最大的灵活性。听起来你已经有了 Measure.Users? Users 的度量值组是否连接到 Date 和 ShopLocations 的维度?
为了帮助实现这一点,我将回顾多对多革命论文中的 Distinct Count 模式。这种方法提供了一种无代码解决方案,它更灵活并且可能更快 运行: