The Flat Earth Society
Other Discussion Boards => Technology, Science & Alt Science => Topic started by: FlatAssembler on May 25, 2022, 04:55:15 AM
-
In 2018, on a test about databases (which I have failed multiple times) at my university, they had this task:
Napisati T-SQL funkciju koja prima 3 realna broja, te kao rezultat vraća onaj broj koji ima najveću apsolutnu vrijednost. Ako slučajno istu najveću apsolutnu vrijednost imaju dva ili tri broja potrebno je vratiti zbroj tih brojeva.
That is:
Write a T-SQL function that accepts 3 real numbers, and as a result it returns the number that has the biggest absolute value. If accidentally the same biggest absolute value is had by two or three numbers it is necessary to return the sum of those numbers.
How would you solve that?
Here is my attempt, I do not know if it is correct nor how to actually test it on my computer:
CREATE FUNCTION max_abs3(@x, @y, @z DECIMAL) RETURNS DECIMAL
BEGIN
IF ABS(@x)=ABS(@y) AND ABS(@y)=ABS(@z)
BEGIN
RETURN @x+@y+@z;
END;
IF ABS(@x)=ABS(@y) AND ABS(@x)>ABS(@z)
BEGIN
RETURN @x+@y;
END;
IF ABS(@x)=ABS(@z) AND ABS(@x)>ABS(@y)
BEGIN
RETURN @x+@z;
END;
IF ABS(@y)=ABS(@z) AND ABS(@y)>ABS(@x)
BEGIN
RETURN @y+@z;
END;
IF ABS(@x)>ABS(@y) AND ABS(@x)>ABS(@z)
BEGIN
RETURN @x;
END;
IF ABS(@y)>ABS(@z) AND ABS(@y)>ABS(@x)
RETURN @y;
END;
RETURN @z;
END;
I have only managed to install SQLite on my computer, and it apparently does not support custom SQL functions.
-
In 2018, on a test about databases (which I have failed multiple times) at my university, they had this task:
Napisati T-SQL funkciju koja prima 3 realna broja, te kao rezultat vraća onaj broj koji ima najveću apsolutnu vrijednost. Ako slučajno istu najveću apsolutnu vrijednost imaju dva ili tri broja potrebno je vratiti zbroj tih brojeva.
That is:
Write a T-SQL function that accepts 3 real numbers, and as a result it returns the number that has the biggest absolute value. If accidentally the same biggest absolute value is had by two or three numbers it is necessary to return the sum of those numbers.
How would you solve that?
Here is my attempt, I do not know if it is correct nor how to actually test it on my computer:
CREATE FUNCTION max_abs3(@x, @y, @z DECIMAL) RETURNS DECIMAL
BEGIN
IF ABS(@x)=ABS(@y) AND ABS(@y)=ABS(@z)
BEGIN
RETURN @x+@y+@z;
END;
IF ABS(@x)=ABS(@y) AND ABS(@x)>ABS(@z)
BEGIN
RETURN @x+@y;
END;
IF ABS(@x)=ABS(@z) AND ABS(@x)>ABS(@y)
BEGIN
RETURN @x+@z;
END;
IF ABS(@y)=ABS(@z) AND ABS(@y)>ABS(@x)
BEGIN
RETURN @y+@z;
END;
IF ABS(@x)>ABS(@y) AND ABS(@x)>ABS(@z)
BEGIN
RETURN @x;
END;
IF ABS(@y)>ABS(@z) AND ABS(@y)>ABS(@x)
RETURN @y;
END;
RETURN @z;
END;
I have only managed to install SQLite on my computer, and it apparently does not support custom SQL functions.
Get Microsoft SQL Express. It's free
I would also write it using SQL CASE. https://www.w3schools.com/sql/sql_case.asp
-
Download the SQL Server Management studio to run it
https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver16 (https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver16)
The other way you could do it is with CASE WHEN statements
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
-
Barring the other very fine suggestions, you could also do it in MySQL/MariaDB. It's FOSS, all it costs you is time and storage space.