A question about databases

  • 3 Replies
  • 715 Views
*

FlatAssembler

  • 668
  • Not a FE-er
A question about databases
« 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:
Quote
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:
Quote
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:
Code: [Select]
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.
Fan of Stephen Wolfram.
This is my parody of the conspiracy theorists:
https://www.theflatearthsociety.org/forum/index.php?topic=71184.0
This is my attempt to refute the Flat-Earth theory:

*

NotSoSkeptical

  • 8548
  • Flat like a droplet of water.
Re: A question about databases
« Reply #1 on: May 25, 2022, 05:26:46 AM »
In 2018, on a test about databases (which I have failed multiple times) at my university, they had this task:
Quote
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:
Quote
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:
Code: [Select]
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
« Last Edit: May 25, 2022, 05:31:07 AM by NotSoSkeptical »
Rabinoz RIP

That would put you in the same category as pedophile perverts like John Davis, NSS, robots like Stash, Shifter, and victimized kids like Alexey.

Re: A question about databases
« Reply #2 on: May 25, 2022, 06:24:14 AM »
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

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;


Quote from: mikeman7918
a single photon can pass through two sluts

Quote from: Chicken Fried Clucker
if Donald Trump stuck his penis in me after trying on clothes I would have that date and time burned in my head.

*

boydster

  • Assistant to the Regional Manager
  • Planar Moderator
  • 17754
Re: A question about databases
« Reply #3 on: May 25, 2022, 01:47:15 PM »
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.