MySQL if statements (with method invocations)? [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 9 years ago.
Improve this question
How do I do the following pseudocode in MySQL:
if (a IS NOT NULL)
{
GROUP BY (id)
num = COUNT(*)
} else {
num = 0
}

Note sure what you are trying to do .....
if you want to count non-null value, check those examples, it would look like this
COUNT(NULLIF(a, ''))
Also consider use Case if you need more complete counting
CASE
WHEN a = 'something' COUNT(1)
ELSE NULL
EDIT
If you want to invoke some query, depend on some value, consider use store procedure/ function

This may be the code you are looking for:
select num := count(distinct id)
from t;
This will set num to 0 if there are no rows.
If you really want the comparison to NULL:
select num := (case when a is not null then count(distinct id) else 0 end)
from t;
Note this assumes that a and num are not columns in the database.

Related

For a particular datetime, find a record with the closest datetime before and after [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Let's have a MySQL database table that contains a DateTime column when. Such a column may carry any DateTime value other than NULL.
How can I create a table that would contain columns when, whenA, and whenB, with all full-minute DateTime values (YYYY-MM-DD HH:MM:00) between the minimum and maximum when values and whenA being the closest DateTime on or before, and whenB being the closest DateTime on or after the when value? If the exact when value exists, whenA and whenB would be the same as when. If no record on or before, or on or after doesn't exist, NULL will be filled into whenA or whenB, respectively.
Obviously, there are many possible approaches how to make it, but the question is what should be the most efficient one?
You can construct the values using a recursive CTE. Then you can use correlated subqueries to get what you want:
with recursive cte as (
select from_unixtime(floor(unix_timestamp(min(whent)) / 60) * 60) as minw,
max(whent) as maxw
from t
union all
select minw + interval 1 minute, maxw
from cte
where minw < maxw
)
select minw,
(select max(t2.when)
from t t2
where t2.when = cte.minw
) as when,
(select max(t2.when)
from t t2
where t2.when <= cte.minw
) as when_before,
(select min(t2.when)
from t t2
where t2.when >= cte.minw
) as when_after
from cte;
Note that when is a really bad name for a column, because it is a SQL keyword and a reserved word in MySQL.
Here is a db<>fiddle.
I should note that if you have a numbers or tally table of some sort, that could also be used.

select a column value from a table only if it has a value assigned else insert a user defined value into that column [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 7 years ago.
Improve this question
my requirement is to generate a mysql query as
select ss.column1 P1,ss.column2 P2,ss.column3 P3 from table1 ss;
i want to select column 2 field values only if it has a value in table1 else i want to insert a constant value into the P2 column. Can someone help me with framing a query for this.
COALESCE is the SQL function that let's you replace null values by something else. E.g.:
select
ss.column1 as p1,
coalesce(ss.column2, 'no value') as p2,
ss.column3 as p3
from table1 ss;
The data types must match however, so you can use the above when column2 is a text column. If it is numeric, you can replace null with a numeric value (e.g. with a zero) or you'd cast the columns' datatype.
Some examples:
coalesce(mytext, 'unknown')
coalesce(mynumber, 0)
coalesce(cast(mynumber as varchar), 'unknown')
coalesce(mydate, date(now()))
coalesce(date_format(mydate, '%Y-%m-%d'), 'unknown')

Order by expression [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 years ago.
Improve this question
How would I do the following:
select * from table order by istrue(field > 90)
What does istrue() do? I don't recognize it as a MySQL function.
Perhaps your intention is to order things so the values of field greater than 90 are first. If so:
order by (field > 90) desc
In a numeric context, MySQL treats boolean expressions as integers with "1" being true. Hence the desc to get the true values first.
use Case statement;
select * from table
order by
case when field > 90 then ordercolumn end
Maybe what your trying to achieve is this:
SELECT * FROM table WHERE field > 90 ORDER BY field
I think that this is what you want:
SELECT * FROM table ORDER BY (field > 90) DESC
Or maybe:
SELECT * FROM table WHERE field > 90 ORDER BY field DESC

replacing column values with Y and N [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a table which contains column Flag with values(Y,N) ,when I write select Query it should return me result as (Yes,No) .how to achieve that????Please Help
Try this:
SELECT CASE Flag
WHEN 'Y' THEN 'Yes'
ELSE 'No'
END AS Flag
FROM TableName
Syntax:
CASE column_name
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result
END
Read more about CASE here.
Try this
SELECT CASE Flag
WHEN 'Y' THEN 'Yes'
ELSE 'No'
END AS Flag
FROM TABLENAME
You can also try if condition apart from casing as suggested above:
SELECT IF(flag='Y','Yes','No') FROM mytable;

SQL: Select key based on missing value [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 9 years ago.
Improve this question
I have one table created like this
Table:
key value
1 10000
1 10001
2 10001
And I want to select key 2 because it has 10001 but not 10000. Is there a simple way? I tried using joins but I have no idea how to make join select only missing value.
Assuming you're looking for keys that don't have all of the available values, you can do that by comparing the number of DISTINCT values for each key to the number of DISTINCT values in the entire table.
SELECT `key` FROM `table`
GROUP BY `key`
HAVING COUNT(DISTINCT value) < (SELECT COUNT(DISTINCT value) FROM `table`)
Seen in action at SQLFiddle
If there are only a particular set of values you're interested in, you can change this to using hardcoded values.
SELECT `key` FROM `table`
WHERE value IN (10001, 10000)
GROUP BY `key`
HAVING COUNT(DISTINCT value) < 2
For this to generalize to a larger number of values, the number in the HAVING clause needs to match the number of elements in the IN condition.
You can simply do this:
SELECT DISTINCT t1.`key`
FROM tablename t1
WHERE t1.`key` NOT IN(SELECT `key`
FROM tablename
WHERE value = 10000);
SQL Fiddle Demo
I think the easiest way to approach this problem is to put the logic in the having clause. The following counts the number of times each key appears and applies your logic:
select "key"
from t
group by "key"
having sum(value = 10000) = 0 and
sum(value = 10001) > 0;
This is using a MySQL feature where boolean expressions (value = 10000) are treated as integers, with 0 being false and 1 being true. So, sum(value = 10000) counts the number of rows where value is 10000 for each key.