You are on page 1of 42

Query BuiIder Guide

TabIe of Contents
CIoud9 Query BuiIder Overview (Beta) 3
Query Builder Components 5
Query Result Tab 6
SQL Script Tab 7
Query BuiIder Join Types 8
Criteria VaIues 11
Timestamp literal formats 11
Date literal formats 13
Time literal formats 13
Run Saved Server Reports 14
Create CaIcuIated FieIds 16
Aggregates 16
Use Multiple Fields 17
2: Build calculation 18
3: Set limits for data returned 19
4: Review and Execute 20
5: Add d to Query 21
Create Time Comparisons 23
2: Create a Join 24
3: Comparison Filters 25
4: View Results 26
Create a Trend Report 29
ExampIes 32
Example: Filtering in WHERE 32
Example: Open by Rep 32
Set Parameters 34
Add Date Prefix 34
Verify and Execute Open by Rep 35
Example: Filters, Sorting and Aggregates 37
Example: Union query 39
Create Subquery 40
3
CIoud9 Query BuiIder Overview (Beta)
When you launch Cloud9 Query Builder, the login will be populated with name and user
account information from Cloud9.
Once connected, the tree on the right will be populated with schemas, tables and views.
Use Query Builder > Main tab to create a query using drag and drop.
4
When you drop a table onto the design area in the Main tab, you can select output fields by
ticking the checkboxes. Selected fields are displayed in the Expressions list at the bottom.
5
Query BuiIder Components
The main components of Query Builder aid in navigation of the application.
1. Menus
2. Toolbar
3. Tabs - Visual Query Builder, SQL editor tab and Query Result tab
4. Main tab - visual query design area
5. Expression editor
6. Metadata tree - shows available schemas, tables and views
6
Query ResuIt Tab
Select the Query Result tab to view and edit the results, or export to either CSV or Excel.
7
SQL Script Tab
Select the SQL Script tab to view or edit the SQL generated by your query.
8
Query BuiIder Join Types
To create a join in Cloud9 Query Builder, select a field in one table and drop it to a field in
another table. To change the join type, right-click on the join line and choose Select all
rows from. Query Builder supports joins on multiple fields; they do not have to be PK-FK
fields.
Inner Join (DefauIt)
Inner join is the most common join operation and can be regarded as the default join type.
Inner join creates a new table by combining two tables (A and B) based on the join pred-
icate. The query compares each row of A with each row of B to find all pairs of rows that
satisfy the join predicate. When the join predicate is satisfied, column values for each
matched pair of rows of A and B are combined into a result row.
Outer Joins
Outer join does not require the records in two joined tables to be matching records. The
joined table retains each record, even if no other matching record exists. Outer joins sub-
divide further into left outer joins, right outer joins, and full outer joins, depending on
which table(s) one retains the rows from (left, right, or both).
Left Join
A left outer join returns all the values from the left table, plus matched values from the
right table (or NULL in case of no matching join predicate). The result of a left outer join
(or simply left join) for table A and B always contains all records of the left table (A), even
if the join condition does not find any matching records in the right table (B). This means
that if the ON clause matches 0 (zero) records in B, the join will still return a row in the
result - but with NULL in each column from B. If the right table returns one row and the
left table returns more than one matching row, the values in the right table will be
repeated for each distinct row on the left table.
9
Right Join
A right outer join (or right join) closely resembles a left outer join, except with the tables
reversed. Every row from the right table (B) will appear in the joined table at least once.
If no matching row from the left table (A) exists, NULL will appear in columns from A for
those records that have no match in B. A right outer join returns all the values from the
right table and matched values from the left table (NULL in case of no matching join pred-
icate). For example, this allows us to find each employee and his or her department, but
still show departments that have no employees.
FuII Join
Conceptually, a full outer join combines the effect of applying both left and right outer
joins. Where records in the FULL OUTER JOIN results tables do not match, the result set
will have NULL values for every column of the table that lacks a matching row. For those
records that do match, a single row will be produced in the result set, containing fields pop-
ulated from both tables.
10
For example, this allows us to see each employee who is in a department and each depart-
ment that has an employee, but also see each employee who is not part of a department
and each department that doesn't have an employee.
11
Criteria VaIues
To enter datetime, timestamp, or string value, use a single quote around a value in the
Criteria column, e.g. `New (string), `2011-09-09 (datetime), `2011-09-09 15:30 (times-
tamp). See the sections on valid formats for Timestamp, Date, and Time formats.
Timestamp IiteraI formats
These are the valid timestamp literal formats.
Format Example
"yyyy/MM/dd 'at' hh:mm:ss:SSS a zzz" 2004/04/25 at 01:42:19:543 am PST
"EEE MMM dd HH:mm:ss z yyyy" FRI Jun 25 13:42:19 GMT 2004
"MMM dd, yyyy HH:mm:ss a z" Jun 25, 2004 13:42:19 AM PST
"MMM dd, yyyy HH:mm:ss a" Jun 25, 2004 13:42:19 AM
"MMM dd, yyyy HH:mm:ss" Jun 25, 2004 13:42:19
"yyyy-MM-dd HH:mm:ss z" 2004-06-25 13:42:19 PST
"yyyy-MM-dd HH:mm:ss Z" 2004-06-25 13:42:19 -0800
"yyyy-MM-dd HH:mm:ss.SSS z" 2004-06-25 13:42:19:543 PST
12
"yyyy-MM-dd HH:mm:ss.SSS Z" 2004-06-25 13:42:1.543 -0800
"yyyy-MM-dd HH:mm:ss.SSS" 2004-06-25 13:42:19.543
"yyyy-MMM-dd HH:mm:ss.SSS" 2004-06-25 13:42:19.543
"yyyy-MM-dd HH:mm:ss" 2004-06-25 13:42:19
"yyyy-MMM-dd HH:mm:ss" 2004-Jun-25 13:42:19
"MM/dd/yyyy HH:mm:ss" 06/25/2004 13:42:19
"MMM/dd/yyyy HH:mm:ss" Jun/25/2004 13:42:19
"MMM-dd-yyyy HH:mm:ss" Jun-25-2004 13:42:19
"dd-MMM-yyyy HH:mm:ss" 25-Jun-2004 13:42:19
"dd-MM-yyyy HH:mm:ss" 25-06-2004 13:42:19
"HH:mm:ss z yyyy" 1:32:19 GMT-0800 2004
"HH:mm:ss Z yyyy" 1:32:19 -0800 2004
"yyyy-MM-dd HH:mm z" 2004-06-25 13:42 GMT-0800
"yyyy-MM-dd HH:mm Z" 2004-06-25 13:42-0800
"yyyy-MM-dd HH:mm" 2004-06-25 13:42
"yyyy-MMM-dd HH:mm" 2004-Jun-25 13:42
"MM/dd/yyyy HH:mm" 06/25/2004 13:42
"MMM-dd-yyyy HH:mm" Jun-25-2004 13:42
"dd-MMM-yyyy HH:mm" 25-Jun-2004 13:42
"dd-MM-yyyy HH:mm" 25-06-2004 13:42
"yyyy-MM-dd HH z" 2004-06-25 01 GMT-0800
"yyyy-MM-dd HH Z" 2004-06-25 01 -0800
"yyyy-MM-dd HH" 2004-06-25 01
"yyyy-MMM-dd HH" 2004-Jun-25 01
"MM/dd/yyyy HH" 06/25/2004 01
"MMM/dd/yyyy HH" Jun/25/2004 01
"MMM-dd-yyyy HH" Jun-25-2004 01
"dd-MMM-yyyy HH" 25-Jun-2004 01
"dd-MM-yyyy HH" 25-06-2004 01
13
Date IiteraI formats
These are the valid date literal formats.
Format Example
"yyyy-MM-dd" 2004-06-25
"yyyy-MMM-dd" 2004-Jun-25
"MM/dd/yyyy" 06/25/2004
"MMM/dd/yyyy" Jun/25/2004
"MMM-dd-yyyy" Jun-25-2004
"dd-MMM-yyyy" 25-Jun-2004
"dd-MM-yyyy" 25-06-2004
Time IiteraI formats
These are the valid time literal formats.
Format Example
"HH:mm:ss z" 13:42:19 PST
"HH:mm:ss Z" 13:42:19 -0800
"HH:mm:ss" 13:42:19
"HH:mm z" 13:42 GMT-0800
"HH:mm Z" 13:42 -0800
"HH:mm" 13:42
14
Run Saved Server Reports
To run a saved server report:
1. Choose Open from the Server Report menu.
2. Select the report you want to run from the list of available server reports and click
Load.
The report will be loaded into Cloud9 Query Builder and will appear in the Main
tab.
3. Click Execute Query to run the query.
15
The Query Result tab will display the results.
16
Create CaIcuIated FieIds
This section provides examples for several approaches to creating calculated fields.
Aggregates
To create an aggregate:
1. Drag the Opportunity table onto the Main tab.
2. Select the Amount field by ticking the checkbox. Note that the field shows up in the
Expression grid under the Expression column.
3. Click in the Aggregate row and choose Avg from the dropdown.
4. Click on the Alias row and type in AvgAmount, then click Return. You have created
the AvgAmount aggregate calculated field.
Generated SQL
Select Avg("ads"."Opportunity"."Amount") As "AvgAmount"
17
From "ads"."Opportunity"
Use MuItipIe FieIds
In this section, we'll create a calculated field that is the difference between the expected
revenue and amount. Well call it DeltaAmount, and fetch this information for just the first
ten opportunities.
To create a calculated field using multiple fields:
1. Drag the Opportunity table onto the Main tab.
2. Select ExpectedRevenue by ticking the checkbox. Notice the field shows up in
Expression grid in the Expression column.
3. Click inside the Expression row at the bottom and click the button on the right to
open Expression Editor.
Next: 2: Build calculation
2: Build calculation 18
3: Set limits for data returned 19
4: Review and Execute 20
5: Add Id to Query 21
18
2: Build calculation
To build the calculation in Expression Editor:
1. In the Expression Editor, click at the end of the field and type a hyphen (-) to start
building the calculation.
2. Double-click Opportunity on the left to insert a table name and type a dot (.) after
the table name to view a list of available fields.
3. Double-click Amount to insert the Amount field.
The final expression displays as shown.
4. Click OK to go back to the Main tab.
Next: 3: Set limits for data returned
19
3: Set limits for data returned
To set limits for data returned:
1. In the Main tab, click in the Alias column and type DeltaAmount (calculated field
name).
2. Right-click in the Main tab and select Properties from the context menu to open Main
Query Properties Setup and set a limit for the number of records returned.
3. In Main Query Properties Setup, tick the LIMIT checkbox and enter 10 to set the
limit to 10 records.
4. Click OK.
20
Next: 4: Review and Execute
4: Review and Execute
To review and execute the query:
1. Select the SQL Script tab to review the query and check that Delta and Limit are
included.
2. Click Execute Query .
3. Select the Query Result tab to view the data returned.
You may notice discrepancies between expected and actual amounts, and want to inves-
tigate further, but you need opportunity Ids. You can return to the query and add Ids.
21
Next: 5: Add Id to Query
5: Add Id to Query
To add Id to the query:
1. Choose the Id field from the Opportunity table; it will appear in the Expression list.
2. Click Execute Query and run the query again.
Opportunity Id is displayed for each DeltaAmount and you can investigate the
opportunity further.
22
23
Create Time Comparisons
Detecting changes in records between two points in time is a common application for
Cloud9 Query Builder. Heres an example of how to detect amount, stage, or probability
changes in a set of opportunities between 9-1-2011 and 9-27-2011.
To set up a time comparison:
1. Add the Opportunity table to the Main tab and select the fields that you would like in
your report.
2. Double-click in the Main tab area to open Main Query Properties Setup, and set the
starting point to As Of Date to 2011-09-01.
3. Return to the Main tab and right-click Q to open Main Query Properties Setup in a
second window.
4. Select the Opportunity table again and select the fields that you want to compare.
24
5. Double-click in the Main tab area to set the comparison date to 2011-09-27.
Next: 2: Create a Join
2: Create a Join 24
3: Comparison Filters 25
4: View Results 26
2: Create a Join
1. Return to the Main tab. You should see two opportunity tables.
2. Double-click the join connector to open Link Properties and choose your join. In this
case, you should select a left join to include all of the original opportunities, in order
to see which ones have changed.
25
3. Rename the fields using the Alias column, to distinguish the original value from the
new value for each field. You can also adjust the field order.
Next: 3: Comparison Filters
3: Comparison FiIters
To create the comparison filters:
1. Click on the Criteria cell for the field "Opportunity2.StageName and click the right-
hand button to open Expression Editor.
2. Type <>, select the StageName field from the Opportunity table, and click OK.
26
3. Repeat for the Amount and Probability fields.
Next: 4: View Results
4: View ResuIts
Click Execute Query to view the results.
27
ExampIe query resuIt
Generated SQL
As Of '2011-09-01'
28
Select "Opportunity"."Name" As "OppName", "Opportunity"."StageName" As
"OrigStageName",
"Opportunity2"."StageName" As "EndStageName", "Opportunity"."Amount" As
"OrigAmount",
"Opportunity2"."Amount" As "EndAmount", "Opportunity"."Probability" As
"OrigProbability",
"Opportunity2"."Probability" As "EndProbability", "Oppor-
tunity"."CloseDate" As "OrigCloseDate",
"Opportunity2"."CloseDate" As "EndCloseDate", "Opportunity"."IsClosed"
As "OrigIsClosed",
"Opportunity2"."IsClosed" As "EndIsClosed", "Opportunity"."IsWon" As
"OrigIsWon",
"Opportunity2"."IsWon" As "EndIsWon", "Opportunity"."ForecastCategory"
As "OrigForecastCategory",
"Opportunity2"."ForecastCategory" As "EndForecastCategory", "Oppor-
tunity"."Id" As "Id"
From "ads"."Opportunity" "Opportunity" Left Outer Join
(As Of '2011-09-27'
Select "Opportunity"."StageName" As "OrigStageName", "Oppor-
tunity"."Amount" As "Amount",
"Opportunity"."Probability" As "OrigProbability", "Oppor-
tunity"."CloseDate" As
"OrigCloseDate", "Opportunity"."IsClosed" As "OrigIsClosed", "Oppor-
tunity"."IsWon" As
"OrigIsWon", "Opportunity"."ForecastCategory" As "Orig-
ForecastCategory", "Opportunity"."Id" As
"Id"
From "ads"."Opportunity" "Opportunity") "Opportunity2" On ("Oppor-
tunity"."Id" =
"Opportunity2"."Id")
Where ("Opportunity2"."StageName" <> "Opportunity"."StageName") Or
("Opportunity2"."Amount" <> "Opportunity"."Amount") Or
("Opportunity2"."Probability" <> "Opportunity"."Probability")
29
Create a Trend Report
To create a trend report:
1. Select the table you want to use for your trend report and select the fields that you
want to see.
2. Double click in the Main tab to open Main Query Properties Setup.
3. Select Trend and set the trend start and end dates. To set a query row limit to speed
up your results, tick the LIMIT checkbox and enter a number. Once you have final-
ized your query, you can remove the limit.
4. Add filters as required.
30
5. Click Execute Query and view the results. You can add addition fields as needed
(Only the Trend Date and Count will appear in this particular example, since no
fields are selected in the table).
ExampIe query resuIts
31
Generated SQL
Trend From Date '2011-01-01' To Date '2011-09-27' By Interval '1' Month
Select TrendDate() As "TrendDate", Count("Opportunity"."Id") As "Closed-
WonCount"
From "ads"."Opportunity" "Opportunity"
Where "Opportunity"."CloseDate" > Date '2011-01-01' And "Oppor-
tunity"."IsWon" = True
Group By *
32
ExampIes
This section provides some examples using sample queries that are commonly used.
ExampIe: FiItering in WHERE
This example shows filtering for (1 and 2), e.g. ads, Opportunity, FiscalYear = 2010 And
ads, Opportunity, ForecastCategory = Forecast.
This example shows filtering for (1 or 2), e.g. ads, Opportunity, FiscalYear = 2010 Or ads,
Opportunity, ForecastCategory = Forecast.
This example shows filtering for (1) or (3 and 4), e.g. (ads, Opportunity, IsClosed = True)
Or (ads, Opportunity, FiscalYear = 2011 And ads, Opportunity, ForecastCategory = Best-
Case).
This example shows filtering for (1 and 2) or (3 and 4), e.g. (ads, Opportunity, FiscalYear
= 2010 And ads, Opportunity, ForecastCategory = Omitted) Or (ads, Opportunity,
IsClosed = True And ads, Opportunity, Probability = 75).
ExampIe: Open by Rep
This example demonstrates how to build a query for open opportunities by rep in a
quarter. You'll work with two tables to build this query: Opportunity (ads) and User (secu-
rity).
33
To build open opportunity by rep query:
1. In the Metadata tree, double-click the Opportunity and the User tables to open them.
The two tables will appear in the Main tab, with PK-FK link created automatically.
2. In the Opportunity table, tick the checkboxes to select:
z StageName
z Amount
z IsClosed
z ForecastCategoryName
5. In the User table, tick the checkbox to select Name.
All selected fields will show up in the Expression editor.
Next: Set Parameters
Set Parameters 34
34
Add Date Prefix 34
Verify and Execute Open by Rep 35
Set Parameters
To set parameters in Expression grid:
1. To set aggregate options, click inside a cell in the Aggregate column and select Sum
from the dropdown menu.
z To enter an alias, click inside a cell in the Alias column and type a name for the
alias.
2. To set conditions for the Where clause, click inside a cell in the Criteria column and
type a condition, e.g = False for IsClosed.
3. To exclude fields from output, untick checkboxes in the Output column, e.g.
IsClosed field (used for Where only and should not be in output).
Next: Add Date Prefix
Add Date Prefix
To add a date prefix:
1. Right-click in the Main tab and choose Properties from the dropdown menu.
35
2. In Main Query Properties Setup, tick SFbeginningOf. and choose Quarter from the
dropdown. Set Limit to 1000 rows.
Next: Verify and Execute Open by Rep
Verify and Execute Open by Rep
To verify and run the query:
1. Click the SQL Script tab to verify your query.
36
2. Click Execute Query to run the query.
ExampIe query resuIt
Query Builder displays the query execution time in seconds, and the number of rows
returned is shown at the bottom.
Generated SQL
As Of SFBeginningOfQuarter(Current_Date)
37
Select "ads"."Opportunity"."StageName", Sum("ads"."Opportunity"."Amount")
As "ThisQuarterAmt",
"security"."User"."Name"
From "ads"."Opportunity" Inner Join
"security"."User" On "security"."User"."Id" = "ads".-
"Opportunity"."OwnerId"
Where "ads"."Opportunity"."IsClosed" = False And "ads".-
"Opportunity"."ForecastCategoryName" <>
'Closed'
Group By *
Limit 1000
ExampIe: FiIters, Sorting and Aggregates
The Opportunity table is used for this example. The output fields chosen in Select are
Amount, Name, and Probability. The fields used for filtering (Where clause) are Type,
Lead Source, Probability, and IsClosed.
To set filter and sorting:
1. Select ALL fields listed by ticking the checkboxes in the Opportunity table. They will
appear in the Expression list. Note that all boxes in the Output column are checked
(well change their state in a later step).
2. To set up AND for a Where clause use the Criteria column; for OR use the Or.. col-
umn in the Expression list. The AND setting appears vertically and OR appears hor-
izontally. Click inside a cell and type in a filter value. Note that fields used for
filtering in Where only appear with their checkboxes unselected in the Output col-
umn. The purpose of the Output column is to control what goes into a query result
set.
38
3. To sort fields for the output, click inside a cell in the Sort Type column and choose
the sort order.
4. To define sort position for fields, click in a Sort Order cell and select the sort posi-
tion for the field. For example, if output should be sorted by Probability first, assign
1 to Probability.
5. Open the SQL Script tab to view the SQL generated for the query.
39
6. Click Execute Query to run the query.
ExampIe query resuIt
ExampIe: Union query
This example shows how to build a Union query using Opportunity and Oppor-
tunityLineItem tables. The left part of the union will be filtered by ForecastCategory =
Closed and the right part of the union will be filtered by ForecastCategory =Omitted.
1. Drop Opportunity and OpportunityLineItem tables onto the Main tab.
2. For Opportunity table, select "ads"."Opportunity"."Amount",
"ads"."Opportunity"."CloseDate", "ads"."Opportunity"."CreatedDate", "ads".-
"Opportunity"."ForecastCategory", "ads"."Opportunity"."IsWon"
3. For OpportunityLineItem table, select "ads".-
"OpportunityLineItem"."LastModifiedDate", "ads".-
"OpportunityLineItem"."TotalPrice", "ads"."OpportunityLineItem"."UnitPrice".
4. Click on the Criteria row for ForecastCategory and type `Closed
View of the left part of the union, once completed.
40
Next: Create Subquery
Create Subquery
To run a subquery:
1. Right-click Q and choose New union subqquery.
41
2. A new design pane displays for the right part of the union, initially empty, and Query
Builder switches to the new pane.
3. Repeat steps 1-4 of the Union Query, and this time type Omitted in the Criteria row
for the ForecastCategory field.
The union query is completed, and you can run it from Cloud9 Query Builder to test. Please
note that if you run a query for testing, a best practice is to limit result set (e.g. LIMIT 30).
Note: Cloud9 Query Builder supports other uber-join operations such as Except and Inter-
sect. To use them, right-click on the union icon between two Q s and choose a type of
uber-join.
42
Generated SQL
Trend From Date '2011-03-01' To Date '2011-08-23' By Interval '1' Day
Select "ads"."Opportunity"."Amount", "ads"."Opportunity"."CloseDate",
"ads"."Opportunity"."CreatedDate", "ads".-
"Opportunity"."ForecastCategory",
"ads"."Opportunity"."IsWon", "ads".-
"OpportunityLineItem"."LastModifiedDate",
"ads"."OpportunityLineItem"."TotalPrice", "ads".-
"OpportunityLineItem"."UnitPrice"
From "ads"."Opportunity" Inner Join
"ads"."OpportunityLineItem" On "ads"."Opportunity"."Id" =
"ads"."OpportunityLineItem"."OpportunityId"
Where "ads"."Opportunity"."ForecastCategory" = 'Closed'
Union
Trend From Date '2011-04-20' To Date '2011-08-23' By Interval '1' Day
Select "ads"."Opportunity"."Amount", "ads"."Opportunity"."CloseDate",
"ads"."Opportunity"."CreatedDate", "ads".-
"Opportunity"."ForecastCategory",
"ads"."Opportunity"."IsWon", "ads".-
"OpportunityLineItem"."LastModifiedDate",
"ads"."OpportunityLineItem"."TotalPrice", "ads".-
"OpportunityLineItem"."UnitPrice"
From "ads"."Opportunity" Inner Join
"ads"."OpportunityLineItem" On "ads"."Opportunity"."Id" =
"ads"."OpportunityLineItem"."OpportunityId"
Where "ads"."Opportunity"."ForecastCategory" = 'Omitted'
LIMIT 30

You might also like