MySql Case Query with select Count - mysql

I am writing a simple case statement where I return the value 1 if number of rows returned are 1 or more and 0 if nothing is found .
I am getting a Syntax error on listed below simple query
SELECT CASE (select count(*) from account where account_id = 12 >0) then 1 else 0 end;

MySQL returns 1 if a condition is TRUE and 0 if it is FALSE
SELECT count(*) >= 1
from account
where account_id = 12

figured out I was missing the word when true in the query
SELECT CASE (select count(*) from .account where account_id = 12 >0) when true then 1 else 0 end;

This
SELECT CASE (select count(*) from .account where account_id = 12 >0) when true then 1 else 0 end;
only syntactically works, as you mistakenly compare 12 with 0. This is certainly an improvement:
SELECT CASE ((select count(*) from .account where account_id = 12) > 0) when true then 1 else 0 end;
However, why do you need to count all the records, when you can test for existence:
SELECT CASE (exists (select 1 from .account where account_id = 12)) when true then 1 else 0 end;
This last one will find the first such row if exists and return one if so, 0 otherwise.

Related

When Current column and Previous column > 0, then do this

mysql> select * from table;
+------+------+------+-------+
| id | cnta | cntb | cntc |
+------+-------------+-------+
4 0 1 2
3 2 3 0
2 1 0 1
1 3 2 2
I would like to compare two sequential rows (current column and previous column) and if they are both greater than 0, I'd like to sum the results of the sequential rows.
this is what I tried and failed:
SELECT
g1.id,
(case
When g2.cnta > 0 and g1.cnta > 0 then g1.cnta ELSE 0) End as cnta +
(case
When g2.cntb > 0 and g1.cntb > 0 then g1.cntb ELSE 0) End as cntb +
(case
When g2.cntc > 0 and g1.cntc > 0 then g1.cntc ELSE 0) End as cntc
FROM table g1 INNER JOIN table g2 ON g2.id = g1.id+ 1;
the final output I'm trying to get is like this (if current column and previous column > 0, then current column1 + etc ) :
id totalcnt
4 1
3 2
2 2
1
How can I fix my query? or can I get alternative approach as a solution, please?
** I forgot to mention that there are no NULL values in my table. Only 0s and positive integers.
If your last row of the result is not to be empty, try this:
SELECT
t1.id,
(
CASE WHEN t1.cnta>0 AND t2.cnta>0 THEN t1.cnta ELSE 0 END
+
CASE WHEN t1.cntb>0 AND t2.cntb>0 THEN t1.cntb ELSE 0 END
+
CASE WHEN t1.cntc>0 AND t2.cntc>0 THEN t1.cntc ELSE 0 END
) AS cValue
FROM
table1 t1
LEFT JOIN table1 t2 ON t2.id=t1.id-1;
OR if you really want it to be empty, you can use a subquery
SELECT
t1.id,
IFNULL(
(
SELECT
(
CASE WHEN t1.cnta>0 AND t2.cnta>0 THEN t1.cnta ELSE 0 END
+
CASE WHEN t1.cntb>0 AND t2.cntb>0 THEN t1.cntb ELSE 0 END
+
CASE WHEN t1.cntc>0 AND t2.cntc>0 THEN t1.cntc ELSE 0 END
)
FROM
table1 t2 WHERE t2.id=t1.id-1)
,'') AS cValue
FROM table1 t1
SELECT t1.id,
(t1.cnta * t2.cnta > 0) * t1.cnta
+ (t1.cntb * t2.cntb > 0) * t1.cntb
+ (t1.cntc * t2.cntc > 0) * t1.cntc totalcnt
FROM test t1
LEFT JOIN test t2 ON t1.id = t2.id + 1;
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=48f296035e95bf4c7331427e82c25619
t1.cntX * t2.cntX is NULL if at least one value is NULL, is zero if at least one value is zero, and is 1 if both values are not zero/NULL.

Return 1 or 0 in SQL depending on the multiple statements

If I find that some of the user exists with such a parameters, I want to get 1 otherwise 0. In the future I'll have to add more blocks. But it doesn't seem to work now. What am I doing wrong?
SELECT CAST(CASE WHEN EXISTS(SELECT 1
FROM Customers
WHERE Country = 'France' AND PostalCode%2 = 0)
OR (WHERE Country = 'Germany' AND PostalCode%2 = 0))
)
THEN 1
ELSE 0
END AS BIT)
You need two separate exists:
SELECT CAST(CASE WHEN EXISTS (SELECT 1
FROM Customers
WHERE Country = 'France' AND PostalCode%2 = 0
)
THEN 1
WHEN EXISTS (SELECT 1
FROM Customers
WHERE Country = 'Germany' AND PostalCode%2 = 0
)
THEN 1
ELSE 0
END AS BIT)
Actually, I broke this into two separate THEN clauses. This is almost equivalent to using OR, but because the logic is inside a CASE, THEN seems more natural. (The difference is that the optimizer could choose to re-arrange the OR conditions, but the THEN conditions are executed in lexical order.)
If your statements are actually this simple, you can combine them as:
SELECT CAST(CASE WHEN EXISTS (SELECT 1
FROM Customers
WHERE Country IN ('France', 'Germany') AND PostalCode%2 = 0
)
THEN 1
ELSE 0
END AS BIT)
It looks to me like you're just having issues with your bracketing:
SELECT CAST(
CASE WHEN EXISTS(
SELECT 1
FROM Customers
WHERE (Country = 'France' AND PostalCode%2 = 0)
OR (Country = 'Germany' AND PostalCode%2 = 0)
) THEN 1 ELSE 0 END AS BIT)
Building on Gordon's assumption that PostalCode%2 = 0 for all tested 'sets' of conditionals (you haven't said as much yet), you could likewise shorten this to:
SELECT CAST(
CASE WHEN EXISTS(
SELECT 1
FROM Customers
WHERE PostalCode%2 = 0
AND Country IN ('France', 'Germany')
) THEN 1 ELSE 0 END AS BIT)

SQL return having sum value

I have a query that checks a group and makes sure that it has more than 1 value under 2
SELECT `tile` FROM TFResults
GROUP BY `tile`
HAVING SUM(CASE WHEN `Place` < 2 THEN 1 ELSE 0 END)> 1 ;
I would like to return the value of sum also but can't seem to get it to work
SELECT `tile`, thesum
FROM TFResults
GROUP BY `tile`
HAVING SUM(CASE WHEN `Place` < 2 THEN 1 ELSE 0 END) as thesum > 1 ;
You define alias names in the select clause
SELECT tile,
SUM(CASE WHEN Place < 2 THEN 1 ELSE 0 END) as thesum
FROM TFResults
GROUP BY tile
HAVING thesum > 1
First you need to move sum part to select statement. And if you need only one column to check then use if instead CASE. Check Below
SELECT tile, SUM(if(Place < 2, 1, 0)) place_sum
FROM TFResults
GROUP BY tile
HAVING place_sum > 1
Just move sum statement to select:
SELECT
`tile`,
SUM(CASE WHEN `Place` < 2 THEN 1 ELSE 0 END) AS thesum
FROM TFResults
GROUP BY `tile`
HAVING thesum > 1 ;

Have Query Results Display In Different Columns

I am trying to avoid the creation of a table to have these results display (if possible). What I want to do is to essentially have the look of a table with my query results and have a column for fullitemlist, a column for 'has been ordered' and a column for 'has not been ordered'. Essentially I am trying to have a result set like such returned from my query:
> Zip = 55555
> fullitemlist ----- Has Been Ordered ---- Has Not Been Ordered
> Knife Set 1 0
> Butcher Block 0 1
> Dishwasher 0 1
> Hair Dryer 1 0
so on and so forth for all items in my tbl_itemlist. This is the 3 CTE queries I was trying to work with, but it returns everything in one giant list, not what I was after :)
Declare #zip varchar(5)
Set #zip = '55555'
;With CTE As
(
Select fullitemlist from tbl_ItemList
Where zip = #zip
)
,CTE2 As
(
Select case when hasbeenordered = 1 then 1 else 0 end as 'Has Been Ordered' from tbl_purchasehistory
WHERE itemid IN (Select itemid from tbl_ItemList where hasbeenordered = 1 and zip = #zip)
)
,CTE3 As
(
Select case when hasbeenordered = 0 then 1 else 0 end as as 'Has NEVER Been Ordered' from tbl_purchasehistory
WHERE itemid IN (Select itemid from tbl_ItemList where hasbeenordered = 0 and zip = #zip)
)
SELECT * from CTE
UNION ALL
SELECT * FROM CTE2
UNION ALL
SELECT * FROM CTE3
-- you may write your query like this according to your question instead of CTE ,union all,SubQuery this is the simplest way .
SELECT fullitemlist
,CASE
WHEN tp.hasbeenordered = 1
AND tI.hasbeenordered = 1
THEN 1
ELSE 0
END AS 'Has Been Ordered'
,CASE
WHEN tp.hasbeenordered = 0
AND ti.tp.hasbeenordered = 0
THEN 1
ELSE 0
END AS 'Has NEVER Been Ordered'
FROM tbl_ItemList ti
INNER JOIN tbl_purchasehistory tp ON ti.itemid = tp.ItemId
WHERE zip = #zip

How to change the value of sum function inside the query?

I have a query with sum aggregation function :
SELECT sum(case when result=1 then 1 when result=2 then 0)as final_result From results
I want to change this part when result=2 then 0 to something like that when result=2 then final_result equals zero
Is it possible to do this ? or there is another way for that?
Try this :
SELECT CASE WHEN cnt=sum THEN sum ELSE 0 END as Final_result
FROM
(
SELECT count(*) as cnt,
SUM(case when result=1 then 1 else 0 end) as sum
from results
) Temp
Working Fiddle here.
Explanation:
Inner query select the total number of records and the sum of the records. Then if those count and sum are equal that means all values are 1.
NB: Removed the checking when result=2 then 0 because, the query will select 0 for any values other than 1. So there is no need to check for result=2.
EDIT:
To find the count of 10 consecutive 1's, you can do:
SELECT SUM(CASE WHEN RN=10 THEN 1 ELSE 0 END) AS final_result
FROM
(
SELECT CASE WHEN result=1 THEN #row_number:=#row_number+1 ELSE #row_number:=0 END AS RN,
result
FROM results, (SELECT #row_number:=0) AS t
) Temp
Sample Fiddle here.
I think you want something like this:
SELECT case max(case result when 2 then 1 else 0 end)
when 1 then 0
else sum(case when result=1 then 1 else 0 end)
end as final_result
From results
Fiddle here
So what I'm doing is checking for the final-result-must-be-0 condition inside a MAX(). That way, if any of the rows meets this condition (i.e. result = 2), the maximum value will be 1. Then I can use that value to determine whether I wanna return 0 or do the SUM().