如何按范围对记录进行分组并将其放在行中

How to group record by range and put it on rows

下面的查询计算员工明智的工作百分比。即员工在行上,工作百分比在列上

With Member [Measures].[EmployeeWisePercent] AS
[Measures].[Hours] / ([Employee].[Employee].[All], [Measures].[Hours]) * 100

Member [Measures].[TotalHours] AS
([Employee].[Employee].[All], [Measures].[Hours])

 SELECT NON EMPTY 
 { [Measures].[Hours], [Measures].[EmployeeWisePercent], [Measures].[TotalHours] } ON COLUMNS,

  NON EMPTY 
  { ([Employee].[Employee].[Employee].ALLMEMBERS ) } 
  DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM
    ( SELECT ( STRTOSET("[Date].[Calendar].[All]", CONSTRAINED) ) ON COLUMNS FROM 
    ( SELECT ( STRTOSET("[Project].[Project Name].&[Self Study]", CONSTRAINED) ) ON COLUMNS FROM 
    [TimeSheetHours Cube])) 

  WHERE ( IIF( STRTOSET("[Project].[Project Name].&[Self Study]", CONSTRAINED).Count = 1,
   STRTOSET("[Project].[Project Name].&[Self Study]", CONSTRAINED), 
   [Project].[Project Name].currentmember ), 
   IIF( STRTOSET("[Date].[Calendar].[All]", CONSTRAINED).Count = 1, 
   STRTOSET("[Date].[Calendar].[All]", CONSTRAINED), [Date].[Calendar].currentmember ) )
   CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS

输出如下

Employee Name  |    Hours   |  Percent   |   Total Hours
Employee1      |   975.86   |   1.32     |    73421.22
Employee2      |   (null)   |  (null)    |    73421.22
Employee3      |   (null)   |  (null)    |    73421.22
Employee4      |   (null)   |  (null)    |    73421.22
Employee5      |   (null)   |  (null)    |    73421.22
Employee6      |   (null)   |  (null)    |    73421.22
...
...
...

我需要更新上面的查询以获取员工人数并将其分组(0-20%、20-40%、...等)行和列中该范围内的员工人数.如下所示:

Employee Name  |  EmployeeCount
0-20%          |    5    
20-40%         |    8
40-60%         |    88
60-80%         |    2
80-100%        |    1

有没有什么方法可以将百分比计数分组到上面指定的范围内? 请帮助我准备 mdx 查询以获得所需的输出。

注意:我现在不需要在行中显示员工姓名。

据我了解,您的要求基本上是创建两个计算量度(员工计数和百分比括号)并显示一个量度与另一个量度的汇总结果。这基本上转化为类似(下面的伪代码) -

SELECT distinct values of the measure [percentage bracket] on rows,

Corresponds counts on columns

from [YourCube]

虽然这在有点复杂的情况下是可能的SQL,但在 MDX 中是不可能的。 主要原因是你这里要的是两步计算。第一步将计算员工将落入的等级,在下一步中,使用此结果获取每个等级的计数。这似乎可以使用计算成员来实现,但您必须记住度量值不是持久的。在没有范围的情况下,度量会在整个层次结构中聚合。除非 MDX 是 运行.

,否则无法推断出度量可以包含的 唯一

在 SQL 中,这可以使用 subqueryjoin 来实现,因为可以在聚合值处进行连接(并且它们被视为持久性),但该功能不会存在于 MDX 中。

可能的是有 5 个单独的计算成员,它们将保存每个括号的计数。由于这将是一步计算,因此在 MDX 的能力范围内。

这将是以下几行之一 -

With Member [Measures].[EmployeeWisePercent] AS
[Measures].[Hours] / ([Employee].[Employee].[All], [Measures].[Hours]) * 100

Member [Measures].[TotalHours] AS
([Employee].[Employee].[All], [Measures].[Hours])

MEMBER MEASURES.[0-20%] AS
COUNT(FILTER(EXISTING [Employee].[Employee].CHILDREN, [Measures].EmployeeWisePercent >=0 AND [Measures].EmployeeWisePercent<20))

MEMBER [20-40%] AS
COUNT(FILTER(EXISTING [Employee].[Employee].CHILDREN, [Measures].EmployeeWisePercent >=20 AND [Measures].EmployeeWisePercent<40))

MEMBER [40-60%] AS
COUNT(FILTER(EXISTING [Employee].[Employee].CHILDREN, [Measures].EmployeeWisePercent >=40 AND [Measures].EmployeeWisePercent<60))

MEMBER [60-80%] AS
COUNT(FILTER(EXISTING [Employee].[Employee].CHILDREN, [Measures].EmployeeWisePercent >=60 AND [Measures].EmployeeWisePercent<80))

MEMBER [80-100%] AS
COUNT(FILTER(EXISTING [Employee].[Employee].CHILDREN, [Measures].EmployeeWisePercent >=80 AND [Measures].EmployeeWisePercent<=100))

 SELECT NON EMPTY 
 { MEASURES.[0-20%],  MEASURES.[20-40%],  MEASURES.[40-60%],  MEASURES.[60-80%],  MEASURES.[80-100%] } ON COLUMNS

 FROM
    ( SELECT ( STRTOSET("[Date].[Calendar].[All]", CONSTRAINED) ) ON COLUMNS FROM 
    ( SELECT ( STRTOSET("[Project].[Project Name].&[Self Study]", CONSTRAINED) ) ON COLUMNS FROM 
    [TimeSheetHours Cube])) 

  WHERE ( IIF( STRTOSET("[Project].[Project Name].&[Self Study]", CONSTRAINED).Count = 1,
   STRTOSET("[Project].[Project Name].&[Self Study]", CONSTRAINED), 
   [Project].[Project Name].currentmember ), 
   IIF( STRTOSET("[Date].[Calendar].[All]", CONSTRAINED).Count = 1, 
   STRTOSET("[Date].[Calendar].[All]", CONSTRAINED), [Date].[Calendar].currentmember ) )
   CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS

我想知道 Sourav 剧本的这个星期对结果有何影响: 1. 它们仍然有效吗? 2. 下面的 运行 会更快吗?

它使用了 COUNT(FILTER 结构的变体。这是由 MoshaP 在这里发布的博客: http://sqlblog.com/blogs/mosha/archive/2007/11/22/optimizing-count-filter-expressions-in-mdx.aspx

With Member [Measures].[EmployeeWisePercent] AS
[Measures].[Hours] / ([Employee].[Employee].[All], [Measures].[Hours]) * 100

Member [Measures].[TotalHours] AS
([Employee].[Employee].[All], [Measures].[Hours])

MEMBER [Measures].[0-20%] AS 
 SUM(
   EXISTING [Employee].[Employee].CHILDREN, 
   IIF(
     [Measures].[EmployeeWisePercent]>=0 
     AND 
     [Measures].[EmployeeWisePercent]<20
    ,1
    ,NULL)
 )

MEMBER [Measures].[20-40%] AS 
 SUM(
   EXISTING [Employee].[Employee].CHILDREN, 
   IIF(
     [Measures].[EmployeeWisePercent]>=20 
     AND 
     [Measures].[EmployeeWisePercent]<40
    ,1
    ,NULL)
 )

MEMBER [Measures].[40-60%] AS 
 SUM(
   EXISTING [Employee].[Employee].CHILDREN, 
   IIF(
     [Measures].[EmployeeWisePercent]>=40 
     AND 
     [Measures].[EmployeeWisePercent]<60
    ,1
    ,NULL)
 )

MEMBER [Measures].[60-80%] AS 
 SUM(
   EXISTING [Employee].[Employee].CHILDREN, 
   IIF(
     [Measures].[EmployeeWisePercent]>=60 
     AND 
     [Measures].[EmployeeWisePercent]<80
    ,1
    ,NULL)
 )

MEMBER [Measures].[80-100%] AS 
 SUM(
   EXISTING [Employee].[Employee].CHILDREN, 
   IIF(
     [Measures].[EmployeeWisePercent]>=80 
     AND 
     [Measures].[EmployeeWisePercent]<=100
    ,1
    ,NULL)
 )

 SELECT 
 NON EMPTY 
 { 
    MEASURES.[0-20%]
 ,  MEASURES.[20-40%]
 ,  MEASURES.[40-60%]
 ,  MEASURES.[60-80%]
 ,  MEASURES.[80-100%] 
 } ON COLUMNS

 FROM
    ( 
     SELECT ( STRTOSET("[Date].[Calendar].[All]", CONSTRAINED) ) ON COLUMNS 
     FROM 
        ( 
          SELECT 
             ( STRTOSET("[Project].[Project Name].&[Self Study]", CONSTRAINED) ) ON COLUMNS 
          FROM [TimeSheetHours Cube]
        )
     ) 

  WHERE 
     ( IIF( STRTOSET("[Project].[Project Name].&[Self Study]", CONSTRAINED).Count = 1,
   STRTOSET("[Project].[Project Name].&[Self Study]", CONSTRAINED), 
   [Project].[Project Name].currentmember ), 
   IIF( STRTOSET("[Date].[Calendar].[All]", CONSTRAINED).Count = 1, 
   STRTOSET("[Date].[Calendar].[All]", CONSTRAINED), [Date].[Calendar].currentmember ) )
   CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS