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

时间:2015-06-12 07:38:56

标签: ssas mdx

以下查询计算员工明智的工作百分比。即行上的员工和列上的工作百分比

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查询以获得所需的输出。

注意:截至目前,我在行上不需要员工姓名。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

我想知道Sourav的脚本如何影响结果: 它们仍然有效吗? 2.以下运行速度更快吗?

它正在使用Sub sbDelete_rows_if_cell_blank() Dim lRow As Long Dim iCntr As Long Dim ws as Worksheet For each ws in ThisWorkbook.Worksheets ' Find last row in column A lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row For iCntr = lRow To 1 Step -1 If ws.name<>"Sheet1" and ws.name <> "Sheet2" then ' change this line to the sheet names you want to leave out. If IsEmpty(ws.Cells(iCntr, 1)) Or Trim(ws.Cells(iCntr, 1).Value) = "" Then ws.Rows(iCntr).Delete End If end if Next iCntr Next ws End Sub 构造的扭曲。这是由MoshaP在这里发表的博客: http://sqlblog.com/blogs/mosha/archive/2007/11/22/optimizing-count-filter-expressions-in-mdx.aspx

if