简体   繁体   English

仅在先前的度量成员满足特定条件时显示行

[英]Show rows only if previous measure member meets certain condition

Consider certain query in MDX 考虑MDX中的某些查询

SELECT NON EMPTY {[Measures].[Income]} ON COLUMNS,
{
            SellDate.[year].Members
} ON ROWS
FROM [HD Kawiarnia]

that shows income of a company during each year. 显示了公司每年的收入。 The question is how to show only this years in which income was higher then it was in the previous year? 问题是如何仅显示今年收入高于上一年的收入?

WITH MEMBER [Measures].[MyMeasure] as 
IIF(
([Measures].[Income], [SellDate].[Year].currentmember) 
     <= ([Measures].[Income], [SellDate].[Year].CURRENTMEMBER.PREVMEMBER) 
, NULL, 
[Measures].[Income])

SELECT NON EMPTY {[Measures].[Income]} on COLUMNS
NON EMPTY {[SellDate].[Year].CHILDREN} on ROWS
FROM 
[HD Kawiarnia]

I used the IIF function to to evaluate the clause and return null if the previous year income is higher. 我使用了IIF函数来评估该子句,如果前一年的收入较高,则返回null。 I got the previous year by using the PREVMEMBER function. 我通过使用PREVMEMBER函数获得了上一年的知识 I also replaced your [SellDate].[Year].MEMBERS with [SellDate].[Year].CHILDREN. 我还用[SellDate]。[Year] .CHILDREN替换了您的[SellDate]。[Year] .MEMBERS。 This removes the ALL member, which you probably didn't want to be returned. 这将删除ALL成员,您可能不想将其返回。

Word of caution: although Iif is the simplest way to understand how to do this, it traditionally is a bit slow (although performance has improved with 2008 and 2012 versions of SQL Server). 警告:尽管Iif是了解如何执行此操作的最简单方法,但从传统上讲它有点慢(尽管SQL Server 2008和2012版本的性能有所提高)。 This one probably won't be too bad, but in general you can replace iif functions with scripting using scope to get better performance. 这一功能可能还不错,但是总的来说,您可以使用作用域的脚本替换iif函数,以获得更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL查询仅在项目的最后一个条目满足条件时才选择行 - Sql query to only select rows if last entry of item meets condition T-SQL:仅获取满足某些条件的行之后的行 - T-SQL: get only rows which are after a row that meets some condition 如何按特定条件检索上一行和下一行 - How can I retrieve previous and next rows by certain condition 在符合条件的行之后选择三行 - Selecting three rows after the one that meets the condition 仅在先前记录具有特定值时显示记录 - Show records only if a previous record has a certain value SQL Lead/Lag 直到某个条件,并使用满足条件的行中的值更新上一行/下一行 - SQL Lead/Lag until a certain condition and update the previous/next rows with a value from the row that hits the condition 如果满足特定条件,则从 select 语句中排除行 - Exclude row from select statement if it meets certain condition 如果日期满足条件,则在日期中添加一定天数 - Add a certain amount of days to a date if the date meets a condition 如何识别表中的哪些行满足特定条件,但条件是基于先前行中的数据? 提供的示例 - How can I identify which rows in a table have met a certain condition, but the condition is based on data in previous rows? Example provided 满足我条件的行的datediff每行仅一次 - datediff for row that meets my condition only once per row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM