I have a table "Cabine" with fields "SBC750" (integer) and "Evaso" (tinyint)
The field Evaso can be 1, 0 or Null
I'm looking for the sum of all sbc750 where Evaso is NOT 1.
I tried with
select sum(sbc750) from cabine where evaso<>1;
but the result is NULL: why???
If I use
select sum(sbc750) from cabine
I obtain 55 and if I use
select sum(sbc750) from cabine where evaso=1
I obtain 34!
So the results might be 21 and not Null. Please help me
With the NULL-safe equality operator you should get the desired results:
select sum(sbc750) from cabine where not evaso<=>1;
Also see here for reference.
SELECT sum(sbc750) FROM cabine where evaso is null or evaso<>1;
Related
count() in select fields, return always one row if no match found mysql with search criteria. But Count function returns 0 always as a result DB returns one records with null values.
Can some help me out of this situation.
I am sorry for my bad English
Try Below Query:
SELECT LastStatusMessageIDName
,COUNT(1) AS [Count of Total Records]
,COUNT(LastExecutionResult) AS [Count of Non-NULL Records]
,SUM(CASE WHEN LastExecutionResult IS NULL THEN 1 END) AS [Count of NULL Records]
FROM dbo.v_ClientAdvertisementStatus
Hope this help ..!!
I have a temporary table and the following query:
Select *, #rnkma:=IF(age>17 and age<22,#rnkma+1,#rnkma),
#rnkmb:=IF(age>21 and age<25,#rnkmb+1,#rnkmb),
#rnkmc:=IF(age>24 and age<30,#rnkmc+1,#rnkmc),
#rnkmd:=IF(age>29 and age<35,#rnkmd+1,#rnkmd),
#rnkme:=IF(age>34 ,#rnkme+1,#rnkme) from tmp_user where gender='M';
Select #rnkma, #rnkmb, #rnkmc, #rnkmd ;
The result is:
#rnkma #rnkmb #rnkmc #rnkmd
5 8 3 4
I want to get max value of this result like:
#rnkmb
8
Any suggestions? Thanks.
Try using CASE and GREATEST():
SELECT
CASE GREATEST(#rnkma, #rnkmb, #rnkmc, #rnkmd)
WHEN #rnkma THEN '#rnkma'
WHEN #rnkmb THEN '#rnkmb'
WHEN #rnkmc THEN '#rnkmc'
WHEN #rnkmd THEN '#rnkmd'
END name,
GREATEST(#rnkma, #rnkmb, #rnkmc, #rnkmd) value
(...)
#Pawel's answer is correct , i will add just one thing as a reference.
There's a difference between Max() and Greatest()
MAX() returns maximum value of an expression. Ex:
select max(Age) from staff;
Greatest() returns the greatest of the list of exprs. More than one column must be given.
SELECT GREATEST(150,160,161) FROM XXX; will return 161
I want to multiply 4 with number of faults if faulttype='business' and faultseverity='fatal', using query listed below;
Select faulttype, IF (faulttype='business' AND faultseverity='fatal', 1,0)* 4 FROM tbl_fault WHERE product='DAS' AND faultdistribution='missed'
group by faulttype
I am getting result business instead of numeric value, What should be corrected in this query?
Regards
try this
Select faulttype, sum(IF (faulttype='business' AND faultseverity='fatal', 1,0))*4 FROM tbl_fault WHERE product='DAS' AND faultdistribution='missed'
group by faulttype;
For reference see the sql fiddle.
Try this
Select CASE faulttype WHEN faulttype='business' AND faultseverity='fatal' THEN 1*4 ELSE 0 END AS rez FROM tbl_fault WHERE product='DAS' AND faultdistribution='missed'
I am trying to solve following problem:
I have a table with timeStamp column and three columns for different types of variables. The columns with values are filled randomly according to the conditions. So it looks like this:
smpl_time float_val num_val str_val 15:31
10 NULL NULL
15:32 NULL 15.4 NULL
15:33
NULL NULL Disabled
What I would like to achieve is a result that would merge all val fields into one column and sorts them according to timeStamp:
smpl_time merge_val
15:31 10
15:32 15.4
15:33 Disabled
So far I have a SELECT just for one val type looking like this (it contains also other field from the table that are common for each entry):
SELECT s.smpl_time AS Time,s.float_val AS Value, e.name AS Severity, m.name AS Status
FROM sample s JOIN channel c ON c.channel_id=s.channel_id JOIN severity e ON
e.severity_id=s.severity_id JOIN status m ON m.status_id=s.status_id WHERE
s.channel_id='id' AND smpl_time BETWEEN TIMESTAMP 'startTime' AND TIMESTAMP 'stopTime';
Does anyone have an idea how to do this or if it is even possible?
Cheers,
Mike
Assuming only one of the three columns is populated for any given row, use the COALESCE function.
SELECT smpl_time, COALESCE(float_vaL, num_val, str_val) AS merge_val
FROM ...
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
COALESCE(value,...)
Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
mysql> SELECT COALESCE(NULL,1);
-> 1
mysql> SELECT COALESCE(NULL,NULL,NULL);
-> NULL
I have this table called "values":
value
12
13
5
56
3
56
79
98
58
74
52
2
8
32
4
I want to get the lowest value for each block of 5, so I tried this query:
SET #a = -1;
SELECT FLOOR((#a:=#a+1)/5) AS block, MIN(value)
FROM values
GROUP BY block
It seems like this query does not include the last row in each block, so I tried count:
SET #a = -1;
SELECT FLOOR((#a:=#a+1)/5) AS block, COUNT(value)
FROM values
GROUP BY block
which returned:
block COUNT(value)
0 4
1 4
2 4
what is happening here?
Try this:
SELECT FLOOR(id/5) AS block, MIN(`value`)
FROM (SELECT (#id:=#id+1) AS id, `value`
FROM `values`, (SELECT #id:=-1) AS A) AS B
GROUP BY block;
Here is a snippet from the MySQL 5.7 documentation that should help:
In a SELECT statement, each select expression is evaluated only when sent to the client. This means that in a HAVING, GROUP BY, or ORDER BY clause, referring to a variable that is assigned a value in the select expression list does not work as expected:
mysql> SELECT (#aa:=id) AS a, (#aa+3) AS b FROM tbl_name HAVING b=5;
The reference to b in the HAVING clause refers to an alias for an expression in the select list that uses #aa. This does not work as expected: #aa contains the value of id from the previous selected row, not from the current row.
Your query uses block as the expression in the GROUP_BY clause, where block is the result of an assignment, so your situation is the analogous to the one from the MySQL documentation. It certainly looks promising, given that you are "off by one"!
Running an explain might tell you exactly what is happening; it will probably be a good exercise to try this.