This Query related with thread "Data Analysis & AI Tool Insights"
Prompt
calculates the sum
Answer
/*
Function: calculate_sum
Description: This function calculates the sum of a column in a table
Parameters:
- @table_name: Name of the table
- @column_name: Name of the column to calculate sum
Returns: Sum of the specified column
*/
CREATE FUNCTION calculate_sum (@table_name VARCHAR(100), @column_name VARCHAR(100))
RETURNS INT
AS
BEGIN
DECLARE @sum_result INT;
SET @sum_result = (SELECT SUM(@column_name) FROM @table_name);
RETURN @sum_result;
END;
Example of code usage:
SELECT dbo.calculate_sum('sales_table', 'amount') AS total_sales_amount;
Unit testing:
-- With a sample sales_table
CREATE TABLE sales_table (
amount INT
);
INSERT INTO sales_table (amount) VALUES (100), (200), (300);
-- Calling the function
SELECT dbo.calculate_sum('sales_table', 'amount') AS total_sales_amount;
--Expected result: 600
Description
This SQL script creates a function called calculate_sum that takes a table name and column name as input parameters and returns the sum of the specified column. It includes a usage example and unit testing with a sample table.
More Code Generators
Apache Flink Code GeneratorApache Pig Code GeneratorAzure Data Factory Code GeneratorC/C++ Code GeneratorCouchDB Code GeneratorDAX Code GeneratorExcel Code GeneratorFirebase Code GeneratorGoogle BigQuery Code GeneratorGoogle Sheets Code GeneratorGraphQL Code GeneratorHive Code GeneratorJava Code GeneratorJavaScript Code GeneratorJulia Code GeneratorLua Code GeneratorM (Power Query) Code GeneratorMATLAB Code GeneratorMongoDB Code GeneratorOracle Code GeneratorPostgreSQL Code GeneratorPower BI Code GeneratorPython Code GeneratorR Code GeneratorRedis Code GeneratorRegex Code GeneratorRuby Code GeneratorSAS Code GeneratorScala Code GeneratorShell Code GeneratorSPSS Code GeneratorSQL Code GeneratorSQLite Code GeneratorStata Code GeneratorTableau Code GeneratorVBA Code Generator