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

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.

Related

MySql Case Query with select Count

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.

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

Calculating an average number from three columns

I am trying to calculate an average number from three columns but only include the column in the calculation if column is not null and is bigger than 0;
for example the average usually is
(column1 + column2 + column3) / 3
but if column3 is null or 0 then it will be
(column1 + column2 + column3) / 2 or (column1 + column2 ) / 2
I have this sol far but it is not complete. the average is wrong when one of the columns is 0 (0 is default)
SELECT movie.title,
movie.imdbrating,
movie.metacritic,
tomato.rating,
((imdbrating + metacritic + tomato.rating)/3) as average
FROM movie, tomato
WHERE movie.imdbid = tomato.imdbid
How can I implement this?
I'm fixing the rest of the query to use table aliases and proper join syntax. But the case statements are what you really need:
SELECT m.title, m.imdbrating, m.metacritic,
t.rating,
((case when imdbrating > 0 then imdbrating else 0 end) +
(case when metacritic > 0 then metacritic else 0 end) +
(case when t.rating > 0 then t.rating else 0 end) +
) / nullif(coalesce((imdbrating > 0), 0) + coalesce((metacritic > 0), 0) + coalesce((t.rating > 0), 0)), 0)
FROM movie m JOIN
tomato t
ON m.imdbid = t.imdbid;
The denominator is using a convenient MySQL extension where booleans are treated as 0 or 1 in a numeric context. The nullif() returns NULL if no rating meets the conditions. And, the > 0 is is not true for NULL values.
Try this:
SELECT (IF Column3 IS NULL OR Column3=0, AVG(Column1+Column2), Avg(Column1+Column2+ Column3)) as Result FROM Table
EDIT:
If any of the tree columns can be nullm, try this:
SELECT AVG(IF(Column1 IS NULL,0,Column1)+IF(Column2 IS NULL,0,Column2)+IF(Column3 IS NULL,0,Column3)) as Result FROM Table
UPDATE
(ifnull(column1,0)+ifnull(column2,0)+ifnull(column3,0))/
nullif((abs(sign(ifnull(column1,0)))
+abs(sign(ifnull(column2,0)))
+abs(sign(ifnull(column3,0)))), 0)
Can handle Negative values.
Demo to test

MySQL matrix multiplication

I am trying to write matrix multiplication for MySQL and am kinda stuck:
basically, my matrices are stored in format
[row#, column#, matrixID, value], so for example matrix [3 x 2] would be something like:
[row#, column#, matrixID, value]
1 1 mat01 1
1 2 mat01 2
1 3 mat01 3
2 1 mat01 4
2 2 mat01 5
2 3 mat01 6
being equivalent to: [[1 2 3],[4 5 6]]
following does calculation of single element of matrix1 * matrix2 quite well:
SELECT SUM(row1.`val` * col2.`val`)
FROM matValues row1
INNER JOIN `matValues` col2
WHERE row1.`row` = 1 AND row1.`mID`='matrix1' AND
col2.`mID`='matrix2' AND col2.`col` = 1 AND row1.col = col2.row
wrapping this into function and then using another function to iterate over row and column numbers might work, but I have problems with generating this set of numbers and iterating over them using SQL.
Any advice / suggestions are welcome
Try:
select m1.`row#`, m2.`column#`, sum(m1.value*m2.value)
from matValues m1
join matValues m2 on m2.`row#` = m1.`column#`
where m1.matrixID = 'mat01' and m2.matrixID = 'mat02'
group by m1.`row#`, m2.`column#`
Example here.
(Replace 'mat01' and 'mat02' with suitable matrixID values.)
You can do the entire calculation in SQL. You only give an example with a single matrix, which because it is not square, cannot be multiplied by itself.
Here is the idea:
SELECT mout.row, mout.col, SUM(m1.value*m2.value)
FROM (select distinct row from matValues cross join
select distinct COL from matValues
) mout left outer join
matValues m1
on m1.row = mout.row left outer join
matValues m2
on m2.col = mout.col and
m2.row = m1.col
I know this is SQL-Server syntax, but it should give you a start on the corresponding MySql syntax. The sparse matrix nature seems to handle well.
with I as (
select * from ( values
(1,1, 1),
(2,2, 1),
(3,3, 1)
) data(row,col,value)
)
,z_90 as (
select * from ( values
(1,2, 1),
(2,1,-1),
(3,3, 1)
) data(row,col,value)
)
,xy as (
select * from ( values
(1,2, 1),
(2,1, 1),
(3,3, 1)
) data(row,col,value)
)
,x_90 as (
select * from ( values
(1,1, 1),
(2,3, 1),
(3,2,-1)
) data(row,col,value)
)
select
'I * z_90' as instance,
a.row,
b.col,
sum( case when a.value is null then 0 else a.value end
* case when b.value is null then 0 else b.value end ) as value
from I as a
join z_90 as b on a.col = b.row
group by a.row, b.col
union all
select
'z_90 * xy' as instance,
a.row,
b.col,
sum( case when a.value is null then 0 else a.value end
* case when b.value is null then 0 else b.value end ) as value
from z_90 as a
join xy as b on a.col = b.row
group by a.row, b.col
union all
select
'z_90 * x_90' as instance,
a.row,
b.col,
sum( case when a.value is null then 0 else a.value end
* case when b.value is null then 0 else b.value end ) as value
from z_90 as a
join x_90 as b on a.col = b.row
group by a.row, b.col
order by instance, a.row, b.col
yields:
instance row col value
----------- ----------- ----------- -----------
I * z_90 1 2 1
I * z_90 2 1 -1
I * z_90 3 3 1
z_90 * x_90 1 3 1
z_90 * x_90 2 1 -1
z_90 * x_90 3 2 -1
z_90 * xy 1 1 1
z_90 * xy 2 2 -1
z_90 * xy 3 3 1
However, I suggest you also check out performing this on your graphics card. NVIDIA has a good example of implementing matrix multiplication in theri C Programming Guide.

SQL QUERY- multiple COUNT returns wrong (same) results when using in GROUP BY

I will simplfy this:
I have two SQL expressions which works OK:
First:
select count(*) as number1
from T1
where DATE1>'2012-01-01' and DATE2<'2012-12-31'
Result:13
select count(*) as number2
from T1
where DATE3>DATE2 and CURDATE()>DATE2
Result:5
But when I try to insert those two COUNTS in GROUP BY I am getting as resulyt always 13!!!
SELECT NAME,
COUNT(case when DATE1>'2012-01-01' and DATE2<'2012-12-31' then 1 else 0 end) as number1,
COUNT (case when (DATE3>DATE2 and CURDATE()>DATE2) then 1 else 0 end) as number 2
from T1
I am getting as result:
NAME NUMBER1 NUMBER2
A 5 5
B 4 4
C 4 4
But I should get:
NAME NUMBER1 NUMBER2
A 5 4
B 4 0
C 4 1
So that SUM of columns be 13 and 5 like in first two queries . What am I doing wrong?
Thank you
COUNT(expression) counts not null expressions. You can modify your query by changing the ELSE 0 to ELSE NULL or by removing it (the ELSE NULL is implied at CASE expressions):
SELECT name,
COUNT(CASE WHEN date1 > '2012-01-01' AND date2 < '2012-12-31'
THEN 1 END
) AS number1,
COUNT(CASE WHEN date3 > date2 AND CURDATE() > date2
THEN 1 END
) AS number2
FROM T1
GROUP BY name ;
Do not use count here.Use Sum. If you want to use the count then put null in else instead of 0
try:
SELECT NAME,
(select count(*) from T1 where DATE1>'2012-01-01' and DATE2<'2012-12-31') AS number1,
(select count(*) from T1 where DATE3>DATE2 and CURDATE()>DATE2) AS number2
FROM T1
GROUP BY NAME
Try this:
SELECT name,
SUM(CASE WHEN DATE1>'2012-01-01' AND DATE2<'2012-12-31' THEN 1 ELSE 0 END) AS number1,
SUM(CASE WHEN (DATE3>DATE2 AND CURDATE()>DATE2) THEN 1 ELSE 0 END) AS number2
FROM T1 GROUP BY name