From the datawarehouse to the chart (part 2)
Last week I introduce the MS Query feature of Excel.
Let's talk today about a few tricks to optimize its utilization with Oracle.
Ok first you may have notice that this a very slow feature. Changes to the source query are uneasy. You need to open the source query. Then to use an old fashion query defintion tool. And then to go back to the Excel file to finally realize that you have to change some criteria in the SQL code.
How to speed up this?
Use your knowledge of the database
It seems to be an easy-to-say thing but, to my point of view, this it is critical. I think that Excel by it self don't have to be a datamart and shouldn't. You should Use Oracle Views to reformat or re-structure your data. There is many standard and Oracle specific SQL statements which can help you for this (that is a good idea for a next post).
Use routine to change SQL.
You are the developper. You know where to get the information in the database.
Most of the time you define your queries in your fovarite Oracle tool and not in Excel or MS Query. So you don't need a wizard to explore the database. Instead you may need, as I, a quick way to seek-and-destroy bugs.
I prefer to use my own a VB statement (which will be not stored into the final file).
Here is the sub I use :
Sub createquery(strODBC, strSQL, strDestinationRange, strNAme)
With ActiveSheet.QueryTables.Add(strODBC, Destination:=Range(strDestinationRange))
.CommandText = strSQL
.Name = strNAme
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
strODBC is the an ODBC string something like :
strSQL is the SQL statement you want to execute here
strDestinationRange is something like "B2" or "$A$12:$H$52"
You should use this statement carefully at your own risq and, off course, fit it to your needs.
Notice that you may change the ODBC string structure, for instance if you omit thePassword excel will prompt you to logon. Notice that
Parameters allow you to change criteria of a query without any VB code.
Here is the way I use parameters in MS query :
I use my favorite Oracle developpement tool and make a SQL query without parameters for instance :
select name from customers where max_ammount>1000
and min_amount <100
here is the versions with parameters
select name from customers where max_ammount>?
and min_amount < ?
Yes, there is no named parameters. Excel have specifical way to bind those things, just using order.
Try it, for instance with the preceding VB code, or with the MS Query tool.
When refreshing the data, Excel ask you to fill those parameters.
At any time you can specify the source for each parameters:
- Right click on the range of data.
- Choose "parameters"
- Select the parameter you
- Then select the option you want for each parameter.
The first option is interesting. It allows you set the text of a question which will be asked to the user for this parameter.
Notice that when you use VB code to create a query with parameter, Excel gives default names to these parameters. If you want set your own names use the following syntax in a vb module :
Dim qt As QueryTable
Set qt = ActiveSheet.QueryTables("MyQuery")
qt.Parameters("Parameter1").Name = ("myFirstParam")
qt.Parameters("Parameter2").Name = ("mySecondParam")
All properties of parameters seems to be reachable by rightclick->parameters but the name which can be change only by code.
Now you know how to make get data from parameterized query over Oracle into your Excel workbook, I suggest you the following if you need to produce many charts from yout datawarehouse.
I - Let's suppose you have built a View describing the monthly 's turnover of your products. In a new excel sheet type-in the product id of one of your products, build a query like the following :
Select MonthNumber, turnover from v_monthly_turn where product_Id = ? and year = 2004
II - When the wizard prompt you for the parameter's value, select a cell which contains your product's ID and check the box to make this permanent.
III - When the data are returned select the range. Click on the chart button an choose your chart's style and location.
IV - Change your product's ID and refresh the data by right click->refresh data over the data range.
You may customise this:
Instead of a cell based parameter use a parameter whith a question like "What is the product ID?" . You may then set the "Refresh on file open" property to yes :(right click on the range then "properties") your file will then become a little decisional tool. And this without any VB code!
That 's all for today thank you for your attention.
All comments are welcome.