Chapter Contents

Previous

Next
SAS SQL Query Window User's Guide

Counting and Grouping Data Automatically

You can count and report the total number of rows that have the same value for one or more columns. You can use the automatic group by feature to group the values according to their columns.

The following query displays the number of employees in each division.

In the SQL QUERY TABLES window, select SAMPLE.EMPINFO from the list of Available Tables and add it to the list of Selected Tables. Select OK.

In the SQL QUERY COLUMNS window, select DIVISION and < COUNT(*) > from the Available Columns list and add them to the list of Selected Columns.


Count

Select COUNT(*) from the Selected Columns List. Select Move After to move the column. Reselect COUNT (*). Select COLUMN ALIAS/LABEL. Type Count of Employees for Each Division in the LABEL field of the Column Alias and Label window.

[IMAGE]

Select OK.


Grouping Columns Automatically

Select

Tools
[arrow]
Run Query
[arrow]
Run Immediate

A dialog is displayed.

[IMAGE]

Select AUTOGROUP to automatically select the correct columns. Selected columns that do not have summary functions applied to them will be the group(s) that the summary functions are computed for.

A second dialog is displayed.

[IMAGE]

Select NO. The automatic Group By clause will be part of the query syntax while the query runs, but will not be retained. You can select or remove columns after the query is executed and use AUTOGROUP to automatically select the columns again.

The count of employees for each division is displayed in the OUTPUT window.

[IMAGE]

In the SQL QUERY COLUMNS window, select

Tools
[arrow]
Reset

to reset your query. A dialog appears.

[IMAGE]

Select OK to return to the SQL QUERY TABLES window. The components of the current query are cleared.


Automatic Group By with More than One Table

The next query joins two tables to display the number of employees for each job title. The JOBCODES table contains the job title for each job code.

Select SAMPLE.JOBCODES and SAMPLE.EMPINFO from the list of Available Tables and add them to the list of Selected Tables.

Select OK.

In the SQL QUERY COLUMNS window, select TITLE and < COUNT(*) > from the list of Available Columns and add them to the list of Selected Columns.

Select

View
[arrow]
Where Conditions for Subset...

In the WHERE EXPRESSION window, select EMPINFO.JOBCODE from the Available Columns list. Select EQ from the list of comparison operators.

[IMAGE]

Select JOBCODES.JOBCODE from the Available Columns List.

[IMAGE]

Select OK to return to SQL QUERY COLUMNS window.

Select COUNT(*) from the Selected Columns List. Select Move After to move the column. Reselect COUNT (*). Select Column Alias/Label. Type Count of Employees for Each Title in the LABEL field of the Column Alias and Label window.

[IMAGE]

Select OK.


Retaining an Automatic Group By as Part of a Query

Select

Tools
[arrow]
Run Query
[arrow]
Run Immediate

A dialog is displayed. Select AUTOGROUP in the dialog window to use JOBCODES.TITLE as the Group By column. A second dialog is displayed. Select YES in the second dialog window to retain the Group By column as part of the query.

The OUTPUT window displays the number of employees for each job title.

[IMAGE]

In the SQL QUERY COLUMNS window, select

Tools
[arrow]
Show Query...

[IMAGE]

The automatic Group By will be retained as part of the query syntax when the query is run again, saved, or used to create a table or view. Select Goback to return to the SQL QUERY COLUMNS window.

In the SQL QUERY COLUMNS window, select

File
[arrow]
Save Query
[arrow]
Save as Query to Include later

In the Entry Name field, type COUNTS as the name of the query. In the Enter a description for the query field type COUNT OF EMPINFO BY TITLE. Select OK to save the query and return to the SQL QUERY COLUMNS window.

Select

View
[arrow]
Tables...

to return to the SQL QUERY TABLES window. Remove SALARY.JOBCODES from the list of Selected Tables. Select OK in the pop-up dialog.


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.