Mysql select max variable value - mysql

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

Related

Problems with SUM in mysql

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;

SQL stament groups rows and calculate average

I am stuck with the following issue. I have 1 table that looks like this:
field_number.. Value
````````````````````````````````
1 ......................... 1
2 ..........................1
3 ......................... 2
4 ..........................2
etc.
I want to group different fieldnumbers and have an average for the value column. So the output should be:
field_number................Value
name(1,2)...................... 1.............. ((1+1)/2)
name(3,4)...................... 2.............. ((2+2)/2)
I have checked previous questions but cannot find any question that covers this issue (I might search on the wrong keywords though). So if this has already been covered my appologies, but any help or a point to a previous answer would be appreciated.
** =============UPDATE============= **
I went through your suggestions but did not get it right. So I am trying to be more specific. I almost have the result I want apart from the fact I want to have a fixed value in one of my columns. I have the following query:
Select
Avg(wp_rg_lead_detail.value),
wp_rg_lead_detail.field_number,
From
wp_rg_lead_detail
Where
wp_rg_lead_detail.field_number In (15, 17, 24) A
UNION
Select
Avg(wp_rg_lead_detail.value),
wp_rg_lead_detail.field_number,
From
wp_rg_lead_detail
Where
wp_rg_lead_detail.field_number In (16, 108, 18)
etc.
This gives me a table with two columns
wp_rg_lead_detail.value................field_number
4.3 (average)..............................15 (first value of av calculation)
What I want is to change the field number (15 in this case) in a fixed value (text). What and how should I add this to the query?
SELECT avg(value) FROM table WHERE field_number in (1,2)
SELECT avg(value) FROM table WHERE field_number in (3,4)
If your table is really this simple, you can also get away with:
select distinct
Value,
count(Value) as '#'
from table_name
group by Value
If you acctually want to group by a range, than you can put the logic of the range in your grouping clause (see this fiddle)
select distinct
avg(Value) as average,
floor(Value),
count(Value) as '#'
from table_name
group by floor(Value)
In the fiddle I used grouping on whole integers, but you can make that as complex as you like (see, for instance, this example)
If you are actually also interested in your corresponding fields, use group_concat() like so
select
Value,
group_concat(
distinct field_number
order by Value
) as fields
from table_name tn1
group by Value
order by Value
output:
Value | fields
---------------------------------
1 | 1,2
2 | 3,4
See this fiddle implemented from this blog post
For a generalized answer.
SELECT CONCAT('name','(',GROUP_CONCAT(field_number),')') AS field_number,
AVG(Value) as Value
FROM table_name
group by table_name.`Value`
Hope this helps.

WHERE clause in SSRS expression for max function

I have for example a query with return something as it
route value
1 3
2 2
3 4
4 5
5 1
then I need to put in 2 textbox the max and the min route so in sql this would be
select top 1 route from table where value=(select max(value) from table)
I add a image done in excel, how this would be.
I believe this is so easy but I dont have idea how to get it.
I got using expression, this was extactly expression
="Route "+
Convert.ToString (
Lookup(max(fields!value.Value),fields!value.Value ,fields!route.Value,"mydataset")
)
changing max for min, for the other...
thanks everyone.
I believe the query you're looking for would be:
With Min_Max_CTE as (
Select MIN(value) as Min_Value
, MAX(value) as Max_Value
From Table
)
Select Top 1 'Min' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Min_Value
Union All
Select Top 1 'Max' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Max_Value
Order by Type desc --This will put the Min Route first followed by the Max Route
Then, put that query into a dataset, and then create a tablix and use the Type, route, and value fields to return the minimum route and the maximum route. It should end up being set up just like your excel section with the min and max routes above.
You can do this SSRS by using a couple of separate tables. Your example data:
And two tables in the Designer:
Since the tables only have header rows, only the first row in the table will be displayed.
To make sure we get the MAX and MIN values in the two tables, each table needs to order its Dataset appropriately, i.e. by Value by descending and ascending respectively.
MAX table:
MIN table:
Which gives your expected result:

MySQL Query returns string instead of numeric value

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'

SUM of difference in values

I need to query in MS Access the difference in value of a column to 8 and only if it is greater than 8.
So if I have a column of numbers 1-10, I want to query the sum of all the value's differences from 8. So the result of the query for the below column would be 3. (9-8)+(10-8)
SELECT Sum(([time1]-8)+([time2]-8)+([time3]-8)+([time4]-8)+([time5]-8)+([time6]-8)+([time7]-8)+([time8]-8)+([time9]-8)+([time10]-8)+([time11]-8)+([time12]-8)+([time13]-8)+([time14]-8)+([time15]-8)+([time16]-8)+([time17]-8)+([time18]-8)+([time19]-8)+([time20]-8)+([time21]-8)+([time22]-8)) AS Total
FROM tblTimeTracking
WHERE (((Month(([Day])))=Month(Now()))) AND ([time1]>8 AND[time2]>8 AND[time3]>8 AND[time4]>8 AND[time5]>8 AND[time6]>8 AND[time7]>8 AND[time8]>8 AND[time9]>8 AND[time10]>8 AND[time11]>8 AND[time12]>8 AND[time13]>8 AND[time14]>8 AND[time15]>8 AND[time16]>8 AND[time17]>8 AND[time18]>8 AND[time19]>8 AND[time20]>8 AND[time21]>8 AND[time22]) ;
Thanks,
How about:
SELECT Sum([Value]-8) As SumOfVal
FROM table
WHERE [Value]>8
Edit re complete change in original question.
It is not clear what you want
SELECT Sum(([time1]-8)+([time2]-8) ...
WHERE [time1]>8 And Time2>8 ...
Time1>8 will exclude nulls, but if that is not what you are doing, you will need to consider:
Nz([time1],0) + ...
Edit re comments
Something like:
SELECT Sum(times) FROM
(SELECT IIf(Time1>8,Time1-8,Time1) As times FROM Table
UNION ALL
SELECT IIf(Time2>8,Time2-8,Time2) As times FROM Table) As b
As b is an alias: Access SQL
UNION / UNION ALL: View a unified result from multiple queries with a union query