Adding multiple columns using CASE statement and '+' but function not adding correctly - teradatasql

I am trying to add the values of multiple columns together and then derive an indicator (Yes or No) if the summed value is greater or equal to 1. My query is below:
select
case when column1 + column2 + column3 >= 1 Then 'Yes' ELSE 'No' end as "Indicator"
from Table_name
The results that I get returned in the Indicator column are all 'No' but I know for a fact that some of the columns that are added have values greater than 1 so I should get some 'Yes' values. Any idea what is causing this?
Thanks

Related

Checking if all non-NA elements between two or more columns or vectors are identical

I have two columns in a dataframe that contain date information after a left outer join. Because of the style of join, one of the date columns now contains NAs. I want to check if all non-NA values are identical between these columns. An example is below:
date 1 date 2
1/1/21 NA
1/2/21 1/2/21
1/3/21 NA
1/4/21 1/4/21
I don't need the second column if all non-NA values match
Before I did the left outer join, I did a outer join and this statement:
identical(df[['date 1']], df[['date 2']])
returned a true as each row in both columns were indeed identical
Is there a way to use this or a similar statement while ignoring all rows that contain an "NA" in "date 2"?
You can test for null values and mismatched values by filtering your df, then check whether there are any.
df_mismatch = df[(df['date 2'].notnull()) & (df['date 1'] != df['date 2'])]
if len(df_mismatch) > 0:
print('found this many mismatches:', len(df_mismatch))
I found a workaround:
first, create a new dataframe that just stores these two columns. The reason you'll want to make a new dataframe is because we will use na.omit in the next step which will remove any row that contains 1 or more "NA" in any column.
df2 <- df[, c("date 1", "date2")]
Then remove all rows that contain an "NA" in any column
df2 <- na.omit(df2)
Finally, run identical to check if the remaining columns are indeed identical
identical(df2[['date1']], df2[['date2']])
I'm sure there is a more elegant way, but this worked for me in the meantime

PL/SQL Case with Group By and Pivot

I have data that I'm presenting in an APEX interactive report, using a pivot statement to display monthly data for a period of 15 years. I am color coding some of the values based on if it contains a decimal using a case statement.
My problem is that by using the case statement, it is creating multiple rows from one row of data. My report is showing 2 rows for each item, one for the row containing values without decimals, and one row with values containing decimals.
Multiple Rows
How can I combine the rows into one? Use a Group By? or is there a better way?
select buscat, prod_parent, year_month, volume, load_source, tstamp,
case when instr(VOLUME, '.') > 0 then 'color:#FF7755;' else 'color:#000000;' end flag
from HISTORY where id > 0
Here is raw data from SQL query...
SQL return
According to the SQL Return image the data is not repeating. It looks like you are not filtering for 'Volume == 0'. Try changing 'ID' to 'volume' in where clause:
select yearMonth, volume, load_source, tstamp,
case when instr(volume, '.') > 0 then 'color:#FF7755;' else 'color:#000000;' end flag
from HISTORY
where volume > 0

adding repeating sequence numbers to a column in SQLite database based on conditions

I added a column in my SQLite database, and I need to insert repeating sequence numbers, starting with 1...n BUT it's based on grouping by other columns. The sequence needs to start over at 1 again when there is a new grouping.
Here is my table:
CREATE TABLE "ProdRunResults" ("ID" INTEGER PRIMARY KEY NOT NULL UNIQUE , "SeqNumbr" INTEGER, "Shift" INTEGER, "ShiftSeqNumbr" INTEGER, "Date" DATETIME, "ProdRunID" INTEGER, "Result" VARCHAR)
ShiftSeqNumbr is the new column that I need to populate with sequence numbers, based on grouping of numbers in ProdRunID column then by numbers in the Shift column.
There could be up to 3 "shifts" (work shifts in a 24 hr period).
I scraped together some code to do this but it adds the sequence numbers to ShiftSeqNumbr column in reverse (descending) order:
UPDATE ProdRunResults
SET ShiftSeqNumbr = (SELECT COUNT (*)
FROM ProdRunResults AS N
WHERE N.ProdRunID = ProdRunResults.ProdRunID
AND N.Shift = ProdRunResults.Shift
AND N.ShiftSeqNumbr = ProdRunResults.ShiftSeqNumbr);
How can I change the Update statement so the sequence numbers start at 1 and go up? Or is there a better way to do this?
Your UPDATE statement counts how many rows there are that have the same values in the ProdRunID/Shift/ShiftSeqNumbr columns as the current row. The current row always has an empty value in ShiftSeqNumbr, so it is counting how many rows in the current group have not yet been updated.
You need to count how many rows come before the current row, i.e., how many rows have the same ProdRunID and Shift values, and the same or a smaller SeqNumbr value:
UPDATE ProdRunResults
SET ShiftSeqNumbr = (SELECT COUNT (*)
FROM ProdRunResults AS N
WHERE N.ProdRunID = ProdRunResults.ProdRunID
AND N.Shift = ProdRunResults.Shift
AND N.SeqNumbr <= ProdRunResults.SeqNumbr);

How to Count number of occurences of values in all columns

I am able to find out the Count number of occurrences of values in a single column.
By using
select column_name,count(count_name)
from table_name order by column_name
But I want a query for no of occurrences of multiple column values.
The count function, when used directly on a column, just returns a count of the rows. The sum of the counts over multiple columns is just the amount of rows times the amount of columns. One thing we could do is to return the sum of decodes of the condition over all columns, e.g.:
select mytable.*,
DECODE(mytable.column1,"target value",1,0) + DECODE(mytable.column2,"target
value",1,0) as hits from mytable
Basically what that does, is for each row, it will check the amount of columns that meet the condition. In this case, that value ('hits') can be 0, 1 or 2 because we are checking the condition over 2 columns.

sql server: set computed value to a column

I want to insert a new row in a table say 'table1' with autogenerated value for a single non-primary column with a formula condition max+1 i.e maximum value among elements of that particular column. Tell me the correct formula In shortHow to compute max value in a column of int type and then how to insert new row with max=1 value to that column
go to sql management studio ->rigth click on select table -> select design->then right click on your column->computed column specification->type desired formula hereORFetch max value from the database for desired column and then add max+1 to that value and insert it to that specific column(new row containing that computed value)
Insert Into table1
(autogeneratedcolumnname)
Values
((Select Max(value) + 1 From tablewithvaluecolumn))
Probably using OUTPUT clause you can achieve this..
INSERT INTO your_table('column1', 'column2')
OUTPUT inserted.column_to_compute
SELECT MAX(column_name) + 1 FROM your_table
Where column_to_compute is the name of column you want to hold computed values

Resources