How to filter Even and Odd numbers and move/copy them to an empty column? - sql

i have a simple problem. i have a column with numbers. i need to filter them to even and odd numbers, but if a number is even than i need to copy or move it to "even-number-column" in the same table. if the number is odd then move it to "odd-number-column" in the same table.
this is my code:
select distinct all_numbers, even-number-column, odd-number-column
case when all_numbers%2=0
then UPDATE my_table SET even-number-column = all_numbers
else UPDATE my_table SET odd-number-column = all_numbers
end
from my_table ;

If you want a SELECT:
select
distinct all_numbers,
CASE
WHEN when all_numbers%2 = 0 THEN all_numbers
ELSE 0 -- or NULL
END as even-number-column,
CASE
WHEN when all_numbers%2 = 1 THEN all_numbers
ELSE 0 -- or NULL
END as odd-number-column
from my_table ;`
If you want an update
UPDATE my_table
SET
even-number-column = CASE
WHEN when all_numbers%2 = 0 THEN all_numbers
ELSE even-number-column -- or change for 0 or NULL
END,
odd-number-column = CASE
WHEN when all_numbers%2 = 1 THEN all_numbers
ELSE odd-number-column -- or change for 0 or NULL
END

Use an array to make it simple
update t
set
odd = (array[null, all_numbers])[all_numbers % 2 + 1],
even = (array[all_numbers, null])[all_numbers % 2 + 1]

Related

Hi, How to update negative values as Zero in Snowflake?

CASE
WHEN round(months_between(CURRENT_DATE, nvl(bpi.StartDate, cli.StartDate))) > round(months_between(nvl(bpi.EndDate, cli.EndDate), nvl(bpi.StartDate, cli.StartDate)))
THEN round(months_between(nvl(bpi.EndDate, cli.EndDate), nvl(bpi.StartDate, cli.StartDate)))
ELSE round(months_between(CURRENT_DATE, nvl(bpi.StartDate, cli.StartDate)), 1)
END AS Months_Used
Using update:
UPDATE tab
SET col = 0
WHERE col < 0;
Alternatively without actual change in base table using CASE expression:
SELECT CASE WHEN col < 0 THEN 0 ELSE col END AS col
FROM tab;

way to have query with case statement

I have table called Numbers in that column I have values from 0 - 10 but I like to keep value of 1-10 only change record of 0 too null
Case numbers
when 0
then ''
but I found this has changed all values and not values that have 0 is there way I can say else leave value as is?
Do you want this?
update t
set number = null
where number = 0;
Or as a select:
select t.*,
(case when number <> 0 then number end)
from t;
SELECT CASE WHEN [column] = 0 THEN NULL ELSE [column] END AS [SomeName]
FROM Numbers

how to update one record column to true, all else false

I am trying to update a column in a record to true to indicate that the record is the one active in the table. However, by updating this record, I must then update all other records for that column to false. Is there a way to do this in one SQL statement? I can do it in two statements like this:
UPDATE My_Table
SET is_group_active = 0
UPDATE My_Table
SET is_group_active = 1
WHERE group_id = 2;
You could use a case expression:
UPDATE my_table
SET is_group_active = CASE group_id WHEN 2 THEN 1 ELSE 0 END;
I would write this as:
UPDATE t
SET is_group_active = (CASE group_id WHEN 2 THEN 1 ELSE 0 END)
WHERE is_group_active <> (CASE group_id WHEN 2 THEN 1 ELSE 0 END);
Or perhaps:
UPDATE t
SET is_group_active = (CASE group_id WHEN 2 THEN 1 ELSE 0 END)
WHERE is_group_active = 1 OR group_id = 2
There is no need to update rows that already have the correct value. (Note: The logic would be slightly more complicated if is_group_active can take on NULL values).

How to execute multiple case when statements for each iteration in SAS?

I'm using proc sql, and using multiple case when statements to add columns with either a 0 or 1 if the condition is met. It's a big bottleneck right now since it has to scan through each id for each case when statement. So I'm trying to figure out a way to somehow nest the case statements to perform each iteration, instead of having to iterate for all case statements.
This is an example of my code that is taking too long right now.
SELECT *,
CASE WHEN loannumber IN (
SELECT loannumber FROM PREPAY_LOAN_IDS
) THEN 1
ELSE 0 END AS PREPAY_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_30_IDS
) THEN 1
ELSE 0 END AS DPD_30_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_60_IDS
) THEN 1
ELSE 0 END AS DPD_60_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_90_IDS
) THEN 1
ELSE 0 END AS DPD_90_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM DPD_120_IDS
) THEN 1
ELSE 0 END AS DPD_120_FLAG,
CASE WHEN loannumber IN (
SELECT loannumber FROM FORECLOSURE_IDS
) THEN 1
ELSE 0 END AS FORECLOSURE_FLAG
FROM(
SELECT *
FROM MORTGAGES
)
The below query will work faster than the one you have posted as the input table is not completely access to retrieve the results. Try running this query and see how it performs.
SELECT M.*,
CASE WHEN PLI.loannumber IS NOT NULL THEN 1
ELSE 0 END AS PREPAY_FLAG,
CASE WHEN D3I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_30_FLAG,
CASE WHEN D6I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_60_FLAG,
CASE WHEN D9I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_90_FLAG,
CASE WHEN D12I.loannumber IS NOT NULL THEN 1
ELSE 0 END AS DPD_120_FLAG,
CASE WHEN FCI.loannumber IS NOT NULL THEN 1
ELSE 0 END AS FORECLOSURE_FLAG
FROM MORTGAGES M
LEFT JOIN
PREPAY_LOAN_IDS PLI
ON M.loannumber = PLI.loannumber
LEFT JOIN
DPD_30_IDS D3I
ON M.loannumber = D3I.loannumber
LEFT JOIN
DPD_30_IDS D6I
ON M.loannumber = D6I.loannumber
LEFT JOIN
DPD_90_IDS D9I
ON M.loannumber = D9I.loannumber
LEFT JOIN
DPD_90_IDS D12I
ON M.loannumber = D12I.loannumber
LEFT JOIN
FORECLOSURE_IDS FCI
ON M.loannumber = FCI.loannumber
;
Since you're using SAS, here's a data step alternative, assuming that each of your datasets is already either sorted by or has an index on loannumber:
data want;
merge MORTGAGES(in = Mortgages)
PREPAY_LOAN_IDS(in = PLIDs keep = loannumber)
/*etc*/
;
by loannumber;
if Mortgages;
PREPAY_FLAG = PLIDs;
/*etc*/
run;
N.B. You will get duplicated records from MORTGAGES if you have duplicates in any of your other tables.

query sql returning custom value

I have a table with 3 columns (containing Integer values that assume only values from 0 to 10). I want extract, with a single query, a table with 1 column. This column must assume a value based on the following logic:
If one of these three columns has value 0 ----> the value of column of table generated by query must be 0 too.
If none of the last three columns has value 0 ----> the value of column must assume the value 1.
You are looking for CASE construct or IF function:
SELECT CASE WHEN (t.field1 = 0 OR t.field2 = 0 OR t.field3 = 0) THEN 0
ELSE 1 END AS value
FROM t;
In this specific case you might also use the fact that any member being zero will zero the product:
SELECT CASE WHEN (t.field1*t.field2*t.field3 = 0) THEN 0 ELSE 1 END AS value
FROM t;
Or
SELECT IF((t.field1*t.field2*t.field3)=0, 0, 1) AS value FROM t;
This is a simple case statement. Assuming there are no NULL values, try this:
select (case when col1 = 0 or col2 = 0 or col3 = 0 then 0 else 1 end)
Try this
SELECT
CASE
WHEN column1 = 0 THEN 0
WHEN column2 = 0 THEN 0
WHEN column3 = 0 THEN 0
ELSE 1
END
FROM urtable