You can automatically populate the Having clause in the SQL generated by the Query Builder in one of two ways.
Note: To create a HAVING clause, you must have added columns to the GROUP BY node.
HAVING entries should be in the form of <expression1> <operator> <expression2>.
To populate the HAVING clause
- Do one of the following:
- Right-click the desired column in the tree, and then select Include in Having Clause.
- Drag a column from a table in the Table Model area to the HAVING clause.
- Enter or build the condition.
- Click OK.
- Repeat until complete.
In order to include a global HAVING clause, there must be a GROUP BY clause as well.
To create a global HAVING clause
- Right-click in the Table Model area.
- Select SQL | Global Having.
- Click the Add button.
- Enter or build your condition.
- Click OK to close the definition window.
- Click OK.
Example
To construct the following query:
SELECT emp.empno, emp.ename, emp.job, emp.mgr, emp.sal,
emp.comm, emp.deptno
FROM emp
GROUP BY emp.deptno, emp.comm, emp.sal, emp.mgr, emp.job,
emp.ename, emp.empno
HAVING ((emp.sal + NVL (emp.comm, 0)> 4000))
Do the following:
- Open the Query Builder.
- In the Object Palette, select the Scott schema.
- Double-click the EMP table to add it to the model.
- Right-click EMP and choose Select All, then clear Hiredate.
- Drag DEPTNO, COMM, SAL, MGR, JOB, ENAME and EMPNO to the Group by node.
- Right-click in the Table model area and select SQL | Global Having. Click to add a new Global Having clause.
Enter the Having clause to say:
EMP.SAL + NVL(EMP.COMM, 0) > 4000
- Click OK twice.
View the generated query. It should display as described above. This query selects all the employees whose salary plus commission is greater than 4000.
The NVL command substitutes a null value in the specified column with the specified value, in this case, 0.