In ETL, we often have to load many targets from a common set of base tables. Inevitably the targets are different enough that we have to create multiple queries or views to populate the many outputs of data. Which is fine, except now you’ve got yourself a maintenance nightmare, one that is avoidable. I’d like to share a trick with you to take a single query that can be recursively modified to dynamically change its structure to get different outputs. I call it, dynamic querying using block quotes.
The business case:
The sales department would like to take a single report that already exists and split it into two reports. The additional report will require different fitlers, aggregates, columns, and even joins!
Dynamic querying using block quotes, the full query:
sum(ol.quantity) as QuantitySold,
sum(ol.unitprice) as ValueSold
FROM sales.Orders o
JOIN sales.OrderLines ol
on o.orderid = ol.orderid
JOIN application.people p
on p.personid = o.salespersonpersonid
join warehouse.packagetypes pt
on ol.packagetypeid = pt.packagetypeid
and p.IsSalesperson = 1
The above query will output Quantity Sold and Value Sold for each Salesperson which has a salesperson flag. Then, by using this same query, we can programmatically, within a procedure or in our ETL, find and replace the block quotes to create an entirely different query of Quantities and Values by Package Type. You’ll need to find and replace all four of the following against your query text:
FIND "SalesBySalesperson_Begin*/" REPLACE WITH "SalesBySalesperson_Begin/"
FIND "/*SalesBySalesperson_End" REPLACE WITH "/SalesBySalesperson_End"
FIND "SalesByPackageType_Begin/" REPLACE WITH "SalesByPackageType_Begin*/"
FIND "/SalesByPackageType_End" REPLACE WITH "/*SalesByPackageType_End"
What’s fantastic about this approach, and hopefully you noticed, we can dynamically add joins, join conditions, filters, groupings, and columns to different block quote begin/end chunks.
This example was built using Microsoft’s World Wide Importers database. Oh, and it’s ANSI Standard.
Have you done anything like this, using block quotes, to enhance your reporting?