t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v...

14
CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung Writing MDX Queries on Multi-Dimensional Cube with OLAP CIS 611 Sunnie Chung Writing MDX queries to Retrieve Cube Data for Analysis in MS SQL Management Studio after Deploying a Cube in Visual Studio with SSDT MDX Tutorial Sites: See MDX Section Under DW and OLAP in the Class Lecture Note http://eecs.csuohio.edu/~sschung/cis611/MDXQueryTutorial.pdf https://docs.microsoft.com/en-us/analysis-services/multidimensional-models/mdx/mdx-query-the- basic-query?redirectedfrom=MSDN&view=asallproducts-allversions&viewFallbackFrom=sql-server- ver15 1. Open SQL Server Management Studio and Select Analysis Services as server type. After connecting , you can find the cube Internet Sales has already in SQL server.

Transcript of t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v...

Page 1: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

Writing MDX Queries on Multi-Dimensional Cube with OLAP

CIS 611

Sunnie Chung

Writing MDX queries to Retrieve Cube Data for Analysis in MS SQL Management Studio after Deploying a Cube in Visual Studio with SSDT

MDX Tutorial Sites: See MDX Section Under DW and OLAP in the Class Lecture Note

http://eecs.csuohio.edu/~sschung/cis611/MDXQueryTutorial.pdf

https://docs.microsoft.com/en-us/analysis-services/multidimensional-models/mdx/mdx-query-the-basic-query?redirectedfrom=MSDN&view=asallproducts-allversions&viewFallbackFrom=sql-server-ver15

1. Open SQL Server Management Studio and Select Analysis Services as server type. After connecting , you can find the cube Internet Sales has already in SQL server.

Page 2: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

2. Right click project name and select New Query and then click MDX.

You can find all the dimensions and their attributes in the panes.

Page 3: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

MDX Examples

https://docs.microsoft.com/en-us/analysis-services/multidimensional-models/mdx/mdx-query-and-slicer-axes-specify-the-contents-of-a-query-axis?view=asallproducts-allversions

Specify Select List on Axes:

Q1

SELECT {[Measures].[Internet Sales Amount]} ON COLUMNS,

{[Date].[Calendar].MEMBERS} ON ROWS

FROM [Adventure Works]

Q2 SELECT {[Measures].[Internet Sales Amount]} ON 0, {[Date].[Calendar].MEMBERS} ON 1 FROM [Adventure Works] SELECT {[Measures].[Internet Sales Amount]} ON AXIS(0), {[Date].[Calendar].MEMBERS} ON AXIS(1) FROM [Adventure Works]

NON EMPTY Keyword

Not to return all the null value results

Q3:

SELECT {[Measures].[Internet Sales Amount]} ON COLUMNS,

{[Date].[Calendar].MEMBERS} ON ROWS

FROM [Adventure Works]

Members Keyword:

Return Selected measures for all different values of a chosen Dimension or Measure

To get Every Different Values in a Dimension or Measure Level without specifying each in the Select

Return ALL (total sum) for the Dimension as well together by default Q4 returns sales amount for all Promotion on Column, all Calendar Years on row for Product.SubCategory = 19 Q4: SELECT {[Measures].[Internet Sales Amount]} * [Promotion].[Promotion].[Promotion].MEMBERS ON COLUMNS,

Page 4: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

{[Date].[Calendar].[Calendar Year].MEMBERS} ON ROWS FROM [Adventure Works] WHERE([Product].[Subcategory].&[19])

Adding a WHERE clause with & to Filter

Q5

SELECT {[Measures].[Internet Sales Amount]} ON COLUMNS,

[Date].[Calendar Year].MEMBERS ON ROWS

FROM [Adventure Works]

WHERE([Customer].[Customer Geography].[Country].&[United States])

Q6:

SELECT NON EMPTY {[Measures].[Internet Sales Amount]} * [Promotion].[Promotion].[Promotion].MEMBERS ON COLUMNS, NON EMPTY {[Date].[Calendar].[Calendar Year].MEMBERS} ON ROWS FROM [Adventure Works] WHERE([Product].[Subcategory].&[19]) Children Keyword Return All the values in a Level of Children of a chosen Dimension (Not in Dimension or Measure ) Every Different Values of Children Levels of Currently Selected Member Value Example MDX Query: to Predict the color of each bike depending on the gender.

Q7 select nonempty( [dim customer].[gender].children) on columns, ( nonempty([dim product].[Model Name].children), nonempty([dim product].[color].children) )on rows from [Internet Sales]

Page 5: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

Q8: Predict which cities we need more bikes supply to in next year. Decide top 5 cities that we need put more bike supply. The following statement show the bike sales in different cities each year. Last Year here is year 2013 as an example to show how to do prediction. select ([order date].[calendar year].members) on columns, order([dim geography].[city].members, [measures].[order quantity],desc)on rows from [Internet Sales]

Page 6: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

where ([measures].[order quantity]);

HAVING clause for a Filtering Condition

The HAVING clause can be used to filter the contents of an axis based on a specific criteria; it is less flexible than other methods that can achieve the same results, such as the FILTER function, but it is simpler to use. Here is an example that returns only those dates where Internet Sales Amount is greater than $15,000:

Q8:

SELECT {[Measures].[Internet Sales Amount]} ON COLUMNS,

NON EMPTY

{[Date].[Calendar].[Date].MEMBERS}

HAVING [Measures].[Internet Sales Amount]>15000 ON ROWS

FROM [Adventure Works]

Page 7: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

Filter Clause

Q9: Show the cities in the United States where the total sales amount is more than $1 Million for all products

SELECT Measures.[Reseller Sales Amount] on 0, { Filter([Reseller Geography].City, Measures.[Reseller Sales Amount] > 1000000) } ON 1 FROM [Adventure Works DW] WHERE [Reseller].Country.[United States]

Top Count Function:

Q10_1 : Find out how many bikes have been sold in particular city in 2013. The following statement finds Top 20 cities which has the best order quantity. select[measures].[order quantity] on 0, TopCount ({[dim geography].[city].members *[order date].[calendar year].[2013]} ,20 ,[measures].[order quantity] ) ON 1 from [Internet Sales]

Page 8: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

Q10_2: The same way to find out top 20 cities in SALES AMOUNT in 2013

select[measures].[sales amount] on 0, TopCount ({[dim geography].[city].members *[order date].[calendar year].[2013]} ,20 ,[measures].[sales amount] ) ON 1 from [Internet Sales]

Page 9: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

Slicer Axis Syntax

The following query does not include a WHERE clause, and returns the value of the Internet Sales Amount measure for all Calendar Years: Q11:

SELECT {[Measures].[Internet Sales Amount]} ON COLUMNS,

[Date].[Calendar Year].MEMBERS ON ROWS

FROM [Adventure Works]

Q12: Drill down onto each Quarter from Year

Using following statement to see the purchase of bikes in each quarter in year 2013.

select ([order date].[calendar quarter].members)on columns, filter([Dim geography].[city].members, [order date].[calendar year].[2013]) on rows from[internet sales] where([measures].[sales amount])

Save the table into excel and sorted in descending order of their total sales amount of year 2013

Page 10: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

I select top 10 sale amounts cities and plot a quarter graph. We can see during quarter 4th the sales of bikes usually get the peak value. We also can notice that some cities (e.g. Goulburn) have downward tendency.

0

50000

100000

150000

200000

250000

300000

quarter1 quarter2 quarter3 quarter4

London

Paris

Wollongong

Warrnambool

Bendigo

Goulburn

Bellflower

Brisbane

Townsville

Geelong

Page 11: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

Q12. Drill down onto each Month from Year

Using following statement to see the purchase of bikes in each quarter in year 2013.

select ([order date].[month number of year].members)on columns,

filter([Dim geography].[city].members, [order date].[calendar year].[2013]) on rows from[internet sales] where([measures].[sales amount])

Save the table into excel and sorted in descending order of their total sales amount of year 2013.

Page 12: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

I select top 10 sale amounts cities and plot a month graph. We can see in Dec the sales of bikes usually get the peak value. January and December usually have the best sales which maybe because of holiday season sales. Another good sale month is June which maybe due to nice weather for outdoor.

Cross JOIN

Q13 Predict the color of each bike depending on gender

SELECT Measures.[Order Quantity] on 0, non empty{ Crossjoin(Product.Subcategory.Children, Customer.Gender.Children, Product.Color.Children)} on 1 FROM [Adventure Works DW] WHERE Product.Category.Bikes

0

20000

40000

60000

80000

100000

120000

1 2 3 4 5 6 7 8 9 10 11 12

London

Paris

Wollongong

Warrnambool

Bendigo

Goulburn

Bellflower

Brisbane

Townsville

Geelong

Page 13: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

DrilldownMember

Q14 Drill down in year 2013 to see the purchase of bikes in each quarter and each month, respectively, in different cities. Then, find any unusual sales trend we should know about to prepare for next year.

SELECT Non Empty Order ([Reseller Geography].City, Measures.[Reseller Order Quantity], BDESC )on 1, ( Measures.[Reseller Order Quantity], DrilldownMember ( [Date].[Calendar].[Calendar Quarter].Members, { [Date].[Calendar].[Calendar Quarter], [Date].[Calendar].[Month] } ) ) on 0 FROM [Adventure Works DW] WHERE ( [Date].Year.&[2013], [Product].Category.Bikes )

Page 14: t ] ] v P D y Y µ ] } v D µ o ] r ] u v ] } v o µ Á ] Z K> W /^ ò í í ^ µ v v ...cis.csuohio.edu/~sschung/cis611/CIS611_LectureNotes_MDX... · 2020. 7. 27. · /^ ò í í

CIS 611 Enterprise Database Systems and Data Warehouse Sunnie S. Chung

Cross Join SELECT Measures.[Order Quantity] on 0, non empty{ Crossjoin(Product.Subcategory.Children, Customer.Gender.Children, Product.Color.Children)} on 1 FROM [Adventure Works DW] WHERE Product.Category.Bikes