Understanding Select Functions

Getting Started #

Below are the Functions available on the Select part when editing a query. They are grouped into the categories below.

  • Aggregates
    • Aggregate functions are used to perform calculations on a set of values such as summarizing data.
  • Text Adjustments
    • Text adjustment functions help clean, modify, and extract text data in a database.
  • Case Statements
    • The CASE statement allows you to apply conditional logic to your queries, similar to an “IF-THEN-ELSE” statement.
  • Special Functions
    • These functions are unique to TAD and not SQL related.

Aggregates #

Aggregate functions help summarize data by performing calculations on multiple rows and returning a single value. They are useful for finding totals, averages, and other key insights in a dataset. Below is a breakdown of common aggregate functions with real-world examples.

  • AVG
    • Returns the average (mean) value of a numeric column.
  • COUNT
    • Counts the total number of rows in a dataset.
  • COUNT DISTINCT
    • Counts the number of unique values in a column.
  • MAX
    • Returns the highest value in a column.
  • MIN
    • Returns the lowest value in a column.
  • SUM
    • Adds up all values in a numeric column.

AVG #

The AVG function calculates the average (mean) value of a numeric column.

Example Scenario:

You manage employee payroll and want to calculate the average salary amount for employees in a specific company.

Example Data:

EmployeeFirstNameLastNameSalaryAmt
1001JohnDoe50,000
1002AliceSmith75,000
1003BobJohnson90,000

Query Example:

This query calculates the average salary amount for employees in Company 1.

SELECT AVG(SalaryAmt) [SalaryAmt] FROM PREH WHERE PRCo=1

Result:

SalaryAmt
71,666.67

Use Case: Used for calculating average salaries, test scores, or transaction amounts.

COUNT #

The COUNT function returns the total number of rows in a dataset.

Example Scenario:

You want to find out how many invoices exist for customers in Company 1.

Example Data:

CustomerNameInvoice
2001Stellar Systems1001
2002Orion Dynamics1002
2003Quantum Services1003

Query Example:

SELECT COUNT(*) [TotalInvoices] FROM ARTH WHERE ARCo=1

Result:

TotalInvoices
3

Use Case: Useful for counting employees, customers, or transactions.

COUNT DISTINCT #

The COUNT DISTINCT function counts the number of unique values in a column.

Example Scenario:

You want to know how many unique states vendors are from.

Example Data:

VendorNameState
3001ABC CoNY
3002XYZ LtdIL
3003LMN IncNY

Query Example:

The COUNT function counts all rows, while COUNT DISTINCT only counts unique values. This query finds the number of unique states vendors are from.

SELECT COUNT(DISTINCT State) [UniqueStates] FROM APVM WHERE VendorGroup=1

Result:

UniqueStates
2

Use Case: Helps count the number of unique states vendors operate from, useful for regional analysis and compliance tracking.

MAX #

The MAX function returns the highest value in a column.

Example Scenario:

You want to find the highest check amount in a pay period to ensure that an employee’s rate was not entered incorrectly.

Example Data:

EmployeeFirstNameLastNameEarnings
1001JohnDoe1,500
1002AliceSmith2,200
1003BobJohnson3,000

Query Example:

SELECT MAX(Earnings) [LargestCheck] FROM PRSQ WHERE PRCo=1

Result:

HighestCheck
3,000

Use Case: Ensuring the highest check amount in a pay period is correct to detect any potential rate entry errors.

MIN #

The MIN function returns the lowest value in a column.

Example Scenario:

You want to find the minimum hourly rate from employee records to ensure compliance with wage policies.

Example Data:

EmployeeFirstNameLastNameHrlyRate
1001JohnDoe15.00
1002AliceSmith18.50
1003BobJohnson20.00

Query Example:

SELECT MIN(HrlyRate) [HrlyRate] FROM PREH WHERE PRCo=1

Result:

HrlyRate
15.00

Use Case: Identifying the lowest hourly rate in employee records to ensure compliance with wage policies.

SUM #

The SUM function adds up all values in a numeric column.

Example Scenario:

You want to summarize total billed revenue for jobs.

Example Data:

ContractDescriptionBilledAmt
5001Skyline Project5,000
5002Greenfield Build7,500
5003Oceanview Complex8,200

Query Example:

SELECT SUM(BilledAmt) [BilledAmt] FROM JCIP WHERE JCCo=1

Result:

BilledAmt
20,700

Use Case: Summarizing the total billed amounts in job revenue details to track financial performance and ensure accurate billing.

Text Adjustments #

Text adjustment functions help clean, modify, and extract text data in a database. These functions are useful for formatting names, standardizing data, and making searches more efficient.

  • ISNULL
    • Replaces NULL (missing) values with a default value.
  • LEFT
    • Extracts a specified number of characters from the left side of a text string.
  • LEN
    • Returns the number of characters in a text string.
  • LOWER
    • Converts all letters in a text string to lowercase.
  • LTRIM
    • Removes leading (left-side) spaces from a text string.
  • REPLACE
    • Replaces occurrences of a substring within a text value.
  • RIGHT
    • Extracts a specified number of characters from the right side of a text string.
  • RTRIM
    • Removes trailing (right-side) spaces from a text string.
  • SUBSTRING
    • Extracts a portion of a text string based on position.
  • TRIM
    • Removes both leading and trailing spaces from a text string.
  • UPPER
    • Converts all letters in a text string to uppercase.

ISNULL #

The ISNULL function replaces NULL (missing) values with a specified default value.

Example Scenario:

You have a list of customers, but some phone numbers are missing.

Example Data:

CustomerNamePhone
1000ABC Company LLC555-1234
1001Space Trade Inc.NULL
1002Quantum Leap Ltd.555-5678

Query Example:

SELECT Customer, Name, ISNULL(Phone, 'No Number') [Phone Number] FROM ARCM WHERE CustGroup=1

Result:

CustomerNamePhone Number
1000ABC Company LLC555-1234
1001Space Trade Inc.No Number
1002Quantum Leap Ltd.555-5678

Use Case: Ensures missing data is replaced with meaningful text.

LEFT #

The LEFT function returns a specified number of characters from the beginning (left side) of a text value.

Example Scenario:

You have material codes, but you only need the first 3 characters.

Example Data:

CategoryMaterial
LaptopLAP123
PhonePHO456
TabletTAB789

Query Example:

SELECT Category, LEFT(Material, 3) [ShortMatl] FROM HQMT WHERE MatlGroup=1

Result:

CategoryMaterial
LaptopLAP
PhonePHO
TabletTAB

Use Case: Extracting prefixes, country codes, or area codes.

LEN #

The LEN function returns the number of characters in a text string, including spaces.

Example Scenario:

You want to check the length of usernames to ensure they meet a required length.

Example Data:

VPUserName
JohnDoe
Alice123
Bob

Query Example:

SELECT VPUserName, LEN(VPUserName) [Length] FROM DDUP

Result:

VPUserNameLength
JohnDoe7
Alice1238
Bob3

Use Case: Validating passwords, checking data consistency.

LOWER #

The LOWER function converts all letters in a text value to lowercase.

Example Scenario:

You want to ensure all email addresses are stored in lowercase for consistency.

Example Data:

EMail
John@Gmail.com
ALICE@Yahoo.com
BoB@OUTlook.com

Query Example:

SELECT LOWER(EMail) [StandardizedEmail] FROM DDUP

Result:

StandardizedEmail
john@gmail.com
alice@yahoo.com
bob@outlook.com

Use Case: Standardizing email addresses, usernames, and search terms.

LTRIM #

The LTRIM function removes any leading (left-side) spaces from a text value.

Example Scenario:

You have a dataset where names have extra spaces at the beginning.

Example Data:

FirstName
” John”
” Alice”
” Bob”

Query Example:

SELECT LTRIM(FirstName) [CleanedName] FROM PREH WHERE PRCo=1

Result:

CleanedName
“John”
“Alice”
“Bob”

Use Case: Cleaning up messy text input.

REPLACE #

The REPLACE function finds and replaces occurrences of a substring within a text value.

Example Scenario:

You need to change all instances of “Street” to “St.” in addresses.

Example Data:

Address1
123 Main Street
456 Elm Street
789 Oak Street

Query Example:

SELECT REPLACE(Address1, 'Street', 'St.') [UpdatedAddress] FROM ARCM WHERE CustGroup=1

Result:

UpdatedAddress
123 Main St.
456 Elm St.
789 Oak St.

Use Case: Fixing spelling errors, updating product names.

RIGHT #

The RIGHT function returns a specified number of characters from the end (right side) of a text value.

Example Scenario:

You have phone numbers and only need the last 4 digits.

Example Data:

Phone
555-1234
555-5678
555-9999

Query Example:

SELECT RIGHT(Phone, 4) [Last4Digits] FROM APVM WHERE VendorGroup=1

Result:

Last4Digits
1234
5678
9999

Use Case: Extracting last digits of IDs, codes, or phone numbers.

RTRIM #

The RTRIM function removes any trailing (right-side) spaces from a text value.

Example Scenario:

You have a dataset where names have extra spaces at the end.

Example Data:

FirstName
“John “
“Alice “
“Bob “

Query Example:

SELECT RTRIM(FirstName) [CleanedName] FROM PREH WHERE PRCo=1

Result:

CleanedName
“John”
“Alice”
“Bob”

Use Case: Cleaning up data before displaying it.

SUBSTRING #

The SUBSTRING function extracts a portion of a text value based on position.

Example Scenario:

You have material codes, and you need to extract the middle portion.

Example Data:

Material
ABC-1234-XYZ
DEF-5678-PQR
GHI-9999-UVW

Query Example:

SELECT SUBSTRING(Material, 5, 4) [ExtractedMatl] FROM HQMT WHERE MatlGroup=1

Result:

ExtractedMatl
1234
5678
9999

Use Case: Extracting ID numbers, part of names, or keywords.

TRIM #

The TRIM function removes both leading and trailing spaces from a text value.

Example Scenario:

You receive messy user input where names have extra spaces.

Example Data:

FirstName
” John “
” Alice “
” Bob “

Query Example:

SELECT TRIM(FirstName) [CleanedName] FROM PREH WHERE PRCo=1

Result:

CleanedName
“John”
“Alice”
“Bob”

Use Case: Cleaning up messy text input.

UPPER #

The UPPER function converts all text to uppercase.

Example Scenario:

You want all last names in uppercase for uniformity.

Example Data:

LastName
smith
Johnson
O’brien

Query Example:

SELECT UPPER(LastName) [StandardizedName] FROM PREH WHERE PRCo=1

Result:

StandardizedName
SMITH
JOHNSON
O’BRIEN

Use Case: Standardizing names, formatting for reports.

Case Statements #

The CASE statement in SQL allows you to apply conditional logic to your queries, similar to an “IF-THEN-ELSE” statement in programming. It is useful for categorizing, transforming, and calculating values dynamically within a query.

The CASE statement evaluates conditions and returns a specific value when a condition is met. If no conditions match, a default value can be returned using ELSE.

Basic Syntax

CASE  
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
END
  • The statement checks each WHEN condition one by one.
  • If a condition is TRUE, it returns the corresponding THEN value.
  • If none of the conditions are met, it returns the ELSE value (if provided).

Example Scenario

You have employee salaries and want to classify them as Low, Medium, or High Salary based on salary brackets.

Example Data:

EmployeeSalaryAmt
John30,000
Alice60,000
Bob90,000

Query Example:

SELECT Employee, SalaryAmt,  
CASE
WHEN SalaryAmt
< 40000 THEN 'Low'
WHEN SalaryAmt
BETWEEN 40000 AND 80000 THEN 'Medium'
ELSE 'High'
END AS SalaryCategory
FROM PREH

WHERE PRCo=1

Result:

EmployeeSalaryAmtSalaryCategory
John30,000Low
Alice60,000Medium
Bob90,000High

Use Case: Used for grouping salaries, pricing tiers, and other classifications.

Special Functions #

These functions are unique to TAD and not SQL related.

  • BLANK TOP
    • Adds a blank column to the top of the Selected Columns which can be used for spacing between other columns.
  • BLANK BOTTOM
    • Adds a blank column to the bottom of the Selected Columns which can be used for spacing between other columns.
  • EXCEL FORMULAS
    • This function allows you to enter an Excel formula which will generate when data is downloaded.
  • COPY RESULTS
    • This function doubles the data output which is useful for creating balanced entries in GL Journal Transaction Entry and JC Cost Adjustments.