Checking which of two column values is closest to a calculated value - sql

Absolute rookie at SQL so apologies upfront if not possible or absurd.
Single table in SQL-Lite
First of all I want to filter the table to only return rows where the difference between decimal in column A and decimal in column B is more than 3
Then for each row I want to subtract integer in column C from integer in column D to give result E. And then I want to know whether the decimal in column A or decimal in column B is closer to result E
Thanks!

The code below basically uses a subquery to keep all the needed values handy, a CASE operator to make the decision, and the ABS() function to determine absolute distance.
select A, B, C, D, E,
case when ABS(A-E) < abs(B-E) then 'A' else 'B' end [Closer_Value]
from (
select A, B, C, D, (C-D) as [E]
from YourTable
where abs(A-B) > 3
) as Temp

Related

How to bring rounded integer as output by multiplying two columns in SQL

I have two columns A and B in SQL. Column A has value 5.2 and Column B has value 2.1. I am multiplying column A and B in Column C. Answer is 10.92. I would like to get rounded number 11 as Integer in column C instead of 10.92 as float or decimal. How should I write query. Should there be a Cast function or similar?
Sample query is:
SELECT *, A multiply B As C
FROM Table X
You need query like this:
select *, cast(round((a*b), 0) as int) from X
Here you can see on DB FIDDLE
select round((columnA*columnB),0)
from Table

For each value in col A finding number of values in column B that are greater than it

Let's say I have a table with 2 columns - A & B.
Using plain SQL (No scripts/cursors etc.), how do I (window function?) calculate for EACH value in column A the number of values in column B that are bigger/smaller than it?
Thanks you.
You would use conditional aggregation:
select a,
sum(case when b < a then 1 else 0 end)
from t
group by a;
Window functions don't seem appropriate to this question.

postrgresql row by row comparison of values in two columns

I have a postgres db where after a GROUPBY I have to evaluate entries approximated by this table:
I then try to find the MIN and MAX of each column and count the number of rows where there isn't a zero in either column 'A' or 'B'. In this case I count one row because in row '4' there are non-zero values in both column 'A' and 'B'. Getting MIN and MAX is straightforward but I can't figure out how to do the last step.
SELECT MIN(A) as "minA",
MAX(A) as "maxA",
MIN(B) as "minB",
MAX(B) as "maxB",
COUNT(????) as "num_full"
FROM bigDB
GROUPBY inlet
I thought maybe I could do a sum on each row and test if the result was equal to the value of 'A' or 'B' i.e. if A or B is zero then the sum is A or B. But sum() works by column not row. Is there a way to do sums by row or is there a better way to do what I want to do?
Use filter:
COUNT(*) FILTER (WHERE A <> 0 AND B <> 0) as num_full
There is no need to enclose column aliases in double quotes.

creating a new column with conditional group by

I am trying to figure a way to sum a column based on conditions are create a new column with the aggregated values.
I need to sum column D based based on column condition from Column A, B and C. Value 1 in column A is the exception. The output that is required is as follows -
Where a new column E is created that sums Column D(2958673+2166646) by the condition of Column A(10,12) and Column B(20) and Column C(3) in Row 1. Similarly, Column D(1799504) by the condition of Column A(12) and Column B(20) and Column C(4) in Row 2
This is perhaps a comment, but it is too long.
I simply do not follow the logic that you want to implement. Your results can be produced by a simple query with filtering:
select a, b, c, d as d, d as e
from t
where a = 1;
EDIT:
Perhaps this is what you want?
select 1, b, c,
sum(d) filter (where a = 1) as d,
sum(d) filter (where a in (10, 12)) as e
from t
group by b, c;

get the last column before null columns sql

How can I retrieve the last non-null value in a row of a table in DB?
Assuming values are non-nulls and then all nulls.
For example, assume there are 5 columns in a table (A B C D E). Here is a sample row data:
A B C D E
1 2 NULL NULL NULL
1 2 3 NULL NULL
I need to get for 1st row - value 2, for second row - value 3. Rows are processed in a cursor.
Thank you
Just use coalesce():
select coalesce(e, d, c, b, a) as last_not_null
from t;
Sql do have a inbuilt function COALESCE for non-null expression in the list.
The COALESCE() function returns the first non-null expression in a list.