How can I multiply numbers(data) in one column in MySQL? - mysql

How can I multiply all numbers in one column?
for example:
Status(finished or not finished)
1
1
1
1
0
I know how to use sum.
sum(Status)=4
I need some thing like sum for multiply
mul(status)=0
do we have something like mul(status)?

I don't know of a multiply aggregate function. However, in the case of a column containing only zeroes and ones the product will be one only if every value be one, otherwise it will be zero:
SELECT
CASE WHEN SUM(status) = COUNT(status) THEN 1 ELSE 0 END AS product
FROM yourTable

CASE WHEN SUM(status) = COUNT(status) THEN 1 ELSE 0 END AS product

Related

subtract 2 values from the same table with UNION

I hope I can get some help here since i cannot think of anything else.
so i have 2 results that come from the same table which i want to see in one query... so i used UNION, all good until there, now i need the difference between these two results. Let's say i have the next result:
measure 30_days
old_purchases 342
new_purchases 54
i need a new row that gives me the difference between those two, something like that:
measure 30_days
old_purchases 342
new_purchases 54
difference 288
You can use conditional aggregation:
select measure, value
from t
union all
select 'difference',
sum(case when measure = 'old_purchases' then value
when measure = 'new_purchases' then - value
else 0
end)
from t;
If you actually want to add this to the table then use insert:
insert into t (measure, value)
select 'difference',
sum(case when measure = 'old_purchases' then value
when measure = 'new_purchases' then - value
else 0
end)
from t;

To get total count based on condition

I am struggling to get the count of a particular person. As I am new to tableau I don't know how to write the condition for this query.
SELECT COUNT(*) as cnt
FROM
Expert_CollaborationsRequests
WHERE
ExpertID=3 AND
IsAccepted = 1
Generate a calculated field as:
Calculation1: IF ExpertID=3 AND IsAccepted = 1 THEN 1 ELSE 0 END
Then, place this field on a shelf (row, columns, etc.) and select Measure (Sum).
You can use LOD (Level of Detail) like so:
{ FIXED [ExpertID]: SUM(IF [IsAccepted]= 1 THEN 1 ELSE 0 END) }
This will give you aggregated values for each user.
For more information on LOD, please follow official Tableau help link here
SELECT COUNT(ExpertID),COUNT(IsAccepted ) as cnt
FROM
Expert_CollaborationsRequests
WHERE
ExpertID=3 OR
IsAccepted = 1

SELECT Inside a Case in MySQL

Is it possible to create an SELECT Statement within Conditional CASE, like this
SELECT la.*,
(
CASE
WHEN la.SoftDelete = 1
SELECT SUM(la.LoansAmount) FROM loans_account
ELSE
SELECT 0
END
) AS outstanding
FROM loans_account
I have tried this, but it does not work, I would like to know how to go about something like this please.
I guess you need the sum of loan amount where softdelete is 1
SELECT la.*,
SUM(
CASE
WHEN la.SoftDelete = 1
THEN la.LoansAmount
ELSE
0
END
) AS outstanding
FROM loans_account la
WHERE ...
or if you need the sum from whole where softdelete is 1 then you can do so
SELECT la.*,
SUM(la.LoansAmount) outstanding
FROM loans_account la
WHERE la.SoftDelete = 1
Note sum is aggregate function and without group by it will result as a single row assuming the whole table as one group
try this
SELECT la.*, CASE
WHEN la.SoftDelete = 1 THEN SUM(la.LoansAmount)
ELSE 0 END AS outstanding
FROM loans_account

Sum function producing whole number instead of percentage

So I have a field 'total' which is made of (fin_dec+fin_acc). I've been trying to divide total by fin acc:
select sum(case when fin_acc = 0 then 0 else (total/fin_acc)end)as sum1
The case statement is because it won't divide by 0s!
For example, total is 4 and fin_acc is 3 therefore the sum should be 75% however it's just giving me 3. It could be how I've created the fin_dec, fin_acc fields as a case when... then 1 else 0 and therefore wont split into a percentage.
Any ideas?
use the nullif function:
select sum(1.0*total/nullif(fin_acc,0))as sum1
Like this:
select sum(fin_acc) as s, count(fin_acc) as n from ..
then in your programming language:
if(n>0) avg= s/n else avg= null;
or just use:
select avg(fin_acc) as a from ..

Comparing 2 Columns in same table

I need to compare 2 columns in a table and give 3 things:
Count of rows checked (Total Rows that were checked)
Count of rows matching (Rows in which the 2 columns matched)
Count of rows different (Rows in which the 2 columns differed)
I've been able to get just rows matching using a join on itself, but I'm unsure how to get the others all at once. The importance of getting all of the information at the same time is because this is a very active table and the data changes with great frequency.
I cannot post the table schema as there is a lot of data in it that is irrelevant to this issue. The columns in question are both int(11) unsigned NOT NULL DEFAULT '0'. For purposes of this, I'll call them mask and mask_alt.
select
count(*) as rows_checked,
sum(col = col2) as rows_matching,
sum(col != col2) as rows_different
from table
Note the elegant use of sum(condition).
This works because in mysql true is 1 and false is 0. Summing these counts the number of times the condition is true. It's much more elegant than case when condition then 1 else 0 end, which is the SQL equivalent of coding if (condition) return true else return false; instead of simply return condition;.
Assuming you mean you want to count the rows where col1 is or is not equal to col2, you can use an aggregate SUM() coupled with CASE:
SELECT
COUNT(*) AS total,
SUM(CASE WHEN col = col2 THEN 1 ELSE 0 END )AS matching,
SUM(CASE WHEN col <> col2 THEN 1 ELSE 0 END) AS non_matching
FROM table
It may be more efficient to get the total COUNT(*) in a subquery though, and use that value to subtract the matching to get the non-matching, if the above is not performant enough.
SELECT
total,
matching,
total - matching AS non_matching
FROM
(
SELECT
COUNT(*) AS total,
SUM(CASE WHEN col = col2 THEN 1 ELSE 0 END )AS matching
FROM table
) sumtbl