How to read a sql expression - mysql

I am very new to sql and trying to understand a simple aggregate expression.
sum(if(i.action = 'clean_delete', 1, 0)) as uninstalls,
What exactly the above expression mean or will do. I am having a hint it means if action = clean_delete assign it as 1 , else 0 .. but why sum ?
Sorry for being dumb at this point

This sums up how many times the condition is true.
In other words how many actions are clean_delete.
if(i.action = 'clean_delete', 1, 0)
returns 1 if the condition is true and 0 otherwise. In MySQL you could even reduce that to
sum(i.action = 'clean_delete') as uninstalls

If the action has the text 'clean_delete', 1 is returned; else 0.
So, this is summing up the cases where action is 'clean_delete' and using the alias uninstalls.

Related

MySQL Case function behave strange and inconstent

We are using MySQL 8 as our java application DB.
We have a query with the following format:
select
id,
group_concat(NAME ORDER BY ID separator ',,') AS Code,
CASE
WHEN MAX(p.VARIABLEfactor) = 1 THEN MAX(i.factor) ELSE MAX(p.factor) END AS factor
from MA_TABLE
join TABLE_P p on (...)
join TABLE_I i on (...)
group by id
The query worked very fine in our development environments until deploy with client where the factor column is getting null.
We have run the same query in the client environment from MySQL Workbench and we can see that the factor column is getting well populated.
After some debugging,we changed :
CASE
WHEN MAX(p.VARIABLEfactor) = 1 THEN MAX(i.factor) ELSE MAX(p.factor) END AS factor
to
MAX(
WHEN p.VARIABLEfactor = 1 THEN i.factor ELSE p.factor END ) AS factor,
and the query worked correctly.
Any help here please?
From your explanation I gather that you don't understand the difference of your two case expressions. But they are very different. Let's look at an example for one ID:
ID
VARIABLEfactor
i.factor
p.factor
100
0
null
10
100
1
null
20
Your expression
CASE WHEN MAX(p.VARIABLEfactor) = 1 THEN MAX(i.factor) ELSE MAX(p.factor) END
looks at the maximum VARIABLEfactor, which is 1, so the THEN case applies and the maximum i.factor is returned. This is null, as all i.factor are null.
Your expression
MAX(WHEN p.VARIABLEfactor = 1 THEN i.factor ELSE p.factor END)
looks at each row's VARIABLEfactor. For the first row this is 0, so the ELSE case applies and p.factor 10 is used. For the second row the VARIABLEfactor is 1, so its i.factor null gets used. Of these you take the maximum, which is 10.
To recap: The first expression is just a CASE expression on the aggregation results. It returns null here. The second expression is a conditional aggregation. It returns 10 for the sample data.

SQL : SELECT SUM WHERE CONDITION

I've got some troubles about SQL request :
I have a table like this table data image
I would like to create a view from this table to get :
Time_A : SUM of a column (total_time_taken) WHERE column (is_radiant)=1
Time_B : SUM of the same column (total_time_taken) WHERE column (is_radiant)=0
Time_AB : SUM of the column (total_time_taken) WHERE column (is_radiant)=0 OR (is_radiant)=1
SELECT
SUM(`matchpickban_radiant`.`total_time_taken`) AS `draft_time_radiant`,
SUM(`matchpickban_dire`.`total_time_taken`) AS `draft_time_radiant`
FROM
(`matchpickban` AS `matchpickban_radiant`
JOIN `matchpickban` AS `matchpickban_dire` ON ((`matchpickban_dire`.`idmatchpickban` = `matchpickban_radiant`.`idmatchpickban`)))
WHERE
`matchpickban_radiant`.`is_radiant` = 1
AND `matchpickban_dire`.`is_radiant` = 0
Actually I can run this request without syntax error but the result is NULL cause no data can be equal to 0 AND equal to 1 in the same time, obviously...
Also, I don't know if it's possible to make a JOIN the table to itself as I did (matchpickban JOIN matchpickban).
If syntax is correct I need to place my WHERE CONDITION away but don't know how, is it possible to replace it with 2 IF statement (IF is_radiant=0 SUM(...))
Thx for reading and helping me about this issue I got !
If you need more info about table or request I will give you all you need !
No need for a self-join or complex logic, you can just use conditional aggregation, which consists in using conditional expression within aggregate functions.
In MySQL, you could go:
select
sum(is_radiant * total_time_taken) time_a,
sum((1 - is_radiant) * total_time_taken) time_b,
sum(total_time_taken) time_ab
from matchpickban
where is_radiant in (0, 1)
This works because is_radiant is made of 0/1 values only - so this simplifies the logic. A more canonical way to phrase the conditional sums would be:
sum(case when is_radiant = 1 then total_time_taken else 0 end) time_a,
sum(case when is_radiant = 0 then total_time_taken else 0 end) time_b,

MYSQL WHERE id = 0 it should show total result

SQLFiddle
In this fiddle example i m trying to use WHERE o.d_id = 1 or 2,3,4 etc but if i use d_id= 0 it should show the total result or we can say the where clause should not work if d_id = 0.
You can do a conditional style WHERE clause like this....
WHERE (?inputvalue = 0 OR ?inputvalue = d_id)
In this case the ?inputvalue is whatever search term you want. You'll provide it from your programming language.
edit Notice that in many queries you can specify an input value just once. For example, if all you want is WHERE d_id = 17 that's easy to specify. You just put the 17 in and you're done.
But, if you follow my suggestion you need to repeat the input value. You need WHERE (0 = 17 OR d_id = 17). That will, when you give it 17, filter your result set to find the seventeens.
And, if you give it 0, like this WHERE ( 0 = 0 OR d_id = 0 ) it will find all results, not filtering on just the zeroes. That's good because there aren't any zeros.
Probably, that's not how WHERE works. If you write a statement like WHERE id = 0, you ask your database to return just the rows where the id column is set to 0. If you want your database to return all values, you have to skip this condition completly

Zend Framework 1 query, ordering results by satisfied criteria

I've to extract all products from "product" table (DBMS: MySQL, Adapter: PDO), ordering result by the number of filtering criteria that are matched.
This is an example of raw SQL query (but I'll use Zend_DB classes and adapters):
SELECT *
FROM product
WHERE price < 300
AND price > 100
AND discount = TRUE
AND used = FALSE
AND type = MEAL
and a lot of other optionals filter criteria that end user could introduce from the UI.
All the filter criteria (where conditions in the query) could be optionally matched by the user in the form of the web app, and the GOAL the my algorithm is to order the results from the most matching criteria product to the product that match at least 2 criteria.
I'm using Zend Framework 1, and my question is:
Is there any Zend class that could help me in this particular algorithm?
If no, could anyone suggest a solution for this problem?
I've tried a crude solution where I'll compose the query considering all the possible combination of the criteria, but considering that there are a lot of criteria, the algorithm complexity increases so much, so I suppose that an alternative may exists.
Thanks
Something like...
SELECT * FROM (
SELECT P.*,
case when price < 300 and price > 100 then 1 else 0 end +
case when discount = true then 1 else 0 end +
case when used = false then 1 else 0 end +
case when type = 'MEAL' then 1 else 0 end +
... (for each possible outcome) as Matches
FROM product p)
Where matches > 2
Order by Matches descending

Reporting Services - Find value in a group

I have a column with three possible values:
-1, 0 and any value greater than 0.
In each group, I must verify if ALL values are equal to -1 and if present ALMOST 0 value.
Is it possible?
I tried to solve it in this way:
=IIf(CountDistinct(Fields!Flag_09.Value,"Group1") = CountRows("Group1")
And First(Fields!Flag_09.Value = "-1"), "-",
[ ???? ])
Thanks
As far as I can tell, you want to see if all the detail rows in a group are -1 and then do something. Fortunately your data allows a very simple solution:
=IIF(CountRows("Group1") = 0 - SUM(Fields!MyField.Value), "-", "They aren't all -1")
That is, if the number of rows is equal to the negative of the sum of this field, then all the fields have -1 in them.