Change result from operation if equal to 0 - mysql

I have created this query, but sometimes this part (CEILING(TIMESTAMPDIFF(MONTH, od.insstart, NOW()) / od.term) returns 0, So I want to check result from this part and if it's equal to 0, to change it to 1, before compare it with second part = (SELECT COUNT(id) is there a way to do this ?
SELECT id FROM od where pol = '123' AND (CEILING(TIMESTAMPDIFF(MONTH, od.insstart, NOW()) / od.term) = (SELECT COUNT(id) FROM od as tt WHERE tt.policyNumber = '123'))
If result from (CEILING(TIMESTAMPDIFF(MONTH, od.insstart, NOW()) / od.term) is equal to 0 -> change it to 1 and then compare with second part which is (SELECT COUNT(id)

Use GREATEST() function
GREATEST( CEILING(TIMESTAMPDIFF(MONTH, od.insstart, NOW()) / od.term), 1 )

Related

MySQL Union two queries of same table

I have a problem. I created these 2 queries to get the start and end value:
Start value:
SELECT IF(`Order`.action = "Buy", `Order`.transMarketGross, `Order`.transMarketNet) AS startValue
FROM `Order`
WHERE agentId = (SELECT id FROM Agent WHERE owner = "Alexander") AND
`Order`.dateTimeExecuted <= DATE_SUB(curdate(), INTERVAL 1 MONTH)
ORDER BY `Order`.dateTimeExecuted DESC
LIMIT 1;
End value:
SELECT IF(`Order`.action = "Buy", `Order`.transMarketGross, `Order`.transMarketNet) AS endValue
FROM `Order`
WHERE agentId = ( SELECT id
FROM Agent
WHERE owner = "Alexander")
ORDER BY `Order`.dateTimeExecuted DESC LIMIT 1;
But now I want the start and end value in one result row, so I thought I could add UNION between the 2 queries:
SELECT IF(`Order`.action = "Buy", `Order`.transMarketGross, `Order`.transMarketNet) AS startValue
FROM `Order`
WHERE agentId = ( SELECT id
FROM Agent
WHERE owner = "Alexander") AND `Order`.dateTimeExecuted <= DATE_SUB(curdate(), INTERVAL 1 MONTH)
ORDER BY `Order`.dateTimeExecuted DESC LIMIT 1
UNION
SELECT IF(`Order`.action = "Buy", `Order`.transMarketGross, `Order`.transMarketNet) AS endValue
FROM `Order` WHERE agentId = ( SELECT id
FROM Agent
WHERE owner = "Alexander")
ORDER BY `Order`.dateTimeExecuted DESC LIMIT 1
Using these queries seperately, they do their job, but I get an error on the total query that this query is not valid. Can someone tell me what I am doing wrong and how I can fix this? Also if there are any improvements to simplify the query, let me know!
If you want one row, you might as well use two subqueries:
SELECT (SELECT (CASE WHEN o.action = 'Buy', o.transMarketGross, o.transMarketNet) AS startValue
FROM `Order` o JOIN
Agent a
ON o.agentId = a.id
WHERE a.owner = 'Alexander' AND
o.dateTimeExecuted <= DATE_SUB(curdate(), INTERVAL 1 MONTH)
ORDER BY o.dateTimeExecuted DESC
LIMIT 1
),
(SELECT (CASE WHEN o.action = 'Buy', o.transMarketGross, o.transMarketNet) AS startValue
FROM `Order` o JOIN
Agent a
ON o.agentId = a.id
WHERE a.owner = 'Alexander'
ORDER BY o.dateTimeExecuted DESC
LIMIT 1
);
Note that I made the following changes:
Added table aliases so the query is easier to write and read.
Qualified all column references, so the query is unambiguous.
Replaced the in with JOIN, which seems to be the intention.
Replaced double quotes with the SQL standard single quotes for string delimiters.
Use CASE (the standard) rather than the bespoke IF() for conditional logic.

mysql GROUP CONCAT not returning values

Here is my query
SELECT
SUM(o.order_disc + o.order_disc_vat) AS manualsale
FROM
orders o
WHERE
o.order_flag IN (0 , 2, 3)
AND o.order_status = '1'
AND (o.assign_sale_id IN (SELECT GROUP_CONCAT(CAST(id AS SIGNED)) AS ids FROM users WHERE team_id = 92))
AND DATE(o.payment_on) = DATE(NOW())
above query return null when i run this query in terminal
When i use subquery below it returns data
SELECT GROUP_CONCAT(CAST(id AS SIGNED)) AS ids FROM users WHERE team_id = 92)
above query returns
'106,124,142,179'
and when i run my first query like below
SELECT
SUM(o.order_disc + o.order_disc_vat) AS manualsale
FROM
orders o
WHERE
o.order_flag IN (0 , 2, 3)
AND o.order_status = '1'
AND (o.assign_sale_id IN (106,124,142,179))
AND DATE(o.payment_on) = DATE(NOW())
it return me value.
Why it is not working with subquery please help
This does not do what you want:
AND (o.assign_sale_id IN (SELECT GROUP_CONCAT(CAST(id AS SIGNED)) AS ids FROM users WHERE team_id = 92))
This compares a single value against a comma-separated list of values, so it never matches (unless there is just one row in users for the given team).
You could phrase this as:
AND assign_sale_id IN (SELECT id FROM users WHERE team_id = 92)
But this would probably be more efficently expressed with exists:
AND EXISTS(SELECT 1 FROM users u WHERE u.team_id = 92 AND u.id = o.assign_sale_id)
Side note: I would also recommend rewriting this condition:
AND DATE(o.payment_on) = DATE(NOW())
To the following, which can take advantage of an index:
AND o.payment_on >= current_date AND o.payment_on < current_date + interval 1 day

Simplify CASE expression used multiple times

For readability, I would like to modify the below statement. Is there a way to extract the CASE statement, so I can use it multiple times without having to write it out every time?
select
mturk_worker.notes,
worker_id,
count(worker_id) answers,
count(episode_has_accepted_imdb_url) scored,
sum( case when isnull(imdb_url) and isnull(accepted_imdb_url) then 1
when imdb_url = accepted_imdb_url then 1
else 0 end ) correct,
100 * ( sum( case when isnull(imdb_url) and isnull(accepted_imdb_url) then 1
when imdb_url = accepted_imdb_url then 1
else 0 end)
/ count(episode_has_accepted_imdb_url) ) percentage
from
mturk_completion
inner join mturk_worker using (worker_id)
where
timestamp > '2015-02-01'
group by
worker_id
order by
percentage desc,
correct desc
You can actually eliminate the case statements. MySQL will interpret boolean expressions as integers in a numeric context (with 1 being true and 0 being false):
select mturk_worker.notes, worker_id, count(worker_id) answers,
count(episode_has_accepted_imdb_url) scored,
sum(imdb_url = accepted_imdb_url or imdb_url is null and accepted_idb_url is null) as correct,
(100 * sum(imdb_url = accepted_imdb_url or imdb_url is null and accepted_idb_url is null) / count(episode_has_accepted_imdb_url)
) as percentage
from mturk_completion inner join
mturk_worker
using (worker_id)
where timestamp > '2015-02-01'
group by worker_id
order by percentage desc, correct desc;
If you like, you can simplify it further by using the null-safe equals operator:
select mturk_worker.notes, worker_id, count(worker_id) answers,
count(episode_has_accepted_imdb_url) scored,
sum(imdb_url <=> accepted_imdb_url) as correct,
(100 * sum(imdb_url <=> accepted_imdb_url) / count(episode_has_accepted_imdb_url)
) as percentage
from mturk_completion inner join
mturk_worker
using (worker_id)
where timestamp > '2015-02-01'
group by worker_id
order by percentage desc, correct desc;
This isn't standard SQL, but it is perfectly fine in MySQL.
Otherwise, you would need to use a subquery, and there is additional overhead in MySQL associated with subqueries.

mysql address outer query from subquery

I have this query:
SELECT
sec_to_time(avg(t1.sessiontime)) as aloc,
CONCAT(TRUNCATE(sum(t1.terminatecauseid = 1) * 100 / count(*),
1),
'%') as asr,
count(*) as calls,
cast(t1.destination as unsigned) as prefix,
t2.destination as destination,
SEC_TO_TIME(sum(t1.sessiontime)) as duration
FROM
cc_call AS t1
inner join
cc_prefix as t2 ON t1.destination = t2.prefix
WHERE
t1.card_id = '133' AND t1.starttime >= ('2014-06-1') AND t1.starttime <= ('2014-07-01 23:59:59') and t1.terminatecauseid = 1
group by t1.destination
order by duration DESC
LIMIT 0 , 25
t1.terminatecauseid = 1 means successful call,
'asr' means average success rate,
Im trying to find out how many calls with (t1.terminatecauseid = 1) from the total calls made to an extension.
this line doesn't work:
sum(t1.terminatecauseid = 1) * 100 / count(*)
since I already have (t1.terminatecauseid = 1) in the WHERE clause.
Im thinking about putting a subquery, to retrieve total calls, where count(*) currently is.
How can I have this query calculate the ASR with total calls made?
example sqlfiddle
if possible, I'd like to not show results with duration=NULL
Use conditional aggregation, something like this:
SELECT sec_to_time(avg(case when t1.terminatecauseid = 1 then t1.sessiontime end)) as aloc,
CONCAT(TRUNCATE(sum(t1.terminatecauseid = 1) * 100 / count(*),
1),
'%') as asr,
count(*) as TotalCalls,
sum(t1.terminatecauseid = 1) as Terminated1Calls,
cast(t1.destination as unsigned) as prefix,
t2.destination as destination,
SEC_TO_TIME(sum(case when t1.terminatecauseid = 1 then t1.sessiontime end)) as duration
FROM cc_call t1 inner join
cc_prefix t2
ON t1.destination = t2.prefix
WHERE t1.card_id = '133' AND
t1.starttime >= ('2014-06-1') AND t1.starttime <= ('2014-07-01 23:59:59')
group by t1.destination
order by duration DESC
LIMIT 0 , 25;

View - Return 0 if no rows found in a grouped by query

Let's say I have the following MySQL view:
create or replace view total_transactions(account_id, total) as
select
t.account_id,
ifnull(sum(t.value), 0) as total
from transactions t
where t.paid IS TRUE
group by t.bank_account_id;
Let's say the account doesn't have any transaction yet, I want the view to return 0.
Right now, if I do a select like:
select * from total_transactions where account_id = 2060;
And account 2060 didn't had any transaction, it will return me nothing, instead of 0.
How could I fix that?
Thanks in advance.
EDIT
I think it could be something with the group by...
If I execute the query that I'm using for the view without the group by, it works (return 0 even with no results), but if I use group by it comes null:
select
t.account_id,
ifnull(sum(t.value), 0) as total
from transactions t
where t.paid IS TRUE
and account_id = 2060;
Returns 0, and
create or replace view total_transactions(account_id, total) as
select
t.account_id,
ifnull(sum(t.value), 0) as total
from transactions t
where t.paid IS TRUE
and account_id = 2060
group by t.bank_account_id;
Return an empty set.
If there is not an entry in the view results, then this will always return NULL - That's SQL. If you change your SELECT that you use against the view, you can achieve what you want:
SELECT IFNULL(total, 0) FROM total_transactions WHERE account_id = 2060
Edit:
(SELECT IFNULL(total, 0) total FROM total_transactions WHERE account_id = 2060)
UNION
(SELECT 0 total)
In production, don't use SELECT *. See this SO question for a thorough response as to why.
So, assuming you're not, you can use COALESCE, which will return the first non-null value.
SELECT
COALESCE(total, 0)
FROM
total_transactions
WHERE
account_id = '2600'
For positive values I use
select max(x.cnt) as cnt
from (
select ifnull(meta_value, 0) as cnt from wp_postmeta where post_id = 5543 and meta_key = '_bbp_voice_count'
union
select 0 as cnt) x
or for any
select x.cnt
from (
select ifnull(meta_value, 0) as cnt from wp_postmeta where post_id = 5543 and meta_key = '_bbp_voice_count'
union
select 0 as cnt
) x
limit 1