24 Sub-Queries - webiworx · 2014. 10. 17. · Sub-Queries 397 24 Sub-Queries Sub-Queries are...

5
Sub-Queries 397 24 Sub-Queries Sub-Queries are another method of restricting data, but they use the results of one query to restrict a second query. Web Intelligence generates SQL that is processed by the database in two steps: 1. Run the sub-query and return a set of results 2. Use the results of the sub-query to restrict an outer-query 3. Return the results of the restricted outer-query back to the user A point to note here is that only one set of results is returned to the user, although the database runs more than one query in the background. 1. Create the following query using the ‘eFashion’ universe and run it. We would like to create a query to show all years for the States that have sold less than 6000 (in quantity) in [This year], i.e. using this example we want to show data for all years for the states Massachusetts, Colorado and Florida. To do this we edit our query to include a sub-query. 2. In the Query Panel select [State] in the Result Objects pane and then click on the ‘Sub-Query’ icon to start defining the sub-query: This simple query shows data for [This year] that has been sorted descending on [Quantity sold]. We have 3 States selling less than 6000. Sub- Query Outer- Query

Transcript of 24 Sub-Queries - webiworx · 2014. 10. 17. · Sub-Queries 397 24 Sub-Queries Sub-Queries are...

Page 1: 24 Sub-Queries - webiworx · 2014. 10. 17. · Sub-Queries 397 24 Sub-Queries Sub-Queries are another method of restricting data, but they use the results of one query to restrict

Sub-Queries

397

24 Sub-Queries

Sub-Queries are another method of restricting data, but they use the results of one query to restrict a second query. Web Intelligence generates SQL that is processed by the database in two steps:

1. Run the sub-query and return a set of results 2. Use the results of the sub-query to restrict an outer-query 3. Return the results of the restricted outer-query back to the user

A point to note here is that only one set of results is returned to the user, although the database runs more than one query in the background. 1. Create the following query using the ‘eFashion’ universe and run it.

We would like to create a query to show all years for the States that have sold less than 6000 (in quantity) in [This year], i.e. using this example we want to show data for all years for the states Massachusetts, Colorado and Florida. To do this we edit our query to include a sub-query. 2. In the Query Panel select [State] in the Result Objects pane and then click on the ‘Sub-Query’ icon

to start defining the sub-query:

This simple query shows data for [This year] that has been sorted descending on [Quantity sold]. We have 3 States selling less than 6000.

Sub-Query

Outer-

Query

Page 2: 24 Sub-Queries - webiworx · 2014. 10. 17. · Sub-Queries 397 24 Sub-Queries Sub-Queries are another method of restricting data, but they use the results of one query to restrict

Sub-Queries

398

Let us first describe the actual Sub-Query Filter itself: Object placed here will be used to restrict the outer-query by comparing the values in the database to the values returned by the sub-query object.

Object placed here will be the columns of data returned by the sub-query.

Objects placed here will be used to restrict the sub-query itself.

3. Drag and drop the objects to create the definition of the sub-query as shown below:

The boxed area is showing the definition of the sub-query. The sub-query will return a single column of data ([State]), listing states that satisfy the filter of having [Quantity sold] less than 6000 in [This year].

The outer-query will be restricted by comparing [State] values to the [State] values returned by the sub-query. 4. Run the query.

The sub-query runs first to bring back the states that have sold less than 6000 this year. Then its results are used to restrict the outer-query to show data for all years but only for the states that have been returned by the sub-query.

Page 3: 24 Sub-Queries - webiworx · 2014. 10. 17. · Sub-Queries 397 24 Sub-Queries Sub-Queries are another method of restricting data, but they use the results of one query to restrict

Sub-Queries

399

5. Create a new table as follows (or re-arrange the existing table):

Creating meaningful sub-queries requires an understanding of how the definition of sub-queries works.

1. The red shaded area (labelled as ‘1’) forms the definition of the sub-query:

It is used to fetch a single column of values, i.e. States that have sold less than 6000 in quantity for this year. In other words, a sub-query effectively defines a list of values.

2. The orange shaded area (labelled as ‘2’) can be referred to as the outer-query (in relation to the

sub-query) and provides the actual data returned to use within the document:

In this case we have 3 columns, [Year], [State] and [Quantity sold].

3. The outer-query and the sub-query are related by the area highlighted in blue (labelled as ‘3’).

1 – The sub-query returned three [State] values that have a [Quantity sold] of less than 6000 in [This year]. These values were Colorado, Florida and Massachusetts. 2 – These three values were then used to restrict the outer-query resulting in data being returned for all years but only for these three states.

1

2

3

Page 4: 24 Sub-Queries - webiworx · 2014. 10. 17. · Sub-Queries 397 24 Sub-Queries Sub-Queries are another method of restricting data, but they use the results of one query to restrict

Sub-Queries

400

The aim is to use objects that will allow a comparison to be made to restrict the outer-query by using the values returned by the sub-query. In this case we are restricting the outer-query to fetch data for only those states that are returned by the sub-query.

24.1 Sub-Queries and other Query Conditions

An outer-query can have further restrictions applied to it by using other Query Filters alongside the Sub-Query filter. Our example returned data for all years for states that sold less than 6000 in this year: 1. Edit and run the query to request only last year’s data for the states that sold less than 6000 in this

year:

In this example we have requested the outer-query to be restricted:

To the states returned by the sub-query.

Then for the states satisfying the sub-query, we only want data for [Last year].

The results of the outer-query now show data for [Last year] only.

2. Save your document.

Any objects placed in the Query Filters pane that are outside of the Sub-Query filter are applied to the outer-query only.

Page 5: 24 Sub-Queries - webiworx · 2014. 10. 17. · Sub-Queries 397 24 Sub-Queries Sub-Queries are another method of restricting data, but they use the results of one query to restrict

Sub-Queries

401

24.2 Sub-Queries and Query Contexts

In an earlier session it was described that ‘Query Contexts’ enforce data integrity by ensuring incompatible paths between tables are not used to generate SQL that yields incorrect results. Having used the ‘Island Resorts Marketing’ universe, we know that sales data and reservations data are in different contexts (related to Sales and Reservations respectively). However, when using sub-queries we can use different contexts in the sub-query compared to the outer-query. 1. Create a new document with the following query using the ‘Island Resorts Marketing’ universe:

The above query definition is requesting [Revenue] for each [Customer] in ‘2006’ where any sort of reservation was made in ‘2007’. 2. Run the query and the results of the above query are shown below:

[Revenue] and [Year 2006] are from Sales context therefore the outer-query is going to use the Sales context.

[Year 2007] is from Reservations context therefore the sub-query is going to use the Reservations context.