官术网_书友最值得收藏!

Understanding the GROUP BY clause

Aggregate data is what reports are made of; speaking ANSI SQL, this means GROUP BY. MicroStrategy applies GROUP BY to all the attributes present on a report grid. Well, this is not completely correct, but we begin with this simple concept:

Every attribute on a grid is reflected in the GROUP BY clause of the resulting SELECT statement.

Hence, we have:

  • Metrics that represent the aggregation (sum, count, and so on) of facts
  • Attributes BY which we GROUP the data

I hope this is clear enough, because this concepts will return every now and then with metrics. In this recipe, anyway, we'll see an example of how it works.

Getting ready

We are using now the SalesAmount metric and the Product attribute. Before going on we need to establish a connection between the fact table and the attribute table, so that MicroStrategy knows how to JOIN them.

The connection is represented by the FK (ProductKey) in FactInternetSales. In other words, the ID form of Product is present in both the fact and the dimension tables, so we need to specify that those two columns are the same ID in order to JOIN them in the SQL statements.

How to do it...

First, we modify the Product attribute then we create a new report:

  1. Go to Schema Objects | Attributes and right-click on Product. From the context menu select Edit.
  2. You're in the Attribute Editor and the ID form is already selected, click on the Modify button.
  3. In the modify Attribute Forms dialog, on the right there is a panel with Source tables where we have selected DimProduct and set it as lookup. Now click to select FactInternetSales too, like in the following screen capture, and click on OK:
  4. Back in the Attribute Editor, click on Save and Close.
  5. Now go to the folder Schema Objects | Tables and right-click on FactInternetSales, selecting Edit from the context menu.
  6. You're in the Table Editor: see in the Logical View tab, that the Product attribute is present together with the two facts we created previously.
  7. Below the list of objects there is a checkbox that says The key specified is the true key for the warehouse table; be sure to uncheck this because ProductKey is not the PK in this table.
  8. Click on Save and Close, and update the schema.
  9. Now go to My Personal Objects | My Reports folder, right-click on the right pane and select New | Report from the context menu.
  10. Leave default Blank Report selected and click on OK.
  11. Here we are in the Report Editor. First, click on the Attributes icon in My Shortcut panel then double-click on the Product icon in the list that appears: note that the Product attribute is added to the grid.
  12. Now click on the Public Objects icon in My Shortcut panel and in the list that appears double-click on Metrics to see the list of metrics that we have in the project
  13. Double-click on the metric named Sum SalesAmount from FactInternetSales, the metric is added to the grid.
  14. From the View menu select Grid View, and the report shows the results.
  15. You see the DESC and LDESC forms of the Product attribute and if you scroll to the right there is the column of Sum SalesAmount from FactInternetSales at the Product level.
  16. We do not need the LDESC form in this report so we remove it. Scroll to the beginning of the grid until you see the header of the Product attribute. Right-click on the cell containing the Product header and from the context menu select Attribute Forms.
  17. You see that there are three Attribute Forms here: ID, DESC, and LDESC. Uncheck LDESC and leave only DESC checked.
  18. The LDESC form has disappeared and we have a SalesAmount report grouped by Product.
  19. Now select the menu View | SQL View to see SQL sentence. There is a GROUP BY clause with the Product PK, the fact SalesAmount is aggregated with the Sum() function and grouped by Product.
  20. Go back to Grid View with View | Grid View and click on Save and Close.
  21. Name the report 06 SalesAmount by Product.

How it works...

In this case we put a single attribute and a single metric on the report. The metric is calculated at the level of the single attribute: sum(SalesAmount) from FactInternetSales group by ProductKey. Easy to guess, if we put more than one attribute on the grid the GROUP BY clause would contain more columns.

There's more...

Refer to Chapter 3, Schema Objects – Attributes, for details about how attribute forms are displayed in reports.

Note

You can watch a screencast of this operation at:

Exercise 15

Following the steps in this recipe create a new report with SalesAmount by customer.

Remember to modify the Customer attribute in order to include the CustomerKey column from the FactInternetSales table (and update the schema). The first few rows of the report should look like this image:

Save the report as 07 SalesAmount By Customer.

主站蜘蛛池模板: 余庆县| 河源市| 恩施市| 申扎县| 双鸭山市| 溧阳市| 徐闻县| 万安县| 南靖县| 乾安县| 卢龙县| 玛纳斯县| 岱山县| 衡阳县| 城固县| 丰顺县| 利辛县| 定南县| 井研县| 交口县| 梅河口市| 连城县| 安吉县| 大城县| 武安市| 二连浩特市| 富民县| 威信县| 上栗县| 仪征市| 读书| 城市| 桐柏县| 喜德县| 孝昌县| 宿州市| 翼城县| 吉林省| 莎车县| 罗田县| 曲水县|