Monday, October 7, 2019

SQL and Scope

One of the first successful programming languages was a relatively simple one named FORTRAN. This was short for “FORmula TRANSlator,” and it attempted to look as much like algebra as it could. The first version of this language had only two datatypes, integers and floating-point. You declared a variable by using it in the program; if the variable name began with the letters “I” thru “N”, then it was an integer, and otherwise it was a floating-point. Before you ask, yes, a typo could create a completely new variable on the fly. But don’t worry! There wasn’t much chance for confusion because variable names could only be six uppercase letters. Yes, I know it seems primitive, but please remember that our principle input device was a punch card while a pocket calendar today has more storage than all the computers that existed on earth back then.

It wasn’t until Algol 60 was invented that we saw the first modern block-structured programming language with something you would recognize as modern scoping rules. Serious programming language people often refer to Algol as being unique because it was better than most of the languages that came before and after it. This was the language that introduced begin-end blocks in programming and the concept of scoping.

Variables come into existence at the front of a begin-end block, persist during the execution of the code in that block, and then are deallocated when control exits that block. Most of you are probably used to using programming languages that follow this model. The syntactic sugar may change from keywords to curly brackets, but the storage model is still the same. There was a design flaw in the original Algol because we had not learned to think in scopes yet. While we did have if-then, if-then-else, begin-end and for-loop constructs, it wasn’t all we had! The language included a GOTO <label> statement that could change the flow of control to any spot in the program. This is what FORTRAN and all the other languages up until that time had. This was not a big problem when you had one and only one scope.

When the code jumps to the middle of a block, and exposes variables with the same name but a different scope, which one has priority? Perhaps a better question is that since we did not enter through the top of the block, does a local variable even exists since it was never allocated in the proper order? If they do exist, then what are their initial values? What are their values when you return to that block again from outside?

Later procedural programming languages got around these problems with the simple solution of getting rid of the GOTO statement. Every programmer should be required to read the classic article from the Communications of the ACM by Dykstra entitled The Go To Considered Harmful. This is what started the structured programming revolution in the 1970s.

The source of the term “structured” in structured programming was the ability to nest blocks inside each other. The goal is that each block would have one entry point and one exit point, and, ideally, it would perform one simple well-defined task. This allowed formal proofs of program correctness, optimization and other things as well as being a heck of a lot easier to maintain. The idea of scoping was carried from the Procedural Language Family into the declarative languages, like SQL. While we don’t have flow control, the structures in our language are also tiered or nested structures.

In SQL, you can nest queries inside each other via joins, set operations and other table level operations. This is actually a mathematical property called Orthogonality or Closure. In English, it is why operations done on numbers produce numbers, and you can add parentheses to mathematical expressions. Any place in which you can use a number, you can use a numeric expression. Anywhere in which you can use a table, you can use a table expression.

In SQL there is a hierarchy of schema objects in which the outermost level is the schema or database. The schema is made up of tables (which can be base or virtual tables) and other schema objects. Each of those tables is made up of a set of rows. These rows have no order, but all have the same structure, so it is a proper set. Each row is located by a key.

Each row is made up of columns, which can be real or virtual. The columns are scalar values drawn from a domain; a domain is set of values of one data type that has rules of its own. These rules include the type of scale upon which it is built and reasonable operations with it. Columns are located by their name, as qualified by the table in which they occur.

Transactions

Another vital concept is that of a unit of work, or a transaction. This means that when you do something in SQL, it either completes or it fails as a whole. But more than that, everything happens “all at once” in the transaction. The classic example of this is a simple update statement:

UPDATE Foobar SET x = y, y = x;

Please notice that this is a table-level transaction. It works on one and only one table. The columns X and Y will swap values because both assignments are made in parallel. Compare this to most procedural languages, which would execute the assignments left to right, record by record. The column X will get the value in column Y from the first assignment, and then column Y would get the same value put right back into it. This same pattern holds for computations in a SELECT list and everywhere else in the language. This is why when you update the table with an expression that has a timestamp in it, the timestamp has one initial value and is not incremented no matter how long it takes to physically write the rows to the schema.

You do not log in to a single table; you log on to the whole database and then use the DCL (Data Control Language) to find out which privileges you have on the constructs you can use. Once you have access to a table, the table is organized in rows, and then the rows are organized in columns. People often make the mistake of thinking that a table is “rows and columns” like a spreadsheet, but they are wrong. This concept is important because some statements work at the table level, some at the row level and some at the column level. When an operation is over, the table still has to be a table, and not full of assorted holes, mixed data types in a column, and so forth. For example, I can DROP TABLE <Tablename>; and get rid of the whole table. I can delete an entire row based on a predicate, DELETE FROM <Tablename> WHERE <search condition>;. But I cannot drop a few columns from a few rows in some odd tables. Likewise, I can’t just willy-nilly insert any old row in any old table.

Derived Tables

A derived table is a table expression embedded in a containing statement. It has to be placed inside parentheses, and it can optionally be given a correlation name and its columns can also optionally given names. The syntax is

(<table expression>) [[AS] <correlation name> [(<derived column list>)]]

If there is no (<derived column list>), then the derived table exposes the tables and their columns that created the derived table. Please notice that a simple table alias is a special case of a derived table. For some reason we don’t seem to think of an alias this way, but it is. Perhaps it’s because no SQL engine would actually be crazy enough to materialize an alias instead of using some kind of pointer structure.

The derived table will act as if it is materialized during the duration of the statement that uses it. Notice the phrase “act as if” in that last sentence. This phrase and the adjective “effectively” occur all over the ANSI/ISO SQL standards. To play safe, we also defined the execution of a query as being left to right, in the order written. This is important when we get to using infixed join operators, and in particular when we use the [LEFT | RIGHT | FULL] OUTER JOIN operators.

Set Operations

Likewise, the UNION [ALL], INTERSECT [ALL] and EXCEPT [ALL] operators are executed from left to right without precedence rules (this is not the way traditional set theory works). On top of these three traditional set operations, we also have OUTER UNION, DISTINCT and other options. Let me go ahead and say this: if you’ve done a good job of designing your schema, you probably will not find any use for “the fancy stuff” in your queries. In fact, the RIGHT OUTER JOIN is a rarity because we’re used to reading from left to right in programming languages based on the Latin alphabet.

The rules for the traditional set operations are that the tables involved must have the same structure. This is called “union compatible,” and it makes sense. The official definition of SQL will cast columns from different tables into the highest data type that is union compatible, but there is no obligation to keep the rows in the results in any particular order and nor do any of the columns in the result set have names unless you specifically give names to them.

In the very early days, various SQL products resolved the column names differently. Some took the names from the first table in the list of UNION-ed tables (SQL Server), some took the names in the last table, and some generated meaningless, long, ugly names.

(SELECT a ,b, c, .. FROM X1
UNION
SELECT x, y, z, .. FROM X2
UNION
..
SELECT r, s, t FROM Xn)
AS Foo (p1, p2, p3,..);

The optimizer is free to re-arrange the clauses of a statement anyway that it wishes so long as the results are the same as the original statement. In fact, a really good optimizer can switch strategies during the execution of a single query when it runs into different parts of the data. The optimizer is where we get to show off how smart we are!

Once the query expression becomes a derived table with a correlation name, the internal structure is hidden. If no (<derived column list>) is given, then it inherits the column names from the component tables. Watch out; if the expression has two or more tables that have a common column name, you have to use <table name>. <column name> syntax to avoid ambiguity. This also happens to be a good programming practice.

In DB2, the optimizer can look at the mix of jobs, see if more than one session uses a view, and decide to materialize it and share it among sessions. Materialization is not an easy choice. If one statement uses a derived table, it might be better to integrate it into that statement like a text macro. But if many statements are using the same derived table, it might be better to materialize it once, and put the result in primary or secondary storage and share it. This is the same decision the SQL engine had to make with VIEWs. But the derived tables are not at the schema level where the optimizer can find them and keep statistics about them. It takes a pretty smart optimizer to select them for materialization. It might be better to put a derived table definition into a VIEW when it is reused often in systems where this functionality is supported.

Column Naming Rules

Derived tables should follow the ISO-11179 Standard naming rules. A table, base or virtual, is a table. The keyword AS is not required, but it is a good programming practice. If you do not provide names, then the SQL engine will attempt to do it for you. The table name will not be accessible to you since it will be a temporary internal reference in the schema information table. The SQL engine will use scoping rules to qualify the references in the statement — and what you said might not be what you meant. Likewise, columns in a derived table inherit their names from the defining table expression. But only if the defining table expression creates such names. As I already mentioned, the columns in a UNION [ALL], EXCEPT [ALL] or INTERSECT [ALL] statement have no names unless you use the AS clause. (SQL Server does use the names of the first query.)

When you have multiple copies of the same table expression in a statement, you need to tell them apart with different correlation names. For example, given a table of sports players, we want to query the team captain and team co-captain.

SELECT T1. team_name,
 T1. last_name AS captain,   --- local column name
 T2. last_name AS cocaptain  --- second local column name
 FROM Teams AS T1, Teams AS T2
WHERE T1. team_name = T2. team_name
 AND T1. team_position = 'captain'
 AND T2. team_position = 'cocaptain';

I have found that using a short table name abbreviation and a sequence of integers for correlation names works very well. This also illustrates another naming rule. The player’s last name is used in two different roles in this query, so we need to rename the column to the role name (if it stands by itself without qualification) or use the role name as a prefix (i.e., use boss_emp_id and worker_emp_id to qualify each employee’s role in this table).

Derived Tables

A derived table can be complete in itself without a scoping problem at all. For example, consider this query:

SELECT O.order_nbr, B.box_size
   FROM Orders AS O,
    (SELECT box_size, packing_qty
        FROM Boxes) AS B(box_size, packing_qty)
WHERE O. ship_qty <= B. packing_qty;

The derived table B has no outer references, and it can be retrieved immediately while another parallel processor works on the rest of the query. Another form of this kind of derived table is a simple scalar subquery:

SELECT O. order_nbr AS over_sized_order
FROM Orders AS O
WHERE O. ship_qty > (SELECT MAX(packing_qty) FROM Boxes);

The scalar subquery is computed; the one row, one column result table is converted into a unique scalar value, and the WHERE clause is tested. If the scalar subquery returns an empty result set, it is converted into a NULL. Watch out for that last case, since NULLs have a data type in SQL, and in some weird situations you can get casting errors.

When a table expression references correlation names in which they are contained, you have to be careful. The rules are not that much different from any block-structured programming language. You work your way from the inside out.

This is easier to explain with a classic relational division example. We have a table of pilots and the planes they can fly (dividend); we have a table of planes in the hangar (divisor); we want the names of the pilots who can fly every plane (quotient) in the hangar. This is Chris Date’s version of Relational Division; the idea is that a divisor table is used to partition a dividend table and produce a quotient or results table. The quotient table is made up of those values of one column for which a second column had all of the values in the divisor.

SELECT DISTINCT pilot_name
 FROM PilotSkillsAS PS1
WHERE NOT EXISTS
(SELECT *
 FROM Hangar AS H
 WHERE NOT EXISTS
(SELECT *
 FROM PilotSkillsAS PS2
WHERE PS1.pilot_name = PS2.pilot_name
 AND PS2.plane_name = H.plane_name));

The quickest way to explain what is happening in this query is to imagine a World War II movie where a cocky pilot has just walked into the hangar, looked over the fleet, and announced, “There ain’t no plane in this hangar that I can’t fly!” This is terrible English but sound logic.

EDITOR’S NOTE: I found that if I look at the query in a row-by-row fashion, it’s easier to understand. For each pilot, is there a row in the Hangar table that isn’t in the innermost query?

Notice that PilotSkills appears twice in the query, as PS1 and as PS2. We have a local copy of PilotSkills as PS2 and outer references to tables H and PS1. We find that H is a copy of the Hangar table one level above us. We find that PS1 is a copy of the PilotSkills table two levels above us.

If we had written WHERE pilot_name = PS2.pilot_name in the innermost SELECT, the scoping rules would have looked for a local reference first and found it. The search condition would be the equivalent of WHERE PS2.pilot_name = PS2.pilot_name, which is always TRUE since we cannot have a NULL pilot name. Oops, not what we meant!

It is a good idea to always qualify the column references with a correlation name. Hangar did not actually need a correlation name since it appears only once in the statement, but do it anyway. It makes the code a little easier to understand for the people that have to maintain it — consistent style is always good. It protects your code from changes in the tables. Imagine several levels of nesting in which an intermediate table gets a column that had previously been an outer reference.

Exposed Table Names

The nesting in SQL has the concept of an “exposed name” within a level. An exposed name is a table name that is not followed by a correlation name, or a view name that is not followed by a correlation name. The exposed names must be unique.

Here are some examples to demonstrate scoping rules.

SELECT *
FROM (SELECT * FROM A WHERE A.x = 1)  -- A is exposed
             CROSS JOIN 
            (SELECT * FROM B WHERE B.x = 2) -- B is exposed
WHERE  x > 0 ;

Tables A and B can be referenced in the outer WHERE clause. These are both exposed names, but this won’t work in SQL Server which requires you to name a derived table.

You can scope them into a new table, X like:

SELECT *
FROM ((SELECT * FROM A WHERE A.x = 1)
             CROSS JOIN 
            (SELECT * FROM B WHERE B.x = 2))
            AS X  -- only X is exposed
WHERE  x > 0 ;

But only Table X can be referenced in the outer WHERE clause. The correlation name X is now an exposed name.

SELECT . .
 FROM (SELECT *
 FROM A
 WHERE A.x
 = (SELECT MAX(xx) FROM C))
        INNER JOIN
       SELECT *  FROM B WHERE B.c = 2)
WHERE . . ;

Table C is not exposed to any other SELECT statement.

Common Table Expressions

ANSI/ISO Standard SQL-99 added the Common Table Expression or CTE. It is also a query expression that is given a name, just like a derived table. The difference is that they appear before the SELECT clause in which they are used. This is the same as factoring out a common subexpression in algebra! They are just a very useful bit of syntactic sugar. The only thing to remember is that each CTE is exposed in the order they appear in the WITH clause. That means the n-th CTE in the list can reference the first thru (n-1)-th CTEs, but it has to reference them in the order written.

LATERAL Tables

If you have worked with blocked structured procedural languages, you will understand the concept of a “Forward Reference” in many of them. The idea is that you cannot use something before it is created in the module unless you signal the compiler. The most common example is a set of co-routines in which Routine A calls Routine B, then Routine B calls Routine A and so forth. If Routine A is declared first, then calls to B must an additional declaration that tells the compiler Routine B will be declared later in the module. T-SQL does not have this feature yet, but you can use APPLY to get some of the same results. This might be easier to demonstrate than to explain. The following example runs, but it returns the overall average salary and employee count, not the values for each department:

SELECT D1.dept_nbr, D1.dept_name, E.sal_avg, E.emp_cnt
   FROM Departments AS D1,
(SELECT AVG(P.salary_amt), COUNT(*)
    FROM Personnel AS P
WHERE P.dept_nbr
= (SELECT D2.dept_nbr
          FROM Departments AS D2
WHERE D2.dept_nbr = P.dept_nbr)
) AS E (sal_avg, emp_cnt);

Notice that the Departments table appears as D1 and D2 at two levels – D1 is at level one and D2 is a level three.

The following example is not valid because the reference to D.dept_nbr in the WHERE clause of the nested table expression references the Personnel table via P.dept_nbr that is in the same FROM clause.

-- Error, Personnel and Departments are on the same level
SELECT D.dept_nbr, D.dept_name, E.sal_avg, E.emp_cnt
  FROM Departments AS D,
(SELECT AVG(P.salary_amt), COUNT(*)
     FROM Personnel AS P
 WHERE P.dept_nbr = D.dept_nbr) AS E(sal_avg, emp_cnt);

To make the query valid, we need to add a LATERAL clause in front of the subquery. Notice the order of Personnel and Departments. This exposes the department number so that a join can be done with it in the derived table labelled E.

 

-- with a lateral clause
SELECT D.dept_nbr, D.dept_name, E.sal_avg, E.emp_cnt
  FROM Departments AS D,
LATERAL (SELECT AVG(P.salary_amt), COUNT(*)
         FROM Personnel AS P
         WHERE P.dept_nbr = D.dept_nbr) AS E(sal_avg, emp_cnt);

To do the same thing in T-SQL, use CROSS APPLY.

SELECT D.dept_nbr, D.dept_name, E.sal_avg, E.emp_cnt
  FROM Departments AS D
CROSS APPLY (SELECT AVG(P.salary_amt), COUNT(*)
             FROM Personnel AS P
             WHERE P.workdept = D.dept_nbr) AS E(sal_avg, emp_cnt);

Programming Tips

Let me finish with some heuristics.

  1. Use aliases that tell the guy maintaining this code what tables are referenced. Do not simply use A, B, C, etc., as names. This comes from the 1950s when files were referenced by the name of the tape drive that held them.

2. Always “Pretty print” the SQL so that the indentation tells the reader what the query structure is. This is a simple task for any of many tools.

3. Never use SELECT * in production code; it will eventually bite you when a table changes. You have a text editor, and it is easy to cut & paste a list of column names.

4. When you have three or more levels of nesting, think about a CTE. The code will be more readable.

5. When you have two or more references to a query expression think about a CTE or a VIEW if the expression is used in several statements. The code will be more readable, but in many SQL systems, it will also help the optimizer make a decision about materialization.

The post SQL and Scope appeared first on Simple Talk.



from Simple Talk https://ift.tt/2AMUp95
via

Lessons Learned From Modelling Clay

I never allowed my children play with modelling clay in my home when they were young, but changed my mind when grandchildren arrived. I have the perfect table, a discarded fast food restaurant booth, so that the mess stays in one spot. There is one sturdy box to hold all the cans of dough and associated toys. They only play with the modelling clay when I’m watching them, and it’s their favourite activity when visiting our house.

The seven-year-old has been doing an excellent job of keeping the colours separated. Typically, it takes her little brother about five minutes before he mixes all the colours together into one grey mass.

A couple of months ago, he decided to be more careful and keep the colours separated. Things were going well until one fateful day when he created a big rainbow blob that eventually turned into several cans of one solid colour. I asked why he did it, and he explained that it was the easiest thing to do. He was right, maintaining the colours took effort!

Lesson 1: Doing the right thing is rarely the easiest thing. Doing the right thing is harder and takes more commitment.

Another interesting thing about watching them play is that they often don’t create anything recognisable. They can smash and stretch the same blob of clay for 30 minutes or more with no goals in mind.

Lesson 2: Many of us adults think we must always be doing something productive, but doing nothing is necessary, too. Meditation, taking a nap, or just spending a pleasant evening outside to enjoy the sunset have big payoffs.

I never know how long it will be before the kids are ready to play with something else. Without fail, they always ask to play with the modelling clay, but the actual playtime could last 15 minutes or two hours. We have a rule about the modelling clay: it all must be put away and cleaned up before getting out any other games or toys.

Lesson 3: Always complete the task, do the paperwork, add the comments and formatting, and clean up your mess.

Occasionally, they produce a masterpiece. We keep those on a bookshelf to dry out and stay on permanent display. We have spheres, cubes, snakes, butterflies, and more. My favourite was a globe that my grandson created by accident.

Lesson 4: Some great things can happen when you least expect it.

Lesson 5 is the most important thing that I learned, but I learned it too late: Waiting until I had grandchildren to allow the modelling clay in my home was a big mistake. Sorry kids!

Commentary Competition

Enjoyed the topic? Have a relevant anecdote? Disagree with the author? Leave your two cents on this post in the comments below, and our favourite response will win a $50 Amazon gift card. The competition closes two weeks from the date of publication, and the winner will be announced in the next Simple Talk newsletter.

 

The post Lessons Learned From Modelling Clay appeared first on Simple Talk.



from Simple Talk https://ift.tt/2oYmVSm
via

Friday, October 4, 2019

Merging tables in Power BI dataflows with PRO subscription

Power BI dataflows is a powerful self-service ETL tool. However, some features demand the use of Power BI Premium, way more expensive than the PRO version and sometimes inaccessible.

One of these tasks, for example, is the merge of two tables. Even in the same dataflow, if you have two tables and make the merge, the resulting table will be a calculated table and this requires Power BI Premium for the refresh.

requires premium

We could argue the data should be better prepared in Data Warehouse, but this is not always true. The merge of two tables is a so simple task that even having a good DW as a source you may need it.

Let’s say we have two tables, Customers and Geography. Customers table has a GeographyId field which identifies the location of the client.

initial state

In order to make the merge between Customers and Geography we need to follow these steps:

  • Edit the query for the DimCustomer table
  • Click on the ‘…’ button, “Combine Tables” menu option, “Merge Queries”

merge queries

  • In the Merge window that will be opened, select the 2nd table as DimGeography
  • Again in the Merge window, select GeographyId field in the DimCustomer table
  • One more time, in the Merge window, select GeographyId field in the DimGeography table

Merge Window

 

  • After the Merge window updates itself, click Ok button
  • In the DimCustomer table, find the DimGeography field and click the expand button

expand table

  • Select the fields you would like to include in the DimCustomers table and click the Ok button

Selecting Fields

  • Close and save the query

Making the merge between Customers and Geography will turn Power BI Premium into a requirement to refresh the dataflow.

In order to work around this problem, there is a simple solution: we can edit the query again, right-click the table Geography and disable the load option.

load table

When this option is disabled, the merge is not considered as a calculated table, allowing the refresh with a Power BI Pro account, no need for Power BI Premium. Another result is that we will not see the Geography table available in the dataflow anymore, only the Customers table.

single table

The post Merging tables in Power BI dataflows with PRO subscription appeared first on Simple Talk.



from Simple Talk https://ift.tt/30Lhwv8
via

Tuesday, October 1, 2019

Reporting Services Basics: Adding Groups to Reports

The series so far:

  1. Reporting Services Basics: Overview and Installation
  2. Reporting Services Basics: Creating Your First Report
  3. Reporting Services Basics: Data Sources and Datasets
  4. Reporting Services Basics: Parameters
  5. Reporting Services Basics: Adding Groups to Reports

So far in this series, I’ve shown you how to create a basic report with parameters. The next necessary skill for report developers is to add grouping levels to the report. Managers often want to see subtotals, for example, at various levels, and adding groups is the way to do this. The report might be divided into categories and subcategories, maybe by locations and departments, or possibly by year and month. Whenever there is a hierarchical relationship in the data, it might make sense to add groups based on those relationships.

There are several ways to add groups, but I’m going to show you the ones that have worked the best for me.

The Groups section

When you look at a report in Visual Studio in design view, you will see a section under the report canvas with Row Groups and Column Groups as shown in Figure 1.

Figure 1: The groups section

If you don’t see the section, click on the report and then select Report View Grouping from the menu as shown in Figure 2 to make it visible.

Figure 2: Where to turn on the Grouping section

You may be wondering what the difference is between column and row groups, and they are quite different. Row groups are used to organize the report into horizontal sections in a typical report. Figure 3 shows one page of a report with row groups.

Figure 3: A report with row groups

Column groups are used in matrix reports, which might also be described as “pivot” reports. A matrix report displays data from a column across the top of the report as headings. You can also add row groups to matrix reports. For example, you might want to display several years across a report along with row groups for category and subcategory as shown in Figure 4.

Figure 4: A matrix report

The SSRS Toolbox contains Table and Matrix items. When you add either one to a report, you’ll notice that each is called a Tablix instead of the original name. It’s also possible to turn a Table into a Matrix by adding column groups or turn a Matrix into a Table by removing the column groups. In my experience, it is better to start with the one you need.

For years, I used the SSRS wizard to create matrix reports, but eventually I realized that simple matrix reports were not that difficult.

Creating a matrix report

In this example, I’ll show you how to create the report shown in Figure 4. If you need help with setting up an SSRS project or creating data sources or datasets, be sure to go back to the earlier articles in this series to learn more.

To get started, you’ll need an SSRS project with a shared data source pointing to the AdventureWorks2017 database. Create a new report by right-clicking Reports and selecting Add New Item…. In the Add New Item dialog, select Report. Name the report Sales by year and category and click Add. The dialog will look similar to Figure 5.

Figure 5: Creating a new report

Inside the Report Data window, create a Data Source that points to the project’s Shared Data Source. Create a Dataset that is embedded in the report named ProductSales with the query below:

SELECT PC.Name AS Category,
       PS.Name AS SubCategory,
       P.Name AS ProductName,
       P.ProductID,
       YEAR(SOH.OrderDate) AS OrderYear,
       SUM(SOD.OrderQty) AS Quantity,
       SUM(SOD.LineTotal) AS SalesAmt
FROM Production.Product AS P
    JOIN Sales.SalesOrderDetail AS SOD
        ON SOD.ProductID = P.ProductID
    JOIN Sales.SalesOrderHeader AS SOH
        ON SOH.SalesOrderID = SOD.SalesOrderID
    JOIN Production.ProductSubcategory AS PS
        ON PS.ProductSubcategoryID = P.ProductSubcategoryID
    JOIN Production.ProductCategory AS PC
        ON PC.ProductCategoryID = PS.ProductCategoryID
GROUP BY YEAR(SOH.OrderDate),
         PC.Name,
         PS.Name,
         P.Name,
         P.ProductID;

After creating the dataset, the Report Data window should look like Figure 6.

Figure 6: The Report Data window

The next step is to drag a Matrix object to the report canvas from the Toolbox window. You can also right click the canvas and select Insert Matrix. The empty matrix object will look like Figure 7.

Figure 7: The empty Matrix

When creating a matrix report, the trick is figuring out what goes where. The Columns cell will contain the column you want displayed as headings across the report. The Data cell will contain the value you want to aggregate. The Rows cell will be the remaining columns. In this case, the report must display the years across the report and add up the sales amount.

Drag OrderYear to the Columns cell, SalesAmt to the Data cell, and Category to the Rows cell as shown in Figure 8.

Figure 8: Report after adding main items

Notice that you can see the grouping levels in the grouping section. When you run the report, it will look something like Figure 9.

Figure 9: The report so far

The report’s not pretty at this point, but it does display as expected with the years going across the top as headings. The original report also includes subcategories. Go back to design view and right click on the Category group in the Row Groups section. Click Add Group Child Group… as shown in Figure 10.

Figure 10: Adding a Child Group

This brings up the Tablix group dialog. Select SubCategory as shown in Figure 11.

Figure 11: The Tablix group dialog

After clicking OK, the report should now look like Figure 12.

Figure 12: After adding SubCategory

When previewing the report, the top of it should look like Figure 13.

Figure 13: The report

You can format your report or not as desired since this article is about grouping, but at this point, I have to add some formatting for my own sanity! In this case, I have bolded the top row and the first two columns. Everything should end up bolded except for the Data cell as shown in Figure 14.

Figure 14: Bold everything except for the total

I also formatted the SalesAmt in the Data cell to be Currency with a thousands separator and no decimal places and added a heading with the report name. The default margins are too wide for this report, so I also changed them in the Report Properties and made sure that everything in the report was pulled to the left. (To learn more about formatting, see the previous articles.)

After the formatting is complete, the report looks like Figure 15.

Figure 15: The formatted report

The next step is to add some totals to the report. It is so easy to do when working with matrix reports! Right-click the Data cell that contains the summed SalesAmt and select Add Total Row. Repeat for Add Total Column. Figure 16 shows you where to find these.

Figure 16: Adding totals

Adding these two totals will leave your report canvas looking like Figure 17.

Figure 17: After adding the row and column totals

You might just guess the next step: adding a grand total in the empty cell. Hover over the cell until the column list appears and click it as shown in Figure 18. Select SalesAmt which will automatically sum.

Figure 18: Adding SaleAmt to the empty cell for a grand total

I also had to format that last cell since it didn’t pick up the previous formatting. After running the report, the first page in Print Layout mode looks like Figure 19.

Figure 19: The matrix report with totals in Print Layout mode

Obviously, you can add colors, a footer, and more to make this report look nicer, but just getting the data displayed correctly was quite simple! There’s one more thing that might be useful here. If you are in Print Layout mode and scroll, you’ll see that the top headings do not carry over from page to page. (If you are seeing alternating blank pages, go back adjust the page margins and make sure that the report edge has been pulled to the left.) To get the headings to appear on the second and subsequent pages, go to the Tablix properties and change the RepeatColumnHeaders and RepeatRowHeaders to True as shown in Figure 20. Probably the easiest way to see the Tablix properties is by selecting it from the list in the Properties window.

(Note that this method to repeat headings doesn’t work on regular table reports, and I’ll explain how to do that later in the article.)

Figure 20: The properties to get the matrix report headings to repeat

Now that you have seen how to create a matrix report, it’s time to learn how to create a regular row-grouped report.

Creating a table report

You saw a glimpse of row groups in the last section, but this time you will learn even more about row groups in reports as you create the report shown in Figure 1. To get started, create a new report in the project with the name Product Sales. It will also point to the project’s shared data source. Create a dataset using the same query that was used in the matrix report. Add a Table control to the report canvas and fill in the Data row, also known as the detail group, as shown in Figure 21. The headings should fill in by themselves.

Figure 21: The table with the detail row fields

The report is grouped by Category Sub Category OrderYear, so OrderYear is the first “parent” above the detail. In this type of report, I think it is easier to start at the detail and build out, but you can also go the other way. To add the group, right click the Details group in the Row Groups section. Select Add Group Parent Group… as shown in Figure 22.

Figure 22: Adding a parent group

This will bring up the Tablix group dialog where you will select the group’s field. Select OrderYear since it is the direct parent of the detail row. Also check Add group footer. This is where the subtotals will go. The dialog should look like Figure 23.

Figure 23: The Tablix group dialog

After you click OK to add the group, you’ll see quite a few changes to the report canvas. The Order Year group has been added to the report and to the Row Groups section. Figure 24 shows the report canvas at this point.

Figure 24: The OrderYear group added to the report

The next parent level is SubCategory. This time, I’ll show you another way to add a group. Right-click on the OrderYear cell and select Add Group Parent Group… as shown in Figure 25.

Figure 25: Another way to add a parent group

Add SubCategory as the new group and be sure to select Add group footer. Repeat the process to add Category using either method. Make sure to add the new group as a parent to SubCategory. Once all the groups are added, the report canvas should look like Figure 26.

Figure 26: All the groups added to the report

There are a lot of empty cells, and you can add totals to some of them. To figure out which grouping level a particular cell belongs to just select it. You’ll see the grouping level light up in orange as shown in Figure 27.

Figure 27: The grouping level lights up in orange

To add subtotal for each level, fill in SalesAmt for each cell underneath the SalesAmt cell. You can also add subtotals for Quantity. They will automatically sum as shown in Figure 28.

Figure 28: Adding subtotals

You can also add a grand total to the report by right-clicking the bottommost SalesAmt cell and selecting Add Total as shown in Figure 29.

Figure 29: Adding a grand total

When adding a total in this way, it is automatically added to the next parent grouping level. In this case, Category is at the top, so the total is added to the report level. You can add a grand total for Quantity. In this case, just add Quantity to the cell. The report design should look like Figure 30.

Figure 30: Adding grand totals

Preview the report. The report should look something like Figure 31.

Figure 31: The unformatted report

There is quite a bit of formatting that should be done, but I’ll leave it up to you to decide what to do except to be sure to modify the report’s margins so that all the fields fit on one page. After formatting, my report looks like Figure 32.

Figure 32: Report with some formatting

If you take a close look, it’s hard to tell what levels the total amounts refer to. To fix that, follow these steps. In the cell under ProductName, add this expression to the Value property:

="Total for " & Fields!OrderYear.Value

Holding down the Shift key, select the two cells in row 4 and columns 4 and 5. Right-click and select Merge Cells as shown in Figure 33.

Figure 33: Merging two cells

Add this expression to the new larger cell:

="Total for " & Fields!SubCategory.Value

Remove the word Total from the cell to the left. Merge the cells in row 5 and columns 4 and 5. Add this formula:

="Total for " & Fields!Category.Value

Finally, merge the cells in the bottom row and columns 4 and 5. The cell should just say Grand Total. Delete the original word Total from several cells. The report design should look like Figure 34.

Figure 34: The subtotals labeled

I would like to move some of the labels more to the left, but I found that the Merge Cells option was not always available. If it is for you, then modify accordingly.

The last thing to do to make this report more usable is to have the headings repeat on new pages. If you go into Print Layout mode and scroll to page 2, you will see that the top headings do not repeat. The RepeatRowHeaders property doesn’t seem to do anything on regular table reports. There is a way to do this, though.

On the grouping section at the bottom of the page, select Advanced Mode as shown in Figure 35.

Figure 35: Turning on Advanced Mode

Make sure this is checked which will add Static sections to the row and column groups as shown in Figure 36.

Figure 36: The Static groups

Working only on the row groups, select the first Static group and open the Properties window. Change the RepeatOnNewPage property to True as shown in Figure 37.

Figure 37: The RepeatOnNewPage property

Repeat the process for the next two Static groups. Now when you run the report and scroll to page 2, you’ll see the top headings repeated. Figure 39 shows you page 2 of my report.

Figure 39: The report with repeating row headings

Summary

This article introduced adding grouping by walking you through the creation of two simple reports. This article covered a lot of ground, but there is still so much to learn. In the next article, you’ll learn more about adding expressions to reports to add even more functionality.

 

The post Reporting Services Basics: Adding Groups to Reports appeared first on Simple Talk.



from Simple Talk https://ift.tt/2ngPx8Z
via