Creating Selections with SQL
Various methods of selecting network objects have been described previously in this tutorial. However, selections can also be made using SQL queries, which allow you to create lists of objects that satisfy specified conditions. The conditions can be as simple or as complex as you like. For example, you can create a list of nodes of a particular type or links for which particular fields lie within specified ranges.
Lists generated with SQL queries can be used to simplify the network.
This stage assumes that you have started InfoWorks ICM and that Master database, containing the ICM Basic Tutorial, is opened in the Explorer window, showing the 'Langley Catchment Study' model group, and that the 'Langley Network TUT' network is open in the GeoPlan.
The tutorial dataset uses InfoWorks native units (metric units). To import the tutorial data correctly, set the user units to metric (if not already set) by selecting Options from the Tools menu and clicking the Metric (Native) button on the Units tab.
This part of the tutorial describes how to use SQL to create selection lists containing:
- All Subcatchments in the network
- All nodes that are not manholes
- All pipes that are less than 250 mm wide
- All pipes that are less than 250 mm wide and less than 10 m long
It also describes how to view the results of a query as a grid.
Subcatchments
To create a selection list of all subcatchments in the currently selected network:
- Click the (SQL select) tool on the Selection toolbar. The SQL Dialog is displayed.
- Ensure that the SQL tab is opened (default).
- Select the Subcatchment option from the drop-down list in the Object Type field.
- Select the subcatchment_id option from the Field drop-down list.
- Click the Builder button. The dialog is expanded to show the operations that can be included when building the SQL query.
- Click the > button.
- Type 0 into the Constant field.
- Click the OK button which appears under the Constant field. The SQL query is now complete.
- Click the Save As... button. A Save as dialog appears with a default name 'SQL' displayed at the top of the window.
- Change the default name from 'SQL' to 'Subcatchments'.
- Open the 'Tutorial' and 'Langley Catchment Study' items from the tree.
- Click the 'Stored Query Group'. The Save button is then enabled.
- Click Save to add the new 'Subcatchments' query to the group. The Save As dialog is closed, and the 'Subcatchments' query is now displayed in the master database tree in the Explorer window.
- In the SQL dialog, click Run. The dialog is closed and all objects matching the query condition are selected (i.e. all those that have a Subcatchment ID). If required, right-click an empty area of the GeoPlan, and, from the pop-up menu, select Zoom to network to maximise the display.
- Right-click the 'Selection List Group' in the Explorer window, and choose New InfoWorks | Selection List from the pop-up menu, and a New Name dialog is displayed.
- Name the list 'Subcatchments (SQL)' and click OK. The window closes and the 'Subcatchments (SQL)' selection list is now displayed in the master database tree in the Explorer window.
Nodes that are not manholes
To create a selection list, in the currently selected network, for all nodes that are not manholes:
- Clear the current selection by clicking on the Selection toolbar.
- Click the (SQL select) tool. The SQL Dialog is displayed.
- Ensure that the SQL tab is opened (default).
- Select the All Nodes option from the drop-down list in the Object Type field.
- Select the node_type option from the Field drop-down list.
- Click the Builder button. The SQL dialog is expanded to show the operations that can be included when building the SQL query.
- Click the <> (not equal) button.
- Type 'manhole' (including the single quotes) into the Constant field.
- Click the OK button which appears under the Constant field.
- Click Test. A window showing the number of objects which match the criteria is displayed.
- Click OK.
- Click the Save As... button. A Save as dialog appears.
- Save the query in the 'Nodes - Not Manholes' item in the Stored Query Group.
- Click Run and the selected nodes are highlighted in the network.
- Right-click an empty area of the GeoPlan and, from the pop-up menu, select Zoom to selection to maximise the display.
-
Open the Nodes grid view by selecting the Grid windows | New nodes window option from the Windows menu, and sort the records in descending order of Node Type.
The selected nodes are listed at the top, which confirms that the list is accurate and provides an alternative method for generating the selection.
- Click to close the grid view.
Pipes less than 250 mm wide
You can use any of the data fields in a query. For example, to create a list of pipes that are less than 250 mm wide:
- Clear the current selection by clicking on the Selection toolbar.
- Click the (SQL select) tool. The SQL Dialog is displayed.
- Ensure that the SQL tab is opened (default).
- Select the Conduit option from the drop-down list in the Object Type field.
- Select the Conduit_width (Width) option from the Field drop-down list.
- Enter the condition: 'conduit_width < 250'.
- Click Test. A window showing the number of objects that match the criteria is displayed.
- Click OK.
- Click the Save As... button. A Save as dialog appears.
- Save the query as 'Pipes < 250 mm'.
- Click Run to run the query, and the pipes which match the '< 250 mm' query are displayed in the GeoPlan.
- Save the selection in the Selection List Group as 'Pipes < 250 mm'.
Pipes less than 250 mm wide and 10 m long
The GeoPlan tools and grids can achieve the same effects as simple SQL queries. However, more complex SQL queries can be designed to select objects according to a combination of criteria. For example, to extend the previous query to limit the search to pipes less than 250 mm wide and 10 m long:
- Make a copy of the last query:
- Right-click the 'Pipes < 250 mm' query (in the Stored Query Group) and choose Copy from the pop-up menu.
- Right-click the 'Stored Query Group' and select Paste.
- Right-click the new query ('Pipes < 250 mm!') and choose Rename. A Rename dialog appears.
- Change the name to 'Minor Pipes' and click OK.
- Clear the current selection by clicking on the Selection toolbar.
- Drag the 'Minor Pipes' query onto the InfoWorks ICM background (not the GeoPlan). The query is reloaded in the SQL Dialog.
- Click the Builder button.
- Extend the condition, either by using the buttons on the dialog or by typing the condition directly: conduit_width < 250 OR conduit_length < 10.
When entering expressions (either directly or using the Builder options), make sure you leave a space either side of the operators (Arithmetic, Text, Comparison and Logic.
- Click Test. A window showing that there are now more matching objects, which have either a width less than 250 mm or length less than 10 metres, is displayed.
- Click OK.
- Click Save to re-save the query.
- Click Run to run the query, and the pipes that match the query are displayed in the GeoPlan.
- Click Save to save the query.
- Save the selection in the Selection List Group as 'Minor Pipes'.
- To view the results of a query as a grid:
- Click to clear the current selection.
- Drag the' Minor Pipes' query onto the InfoWorks ICM background.
- Click the Grid tab.
- Hold down Ctrl and click the 'us_node_id (US node ID)', 'link_suffix (Link suffix)', 'conduit_length Llength)' and 'conduit_width (Width)' fields.
- Click Include.
- Click Test Grid. The query is run and the Conduit grid view is loaded.
- Close the SQL dialog.
- Close all the windows, including the GeoPlan window.
Lists generated with SQL queries can be used to simplify the network.