Using multiple OR in SQL [closed] - mysql

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 years ago.
Improve this question
I need to write a query which should check if there is at least one column which has a value greater than 0 (>0) in the table. Currently, I have written a 'WHERE' clause with multiple OR conditions. I suspect that this may not be the best way of doing this (may be use SUM?). Seeking opinion from SQL experts.
WHERE raw_0 >0 OR raw_1 >0 OR raw_2 >0 OR raw_3 >0 OR raw_4>0

You can use
WHERE (raw_0 >0 OR raw_1 >0 OR raw_2 >0 OR raw_3 >0 OR raw_4>0);
As per your question. As dnoeth commented, is the easiest to write and understand (and probably the best for the optimizer as there's no calculation involved)
Alternatively you can use:
WHERE ((RAW_0 + RAW_1 + RAW_2 + RAW_3) > 0);
The above condition would be satisfied IF and only IF, atleast one of the values is >0.
You can also use concat operator to concat the strings and check if the concatenated string has value other than zero:
WHERE TO_NUMBER ( RAW_0 || RAW_1 || RAW_2 || RAW_3 ) > 0
Test data for 3rd query:
select 'YES value' data from dual where TO_NUMBER ( 1 || 2) > 0 ;
select 'NO value' data from dual where TO_NUMBER ( 0 || 0) > 0 ;

If the values can only be non-negative, as in one of the OP comment then you can check the multiplication
WHERE raw_0 * raw_1 * raw_2 * raw_3 * raw_4 > 0
but that will check if every value as positive. To check if only one is positive we can negate this logic using the SIGN function
WHERE (1 - SIGN(raw_0))
* (1 - SIGN(raw_1))
* (1 - SIGN(raw_2))
* (1 - SIGN(raw_3))
* (1 - SIGN(raw_4)) = 0
(1 - SIGN(value)) return 1 when the value is zero and 0 when the value is positive, so if at least one value is positive the product will be 0.

Related

Count all rows and add a certain int from column of each result row into overall count [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 6 months ago.
Improve this question
I have a table consisting of columns tblEventID, isSafeSlot, plusOnes. I want to retrieve the number of rows where tblEventID is equal to X and isSafeSlot is equal to true, and if both are true, also check the column plusOnes for that one row. This column will hold an int from 0 to 99. I need to add those numbers also into the count.
So say we have this table:
tblEventID / isSafeSlot / plusOnes
100 / true / 0
100 / true / 53
100 / false / 0
101 / true / 99
And I am giving it tblEventID 100, the count result should be 55 (two rows making the count 2, plus one row has the number 53 stored under plusOnes, result 55)
How can I make this inside one query?
Filter the rows of the table with your conditions and aggregate:
SELECT COUNT(*) + SUM(plusOnes)
FROM tablename
WHERE tblEventID = 100 AND isSafeSlot;
or, simpler:
SELECT SUM(plusOnes + 1)
FROM tablename
WHERE tblEventID = 100 AND isSafeSlot;
SELECT COUNT(*) + SUM(plusOnes) AS total
FROM `say_we_have_this_table`
WHERE tblEventId = 100
AND isSafeSlot = true;

mySQL: Select first number not in subquery results [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 2 years ago.
Improve this question
I have a column itemId that contains unique IDs in the format US001, US002, US003 etc.
The trailing numbers of these IDs are not consecutive as rows may have been deleted.
I am looking for a way to find the first number that DOES NOT EXIST in my column.
Example 1:
Column: US001, US002, US004, US006.
Expected result: 3.
Example 2:
Column: US001, US002, US003, US004, US007.
Expected result: 5.
Example 3:
Column: US001, US002, US003, US004, US005.
Expected result: 6.
I tried the following but this returns NULL or no results (also no error):
SELECT MIN((RIGHT(i.itemId, 3)) + 1) AS nextAvailableId
FROM items i
WHERE RIGHT(i.itemId, 3) NOT IN
(
SELECT * FROM
(
SELECT RIGHT(i2.itemId, 3) AS itemNo
FROM items i2
) AS x
)
Can anyone please help me with this ?
Thanks,
Tim
Here is one option using window functions and string functions:
select min(id) + 1 as nextAvailableId
from (
select substr(itemid, 2) as id,
lead(substr(itemid, 2)) over(order by substr(itemid, 2)) as lead_id
from mytable t
) t
where lead_id > id + 1 or lead_id is null
In MySQL 5.x, where string functions are not available, you can use a subquery:
select min(substr(itemid, 2)) + 1 as nextAvailableId
from mytable
where not exists (
select 1
from mytable t1
where substr(t1.itemid, 2) = substr(t.itemid, 2) + 1
)

Query for students passed or not passed in specific Subjects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a Table with Column Headings:
ID Student_Name
Roll_Number
Subject1MarksObtained
Subject1TotalMarks
Subject2MarksObtained
Subject2TotalMarks
Subject3MarksObtained
Subject3TotalMarks
Subject4MarksObtained
Subject4TotalMarks
I want to write a query to output the results for individual student who have pass at least three of the subjects.
Without seeing the data, lets make some assumptions:
A pass is awarded for a subject if the marks obtained for that subject are equal to or more than 50% of the total marks available for that subject.
The name of the table is called Enrollment
To return a list of students who have passed at least 3 subjects we can use a query similar to the following:
This solution uses CASE to evaluate a 1 for a pass and a 0 for fail for each subject, then we sum those results and only return rows that have a score of 3 or more.
SELECT ID, Student_Name, Roll_Number
FROM Enrollment
WHERE
( CASE WHEN (Subject1MarksObtained / Subject1TotalMarks) >= 0.5 THEN 1 ELSE 0 END
+ CASE WHEN (Subject2MarksObtained / Subject2TotalMarks) >= 0.5 THEN 1 ELSE 0 END
+ CASE WHEN (Subject3MarksObtained / Subject3TotalMarks) >= 0.5 THEN 1 ELSE 0 END
+ CASE WHEN (Subject4MarksObtained / Subject4TotalMarks) >= 0.5 THEN 1 ELSE 0 END
) >= 3
There are different way to approach this, but this query is simple to read and gets the job done.
If you are querying an access table, then CASE WHEN is not supported but you can use IIF or SWITCH to achieve the same results:
SELECT ID, Student_Name, Roll_Number
FROM Enrollment
WHERE
( IIF( (Subject1MarksObtained / Subject1TotalMarks) >= 0.5, 1, 0)
+ IIF( (Subject2MarksObtained / Subject2TotalMarks) >= 0.5, 1, 0)
+ IIF( (Subject3MarksObtained / Subject3TotalMarks) >= 0.5, 1, 0)
+ IIF( (Subject4MarksObtained / Subject4TotalMarks) >= 0.5, 1, 0)
) >= 3
Let's instead start by fixing your broken schema. A normalised design might look somewhat as follows:
Student
ID
Student_Name
Roll_Number
Results
StudentID
Subject
Mark

SQL query - find values that meet m conditions out of n

Is there any way to find values that meet any m conditions out of given n conditions? For instance, if there are 10 conditions, and I want to find values that meet any 2 of them.
Use CASE expressions in the WHERE clause, 1 for each condition like this:
WHERE 2 =
CASE WHEN <condition1> THEN 1 ELSE 0 END +
CASE WHEN <condition2> THEN 1 ELSE 0 END +
CASE WHEN <condition3> THEN 1 ELSE 0 END +
..........................................
You can change the = sign to > or < to meet your requirement.
There is. It's not gonna be pretty though.
Start with your conditions as SELECT expressions.
select T.*,
case
when T.SOME_NUMERIC_COLUMN > 0 then 1
else 0
end IS_POSITIVE,
(select sign(COUNT(*))
from SOME_OTHER_TABLE
where parent_id = T.ID) HAS_CHILDREN
...
from SOME_TABLE T
Design these expression in such a way that you get 1 when a condition is met and 0 when it's not.
Then sum up the score and add a WHERE clause.
SELECT *
FROM (
SELECT R.*,
IS_POSITIVE + HAS_CHILDREN + ... SCORE
FROM (...) R)
WHERE SCORE > 2
Of course you're gonna pay a hefty price in performance for this. You won't be able to use your conditions directly to limit the resultset so I'd expect the execution plans to be extremely disappointing. That said, it's not like what you have in mind is a standard task for RDBMS so it should be enough for a proof of concept.

MySQL add 0 when < *.10 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can't seem to find/create a working query which will update the x column with an extra zero when value after the dot is below .10 - I've got column x with values;
7.3
5.3
0.14
I am trying to update the column so when the value after the dot is below .10 it adds a zero, above example will eventually need to look like;
7.03
5.03
0.14
Is there a way I can do this using an mysql UPDATE-query?
Many thanks.
First of all, 0.03 is not equal to 0.30, so i believe what you want is 7.30 and not 7.03. To do this, you need to alter your table column to display two decimal digits:
my_magic_column float(3,2)
where 3 is the total number of digits and 2 is the number of decimals to show.
I don't really get idea behind such operation but it's pretty simple.
SELECT CASE
WHEN INSTR(id, '.') = 0 THEN id
ELSE CASE
WHEN CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED) < 10 THEN
CONCAT(SUBSTRING_INDEX(id, '.', 1), '.0', CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED))
ELSE id
END
END as result
FROM table_name
UPDATE table_name SET id = CASE
WHEN INSTR(id, '.') = 0 THEN id
ELSE CASE
WHEN CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED) < 10 THEN
CONCAT(SUBSTRING_INDEX(id, '.', 1), '.0', CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED))
ELSE id
END
END
But before using this, think once again about your schema and what are you trying to do. I don't think you'll need this then.
Looks like what you want to do is:
update TABLE set YOUR_NUMBER = (FLOOR(NUMBER) + (NUMBER-FLOOR(NUMBER))/10)
where FLOOR(NUMBER*10) = NUMBER*10
FLOOR(NUMBER) - take the integer part and add to it:
(NUMBER-FLOOR(NUMBER))/10 - the fraction part of the number - divided by 10
The where condition checks that there's only one digit after the dot