If we have a table of 15 columns, the first one is id, and the other columns have numeric
data type,
in MySQL or a similar languages, how can I find if the record have 7 consecutive non zero values, meaning that 7 of the contiguous columns have a value not equals to zero?
We can write a query has a lot of OR operations to check that each 7 columns don't have 0 value, but I want to know if there is shorter way
Try the following using CONCAT and INSTR functions, explanation is within query comments:
/*
check if the col value is not equal to 0,
this will return 1 if true and 0 if false,
and concatenate all of the returned values.
*/
with concat_cols as
(
select *,
concat(
col1<>0, col2<>0, col3<>0, col4<>0, col5<>0,
col6<>0, col7<>0, col8<>0, col9<>0, col10<>0,
col11<>0, col12<>0, col13<>0, col14<>0, col15<>0
) as con_col
from table_name
)
/*
using the instr function, check if the concatenated
result contains 7 consecutive 1s (7 not 0 values).
*/
select * from concat_cols
where instr(con_col, repeat('1', 7))
See demo
One way:
field(0,col1,col2,col3,col4,col5,col6,col7) = 0 or
field(0,col2,col3,col4,col5,col6,col7,col8) = 0 or
field(0,col3,col4,col5,col6,col7,col8,col9) = 0 or
field(0,col4,col5,col6,col7,col8,col9,col10) = 0 or
field(0,col5,col6,col7,col8,col9,col10,col11) = 0 or
field(0,col6,col7,col8,col9,col10,col11,col12) = 0 or
field(0,col7,col8,col9,col10,col11,col12,col13) = 0 or
field(0,col8,col9,col10,col11,col12,col13,col14) = 0 or
field(0,col9,col10,col11,col12,col13,col14,col15) = 0
SELECT id
FROM your_table
WHERE SUM(CASE WHEN column_2 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_3 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_4 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_5 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_6 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_7 > 0 THEN 1 ELSE 0 END +
CASE WHEN column_8 > 0 THEN 1 ELSE 0 END) >= 7
I get the type mismatch error when i use CASE WHEN in SparkSQL. Below is the error i get:
pyspark.sql.utils.AnalysisException: u"cannot resolve 'CASE WHEN q.`utama` THEN 1 ELSE 0 END' due to data type mismatch: WHEN expressions in CaseWhen should all be boolean type, but the 1th when expression's type is utama#7L; line 3 pos 11;
I am using SparkSQL as following.
from pyspark.sql import SparkSession
spark = SparkSession \
.builder \
.appName("Python Spark SQL basic example") \
.config("spark.sql.crossJoin.enabled", "true") \
.enableHiveSupport()\
.getOrCreate()
results = spark.sql("""
SELECT q.ID_Propinsi_reg,
SUM(CASE
WHEN q.utama THEN 1
ELSE 0
END)AS utama,
SUM(CASE
WHEN q.madya THEN 1
ELSE 0
END)AS madya,
SUM(CASE
WHEN q.muda THEN 1
ELSE 0
END)AS muda,
p.Nama
FROM
(SELECT first(a.ID_Personal) as ID_Personal,
CONCAT(a.ID_Personal, '', a.ID_Sub_Bidang) AS kode_ta,
first(a.ID_Propinsi_reg) as ID_Propinsi_reg,
first(a.ID_Kualifikasi) as ID_Kualifikasi,
SUM(CASE
WHEN a.ID_Kualifikasi='1' THEN 1
ELSE 0
END)AS utama,
SUM(CASE
WHEN a.ID_Kualifikasi='2' THEN 1
ELSE 0
END)AS madya,
SUM(CASE
WHEN a.ID_Kualifikasi='3' THEN 1
ELSE 0
END)AS muda
FROM temp_db.personal_reg_tt a
GROUP BY 2) q
INNER JOIN lpjk_dwh.new_lpjk_propinsi p ON q.ID_Propinsi_reg=p.ID_Propinsi
GROUP BY 1
""")
results.show()
Whats wrong with the code? Is it something related to the data type of ID_kualifikasi column?
The error says WHEN expressions in CaseWhen should all be boolean type, but the 1th when expression's type is utama#7L. You need to have a boolean type in the when expression. You can try casting it into a boolean by CASE WHEN CAST(q.utama AS BOOLEAN) THEN 1 ELSE 0 END etc.
The question I try to solve:
Return the value for "prefix" following these conditions:
If model_hyouji is a number and ranging from 0 to 9, set prefix = 0
If model_hyouji is a letter and ranging from a to z or A to Z, set prefix = 1
In all other cases, set prefix = 2
I used this SQL:
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case prefix
when 'a' then 1
when 'b' then 2
else 3
end
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
and I get this error:
Unknown column 'prefix' in 'field list'
assuming you column model_hyouji is varchar(1) (otherwise use substr(model_hyouji,1) )
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case
when model_hyouji between '0' and '9' then 0
when model_hyouji between 'a' and 'z' then 1
when model_hyouji between 'A' and 'Z' then 1
else 2
end prefix
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
or you can use regexp
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case
when model_hyouji REGEXP '^[0-9]{1}$' then 0
when model_hyouji REGEXP '^[A-Za-z]$' then 1
else 2
end prefix
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
SELECT
model_code,model_hyouji,model_name_prefix,model_kana_prefix,model_count,
case
when prefix = 'a' then 1 when 'b' then 2 else 3 end
You need CASE WHEN prefix =
But you also dont specify which prefix column you want to use hence the error on unknown column
E.G
SELECT
model_code,model_hyouji,model_name_prefix,model_kana_prefix,model_count,
case
WHEN model_kana_prefix = 'a' then 1
WHEN model_kana_prefix = 'b' then 2
else 3 end
i need a my sql statement which selects something similar to this
SELECT present, wholeday, attendance
present and wholeday is given, while attendance is generated by a combination of present and wholeday
if present == 1 and wholeday == 1 then attendance = 1
if present == 1 and wholeday == 0 then attendance = .5
if present == 0 and wholeday == 0 then attendance = 0
Your query would be:
SELECT PRESENT, WHOLEDAY,
CASE
WHEN (PRESENT = 1 AND WHOLEDAY = 1) THEN 1
WHEN (PRESENT = 1 AND WHOLEDAY = 0) THEN 0.5
ELSE 0
END as ATTENDANCE
FROM MY_TABLE
Case Syntax is :
CASE
WHEN condition_1 THEN commands
WHEN condition_2 THEN commands
...
ELSE commands
END CASE;
From the above sample data, i want to write a query that will return SalesAmount as 0.0 if ExpRow value = 1 and return ExpenseAmount as 0.0 if ExpRow value = 2
SELECT
ExpRow,
ExpenseDate,
CASE WHEN ExpRow = 2 THEN 0 ELSE ExpenseAmount END AS ExpenseAmount,
CASE WHEN ExpRow = 1 THEN 0 ELSE SalesAmount END AS SalesAmount
FROM
Table
Not sure if this is exactly what you are after but...
SELECT CASE([ExpRow]) WHEN 1 THEN 0 ELSE SalesAmount END AS 'SalesAmount',
CASE([ExpRow]) WHEN 2 THEN 0 ELSE ExpenseAmount END AS 'ExpenseAmount'
FROM [YOUR_TABLE]