7 Collecting data from a database using a supplied query
- Chris Welford
- Zoe Baldwin
- Anthony George
- Fiona Sargeant (Unlicensed)
By the end of this chapter you will be able to do the following:
- Create a database collector directly in a model
- Collect data from a database using a supplied query
Collect data from a database using a supplied query
You will add a database collector to your Introduction to Modelling analysis model that will use a query that has been supplied to you. This will retrieve the last bill for each customer:
- Hover your mouse over Databases in the toolbar to show the list of options.
- Drag Database Collector from the toolbar into the model.
- In the New Collector settings, enter:
- Name:
Last bill amount
- Datasource:
CRM
Statement Expression:
select CustomerRef, BillAmount from SOURCE_CUST_BILLS SCB1 where BillDate = (select max(BillDate) from SOURCE_CUST_BILLS SCB2 where SCB2.CustomerRef = SCB1.CustomerRef)
- Allow Non-Scheduled Collection: .
- Enabled: .
- Name:
- Press Apply.
At the top of the Last bill amount database collector settings, press More Options and press Run query and see result.
In a view window that pops up you can see the data that will be returned by the query; this feature is a useful way to check that your query works.
Close the view window.
Press Apply and Close in the Last bill amount database collector settings.
Now will you create a table based on the structure of the query results:
Hover over the Last bill amount database collector on your model.
In the pop up menu press Create Table Using Attributes
A new table will appear on your model to store the results of the query. Run analysis on the new table and check that the data has been loaded from the database, like the image below:
Enrich customer data with last billed amount
You will now add a lookup pipe from the table Last Bill Amount into the table Customer Details with Region, linking the data across the two tables using the attribute CustomerRef
:
- Hover over the table Last Bill Amount and press Connector.
- Move the pointer over the top of the table Customer Details with Region and select – this will attach the end of the pipe to the selected table.
- In the pipe details form that pops up enter:
- Name:
lba
- Type: Look-up
- Data To Read: leave as the default – Latest
- Press Apply.
- In the Order/Index section:
- Press Show Attributes.
- Drag the attribute CUSTOMERREF from the left hand list into the list of Order/Index attributes.
- Drag the attribute CustomerRef from the right hand list on top of the Order/Index attribute CUSTOMERREF.
- This means that the lookup will match records in the reference data (Last Bill Amount) where CUSTOMERREF
=
CustomerRef in the output data (Customer Details with Region).
- This means that the lookup will match records in the reference data (Last Bill Amount) where CUSTOMERREF
- Press Apply and Close on the pipe configuration form.
- Name:
- A lookup pipe will appear linking Last Bill Amount to Customer Details with Region
.
- Drag the attribute
BILLAMOUNT
from the table Last Bill Amount onto Customer Details with Region.
- Run analysis on Customer Details with Region
.
- View the data in the Customer Details with Region table to check that the last billed amount has been retrieved for all records, like the image below:
- In the model window, click Save.
Check Point
At this point your screen should look similar to this: