Run all three queries at once and verify that they return the same results

Một phần của tài liệu Pro SQL server 2012 BI solutions (Trang 577 - 582)

Tip: Remember that the word GO is case sensitive! You will receive an error when attempting to run all three queries at once if GO is not all caps.

in this exercise, you wrote and executed three MDX queries. now let’s work on expanding your knowledge of the MDX language.

Using the Axis 0 and 1 Instead of Column and Row

in MDX queries; also written as (0) and (1). Columns are position 0, and rows are position 1. Although some developers prefer to use words, many prefer to write the MDX code using the numeric identifier instead.

Listing 14-10 shows the three different styles used to identify the positions.

Listing 14-10. Identifying Axis Positions Within Queries Select

{ [Measures].[SalesQuantity] } On Columns,

{ [DimTitles].[TitlesByType].[TitleType] } On Rows From [CubePubsSales];

-- Same as. . . Select

{ [Measures].[SalesQuantity] } On Axis(0), -- Using Axis Key { [DimTitles].[TitlesByType].[TitleType] } On Axis(1) From [CubePubsSales];

-- Note that the Axis collection uses Parentheses (like VB) -- and not Brackets (like C#)

-- same as before, but does not use the Axis keyword Select

{ [Measures].[SalesQuantity] } On 0, -- Using Axis Key { [DimTitles].[TitlesByType].[TitleType] } On 1 From [CubePubsSales];

It is worth noting that you cannot skip a position in your MDX code. While some client applications allow you to display the results across columns and across rows, the MDX code must specify 0 before 1. Position 0 cannot be skipped. Listing 14-11 shows an example of this.

Listing 14-11. Positions Cannot be Skipped in Columns and Rows -- This WILL NOT work, because we are not including the 0 axis Select

{ [DimTitles].[TitlesByType].[TitleType] } On 1 From [CubePubsSales];

--ERROR = "Axis numbers specified in a query must be sequentially specified, and cannot contain gaps."

-- Must be written as . . . Select

{ [DimTitles].[TitlesByType].[TitleType] } On 0 From [CubePubsSales];

Cells and Tuples

Microsoft’s help pages define a tuple with the following statement: “A tuple uniquely identifies a slice of data

result. Nevertheless, the two are quite different. You can write an MDX query that returns a tuple from a single cell, but you can also return a tuple that is the aggregate value of many cells.

Listing 14-12 shows three queries. The top two queries return one tuple, but the first uses data from a single cell while the second uses an aggregate from multiple cells. The last query returns two results (tuples) by aggregating data from multiple cells. Figure 14-8 displays the result of the last query.

Listing 14-12. Queries That Return One or More Tuples -- This query returns one tuple from a single cell Select

{

[Measures].[SalesQuantity]

}

On Columns,

{(−− Parentheses are required since we specify a SET of Coordinates [DimAuthors].[Author].[Cheryl Carson]

,[DimStores].[Store].[Bookbeat]

,[DimTitles].[Title].[But Is It User Friendly?]

,[DimDates].[Year-Qtr-Month-Day].[Date].[Saturday, May 22 1993 12:00 AM]

)}

On Rows

From [CubePubsSales];

-- This query also returns one tuple, but is an aggregate of many cells Select

{

[Measures].[SalesQuantity]

}

On Columns,

{( −- Parentheses are NOT required since it no longer specifies a SET of Coordinates [DimStores].[Store].[Bookbeat]

)}

On Rows

From [CubePubsSales];

-- This query returns two tuples, each is an aggregate of many cells Select

{

[Measures].[SalesQuantity]

}

On Columns,

{−− Braces are required when more than one tuple is returned ([DimStores].[Store].[Bookbeat]) -- Returns one tuple

-- Remember each item in parentheses implicitly includes the other dimensions ,([DimStores].[Store].[News & Brews]) -- Returns another tuple

} On Rows

From [CubePubsSales];

Be careful not to identify the multiple members from the same dimension as coordinates for a tuple. When you do so, you will receive an error. In Listing 14-13 you see that we are using two members, [Business] and [Psychology], from the same [DimTitles].[TitleType] hierarchy. Running this query gives the error message

“The ‘TitleType’ hierarchy appears more than once in the tuple.”

Listing 14-13. Improper Coordinates for a Single Tuple -- This query will not work!

Select {

[Measures].[SalesQuantity]

, [Measures].[NumberOfSales]

} On Columns,

{ ( −- This parenthesis indicates you are defining multiple coordinates -- for a SINGLE tuple (a.k.a. a result value )

[DimTitles].[TitleType].&[Business]

-- A second coordinate won’t work if it is from the same dimension!

, [DimTitles].[TitleType].&[Psychology]

) } On Rows

From [CubePubsSales];

//Error = The 'TitleType' hierarchy appears more than once in the tuple.

-- This works though!

Select {

[Measures].[SalesQuantity] -- 1st tuple Figure 14-8. A query that returns two tuples

{ −- Do NOT include parentheses when

-- you want TWO tuples (a.k.a. result values) from the SAME dimension-heirarchy [DimTitles].[TitleType].&[Business] -- 3rd tuple

, [DimTitles].[TitleType].&[Psychology] -- 4th tuple } On Rows

From [CubePubsSales];

Figure 14-9 shows the results of the working query.

Figure 14-9. The results from Listing 14-13

The same is true when working with the Measures dimension. If you use parentheses to encompass two multiple measures, as demonstrated in Listing 14-14, SSAS assumes you are trying to indicate two coordinates on the Measures dimension. Doing so gives you the following error: “The ‘Measures’ hierarchy appears more than once in the tuple.”

Listing 14-14. Using Parentheses to Encompass Two Multiple Measures Select

{ ( −- This will not work!

[Measures].[SalesQuantity]

, [Measures].[NumberOfSales]

) } On Columns From [CubePubsSales];

//Error = The 'Measures' hierarchy appears more than once in the tuple.

This aspect of MDX does not come into play if you are asking for a single tuple based on the coordinates from two different dimensions. Listing 14-15 shows an example that returns a single tuple across the rows axis by identifying coordinates from both the DimTitles and DimStores dimensions. Figure 14-10 shows the results.

Listing 14-15. Aggregating Store and Title Data into a Single Tuple Select

{

[Measures].[SalesQuantity] -- 1st tuple , [Measures].[NumberOfSales] -- 2nd tuple } On Columns,

{ ( −- This parenthesis begins the coordinates of a 3rd tuple!

[DimTitles].[TitleType].&[Business] -- From the Titles dimension , [DimStores].[Store].[BookBeat] -- From the Stores dimension ) } On Rows

From [CubePubsSales];

Một phần của tài liệu Pro SQL server 2012 BI solutions (Trang 577 - 582)

Tải bản đầy đủ (PDF)

(823 trang)