使用純量運算式
在多維度運算式 (MDX) 中,純量運算式是 MDX 語法的元素,在評估時,會在評估內容中傳回單一值。
純量運算式包含 MDX 中的字串、數值和日期運算式。
純量運算式通常用於匯出成員定義中,因為匯出成員必須傳回純量值。 下列查詢顯示使用不同純量運算式類型之量值維度上匯出成員的範例:
WITH
MEMBER MEASURES.NumericValue AS 10
MEMBER MEASURES.NumericExpression AS 10 + 10
MEMBER MEASURES.NumericExpressionBasedOnMeasure AS [Measures].[Internet Sales Amount] + 10
MEMBER MEASURES.StringValue AS "10"
MEMBER MEASURES.ConcatenatedString AS "10" + "10"
MEMBER MEASURES.StringFunction AS MEASURES.CURRENTMEMBER.NAME
MEMBER MEASURES.TodaysDate AS NOW()
SELECT
{MEASURES.NumericValue,MEASURES.NumericExpression,MEASURES.NumericExpressionBasedOnMeasure,
MEASURES.StringValue, MEASURES.ConcatenatedString, MEASURES.StringFunction, MEASURES.TodaysDate}
ON COLUMNS
FROM [Adventure Works]
量值計算或其他唯一可以傳回的資料類型是 OLE Variant 類型。 因此,有時候您可能需要將量值轉換成特定類型,以接收您預期的行為。 下列查詢顯示下列範例:
WITH
//Two calculated measures that return strings
MEMBER MEASURES.NumericString1 AS "10"
MEMBER MEASURES.NumericString2 AS "10"
//In this case, the + operator acts to concatenate the strings
MEMBER MEASURES.Concatenation AS MEASURES.NumericString1 + MEASURES.NumericString2
//Casting one value to an integer with the CINT function causes the second measure
//to be treated as an integer too, so that the + operator now acts to add the values
MEMBER MEASURES.Addition AS CINT(MEASURES.NumericString1) + MEASURES.NumericString2
SELECT
{MEASURES.NumericString1,MEASURES.NumericString2,MEASURES.Concatenation,MEASURES.Addition }
ON COLUMNS
FROM [Adventure Works]