How to find 7 consecutive non zero values in SQL? - mysql

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

Related

SQL - How To List True Case Statements In 1 Field [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
My current SQL statement groups certain colors and certain fruits as 2 new columns (Colors, Fruit) and adds + 1 whenever either respective column is greater than 0.
I want whenever Colors, or Fruits, is > 0, to have the name listed in the column "NewColumn".
Here is an example, with the last column being the expected output.
You can see Tom Brady does not have "Fruit" in the NewColumn, as the value is 0 for "Fruit".
Here Is My Current Code
SELECT
*,
( (CASE WHEN apple > 0 THEN 1 ELSE 0 END)
+ (CASE WHEN WHEN grapes > 0 THEN 1 ELSE 0 END)) as fruit,
( (CASE WHEN red > 0 THEN 1 ELSE 0 END)
+ (CASE WHEN blue > 0 THEN 1 ELSE 0 END)) as colors
from table
You can just add up color and fruit counts to get their totals and see whether you get a count greater than zero. Use CASE expressions for the texts 'Colors' and 'Fruits'. Use CONCAT_WS for the concatenation.
select
blue, red, apple, grapes,
concat_ws(',',
case when blue + red > 0 then 'Colors' end,
case when apple + grapes > 0 then 'Fruits' end
) as new_column
from mytable;
You can use a subquery to get the value of your NewColumn. Additional select case for this newly added column.
select t.*, case when t.Colors > 0 and t.Fruit > 0 then 'Colors;Fruit'
when t.Colors > 0 and t.Fruit = 0 then 'Colors'
when t.Colors = 0 and t.Fruit > 0 then 'Fruit'
else '' end as NewColumn
from (
select case when Red+Blue > 0 then Red+Blue else 0 end as Colors
, case when Apple+Grapes > 0 then Apple+Grapes else 0 end as Fruit
, Blue, Red, Apple, Grapes
from table) as t
This code is work for you.
Select *,
case When Fruit>0 and Colors > 0 then 'Colors;Fruit'
when Fruit > 0 and Colors = 0 then 'Fruit'
when Fruit = 0 and Colors > 0 then 'Colors' end as NewColumn
From Example

Case statement logic gone wrong

I have a question , as I am new to programming , I Can't figure out this simple logic. I am creating buckets under this code and for some reason the last buckets that I have created i.e 5 and 6 are not working. Everything is falling under classification 1. Need some help to categorising the data in different buckets. Can someone please tell me what can be done so that the data that is meeting condition 5 & 6 falls under it.
case when Sum(SubscriptLineAmount) > 0 and sum([Previous Amount]) = 0 then 1
when Sum(SubscriptLineAmount) > 0 and Sum(differenceAmount) > 0 then 2
when Sum(SubscriptLineAmount) > 0 and Sum(differenceAmount) < 0 then 3
when Sum(SubscriptLineAmount) = 0 and Sum(differenceAmount) <> 0 then 4
when Sum(SubscriptLineAmount) > 0 and sum([Previous Amount]) = 0 and [SUBSCRIPTION ORDER TYPE LIST] = 'Acquisition' then 5
when Sum(SubscriptLineAmount) > 0 and sum([Previous Amount]) = 0 and [SUBSCRIPTION ORDER TYPE LIST] = 'Other' then 6
else 7
end classification

how to replace the values in column to a string when a condition is met

I am trying to replace the values 0 or 1 in a column with 'hit' or 'flop'
select (Box_office_revenue-Budget)>Budget as hit_flop
from movie_data
hit_flop hit_flop
0 flop
0 flop
0 flop
1 hit
1 hit
how to replace the values with the texts without creating a new column
Also, I want to get 'super hit' when box_office_revenue/budget > 10
select
Case when box_office_revenue/budget > 10 then 'Super Hit'
when (Box_office_revenue-Budget)>Budget then 'Hit'
else 'Flop' end as hit_flop
from movie_data

SQL statement equivalent to ternary operator

I would like to create a statement that is equivalent to (x - y == 0) ? return 0 : return 100 in MySQL. Something that might look like this:
SELECT id, [(integer_val - 10 == 0) ? 0 : 100] AS new_val FROM my_table
I want to compare an attribute in each row to a certain number, and if the difference between that number and the number in the row is 0, I want it to give me 0, otherwise, I want it to give me 100.
Example:
Applying this query on my_table (with 10 being the 'compared to' number):
id | integer_val
===================
1 10
2 10
3 3
4 9
Would return this:
id | new_val
===================
1 100
2 100
3 0
4 0
How can I do this?
Try this:
SELECT id, IF(integer_val = 10, 100, 0) AS new_val
FROM my_table;
OR
SELECT id, (CASE WHEN integer_val = 10 THEN 100 ELSE 0 END) AS new_val
FROM my_table;
Use case when statement:
select *, (case when integer_val = 10 then 100 else 0 end) as New_Val
from yourtable
Try using the IF function:
SELECT id, IF(integer_val - 10 = 0, 0, 100) AS new_val FROM my_table
(I stuck with your condition expression, but it can be simplified a bit since integer_value - 10 = 0 has exactly the same truth value as integer_value = 10.)
Note that the IF function is different from MySQL's IF statement used for stored programs.

If two columns are 1 then make new column

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;