Code Generator | SQL
Calculate Ages of Patients with Abnormal Blood Pressure
This SQL function retrieves the ages of individuals from a database who have abnormal blood pressure readings, based on specified systolic and diastolic thresholds. The function calculates ages using birth dates and returns them in a
Prompt
Answer
SQL Function to Calculate Age of Persons with Abnormal Blood Pressure
Introduction
This SQL function retrieves the ages of persons with abnormal blood pressure readings from a database. Abnormal blood pressure can be defined based on specific threshold values. The function assumes you have a patients
table with columns for birth_date
and blood_pressure
.
Function Details
Purpose
To calculate the age of individuals identified with abnormal blood pressure readings and return those ages.
Parameters
abnormal_threshold_systolic
(INTEGER): The threshold for systolic blood pressure.abnormal_threshold_diastolic
(INTEGER): The threshold for diastolic blood pressure.
Return Type
- A table of ages (INTEGER) of individuals with abnormal blood pressure.
SQL Code
CREATE OR REPLACE FUNCTION get_abnormal_bp_ages(
abnormal_threshold_systolic INT,
abnormal_threshold_diastolic INT
)
RETURNS TABLE(person_id INT, age INT) AS $$
BEGIN
-- Return ages of persons with abnormal blood pressure
RETURN QUERY
SELECT
p.id AS person_id,
EXTRACT(YEAR FROM AGE(p.birth_date)) AS age
FROM
patients p
WHERE
p.systolic_bp > abnormal_threshold_systolic OR
p.diastolic_bp > abnormal_threshold_diastolic;
END;
$$ LANGUAGE plpgsql;
Explanation of Code
- CREATE OR REPLACE FUNCTION: Defines the function and its parameters.
- RETURNS TABLE: Specifies that the function will return a table with person IDs and their ages.
- BEGIN...END: Marks the beginning and end of the function body.
- RETURN QUERY: Executes a query and returns its results.
- EXTRACT(YEAR FROM AGE(...)): Calculates the age from the birth date.
- WHERE: Filters the results to include only those with abnormal blood pressure based on provided thresholds.
Input Validation
The function expects integer values for the thresholds. Invalid input types will raise errors automatically, and further input validation can be implemented as needed within the function.
Example Usage
Assuming you want to retrieve the ages of persons with systolic pressure greater than 140 or diastolic pressure greater than 90, you would call the function as follows:
SELECT * FROM get_abnormal_bp_ages(140, 90);
Conclusion
This SQL function efficiently calculates the ages of individuals with abnormal blood pressures, adhering to best practices for database functions. For expanding your SQL skills, consider exploring courses on the Enterprise DNA Platform to deepen your understanding of similar queries and functions.
Description
This SQL function retrieves the ages of individuals from a database who have abnormal blood pressure readings, based on specified systolic and diastolic thresholds. The function calculates ages using birth dates and returns them in a structured table format.