I have a table in MySQL with three columns that need an average of each row of three columns using the stored procedure:
Id | One | Two | Three
----+-------+-------+-------
1 | 10 | 30 | 20
2 | 50 | 60 | 20
3 | 60 | 0 | 40
The average must be determined using a stored procedure, not a normal query.
I have this SQL query
select
id,
(ifnull(one, 0) + ifnull(two, 0) + ifnull(three, 0)) /
((one is not null) + (two is not null) + (three is not null)) as average
from table
I want that to look like this, with a MySQL query:
Id | Average
---+--------
1 | 20
2 | 43.3
3 | 50
Maybe not the best solution but you could use:
select id,
SUM(coalesce(one,0)+coalesce(two,0)+coalesce(three,0)) /
count(CASE WHEN one != 0 and one is not null then 1 ELSE NULL END)
+ count(CASE WHEN two != 0 and two is not null then 1 ELSE NULL END)
+ count(CASE WHEN three != 0 and three is not null then 1 ELSE NULL END ) as average
from my_table
group by id;
Result:
id average
1 20.0000
2 43.3333
3 50.0000
4 35.0000
Demo
This query excludes Null and 0 values
coalesce
Full Procedure
DELIMITER//
CREATE PROCEDURE average()
BEGIN
select id, SUM(coalesce(one,0)+coalesce(two,0)+coalesce(three,0)) /(count(CASE WHEN one != 0 and one is not null then 1 ELSE NULL END) + count(CASE WHEN two != 0 and two is not null then 1 ELSE NULL END) + count(CASE WHEN three != 0 and three is not null then 1 ELSE NULL END)) as average from my_table group by id ;
END
DELIMITER ;
Related
I want to get the total maximum number of column CODE which the maximum is defined by the last five digits from mybarcode column.
mybarcode | code | judge | create_date |
-------------+------+--------+-------------+
M71X400001 | 7 | pass |
M71X400002 | 7 | pass |
M71X400005 | 7 | pass |
M71X400010 | 7 | pass |
M81X400001 | 8 | pass |
M81X400002 | 8 | pass |
M81X400007 | 8 | pass |
M91X400001 | 9 | pass |
M91X400003 | 9 | pass |
```
Example:
>The maximum value of 7 from CODE column is 10 ( from M71X4'00010')
>The maximum value of 8 from CODE column is 7 ( from M81X4'00007')
>The maximum value of 9 from CODE column is 3 ( from M91X4'00003')
The result should be 10+7+3=20.
And want display in the result table below.
```
SELECT DAY,
SUM(CASE WHEN judge = 'pass' then 1 else 0 end) pass,
SUM(CASE WHEN judge = 'fail' then 1 else 0 end) fail
**??? as number**
from MYTABLE
where MONTH(create_date) = '04' and YEAR(create_date) = '2019'
GROUP BY DAY
Result Table
day | pass | fail | number |
--------+------+--------+----------+
1 | 9 | 0 | 20 |
2 | 9 | 0 | ?? |
3 | 9 | 0 | ?? |
I think you need to do group by two times. Please try below code -
For MySQL -
SELECT
DAY,
SUM(pass),
SUM(fail),
SUM(max_barcode)
FROM (
SELECT
DAY,
SUM(CASE WHEN judge = 'pass' then 1 else 0 end) pass,
SUM(CASE WHEN judge = 'fail' then 1 else 0 end) fail,
Code,
CAST(MAX(SUBSTRING(mybarcode, 5)) AS SIGNED) AS max_barcode
FROM MYTABLE
WHERE MONTH(create_date) = '%s' and YEAR(create_date) = '%s'
GROUP BY DAY, Code
) AS CTE
GROUP BY DAY;
FOR MS SQL Server -
;WITH CTE AS (
SELECT
DAY,
SUM(CASE WHEN judge = 'pass' then 1 else 0 end) pass,
SUM(CASE WHEN judge = 'fail' then 1 else 0 end) fail,
Code,
max_barcode = cast(max(right(mybarcode, 5)) as int)
FROM MYTABLE
WHERE MONTH(create_date) = '%s' and YEAR(create_date) = '%s'
GROUP BY DAY, Code
)
SELECT
DAY,
SUM(pass),
SUM(fail),
SUM(max_barcode)
FROM CTE
GROUP BY DAY;
I'm trying to get the total cost from the table below and if Include_Extra is set, use the value in the corresponding Extra_Seat_Cost column. I'm new to mysql, so any help would be greatly appreciated!
+-----------+-----------------+---------------+
| ID | Cost | Extra_Seat_Cost | Include_Extra |
+-----------+-----------------+---------------+
| 3 | 20 | 15 | 1 |
| 4 | 10 | 5 | 0 |
+----+------+-----------------+---------------+
The result should yield total = $45
You can use SUM with CASE WHEN:
SELECT SUM(Cost + CASE WHEN Include_Extra = 1 --if Include_Extra is bool delete = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
SqlFiddleDemo
I've added COALESCE in case Extra_Seat_Cost can be nullable. number + NULL produces NULL.
If you have grouping column use:
SELECT group_column, SUM(Cost + CASE WHEN Include_Extra = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
GROUP BY group_column;
The query below returns null rows in the output. We can avoid null rows if I've two separate queries. But is there a better approach to avoiding null rows?
SELECT date_part('h',convert_timezone('UTC+05:30', value)) as h ,
count (CASE WHEN cond1 THEN 1 else null END) AS "result1",
count (CASE WHEN cond2 THEN 1 else null END) AS "result2"
FROM table_name
WHERE conds
GROUP BY cols
Expected Output:
h | result1 | result2
1 | 23 | 51
2 | 45 | 100
Actual Output:
h | result1 | result2
| 0 | 0
| 0 | 0
1 | 23 | 51
If you don't want another query, then try this approach:
SELECT date_part('h',convert_timezone('UTC+05:30', value)) as h ,
count (CASE WHEN cond1 THEN 1 else null END) AS "result1",
count (CASE WHEN cond2 THEN 1 else null END) AS "result2"
FROM table_name
WHERE conds
GROUP BY cols
HAVING date_part('h',convert_timezone('UTC+05:30', value)) is not null
In this example, you could alternatively expand your where conditions to include the same test for not null and forego using HAVING clause.
This is my table:
id | num | comment
---+-----+--------
3 | 10 | hello
3 | 20 | pls
3 | 30 | respond
7 | 10 | leet
7 | 20 | hax
7 | 30 | zor
How can I query it out in this manner:
id | first | second | third
---+-------+--------+--------
3 | hello | pls | respond
7 | leet | hax | zor
In the event that the num column does not reliably always start at 10 and ascend by 10 you can use the following to establish a row number that restarts at each change in ID, that way you can use the rownumbers in conjunction with conditional aggregation to show each comment. The following would do so for up to 10 comments per ID, and the NUM column does not have to be 10/20/30/40/50/60/70/80/90 (it could be anything).
If the NUM column reliably starts at 10 and ascends by 10, this question has been asked and answered: How to pivot rows into columns (custom pivoting)
select id,
max(case when row_number = 1 then comment else null end) as c01,
max(case when row_number = 2 then comment else null end) as c02,
max(case when row_number = 3 then comment else null end) as c03,
max(case when row_number = 4 then comment else null end) as c04,
max(case when row_number = 5 then comment else null end) as c05,
max(case when row_number = 6 then comment else null end) as c06,
max(case when row_number = 7 then comment else null end) as c07,
max(case when row_number = 8 then comment else null end) as c08,
max(case when row_number = 9 then comment else null end) as c09,
max(case when row_number = 10 then comment else null end) as c10
from(
select #row_number := case when #prev_val = id then #row_number+1 else 1 end as row_number,
id,
comment,
#prev_val:=id as prev_val
from tbl, (select #row_number:=0,#prev_val:='') x
order by id, num) x
group by id
order by id
cusID | Name | status | Date
---------------------------------
1 | AA | 0 | 2013-01-25
2 | BB | 1 | 2013-01-23
3 | CC | 1 | 2013-01-20
SELECT COUNT(cusID) FROM customer WHERE STATUS=0;
SELECT COUNT(cusID) FROM customer WHERE STATUS=1;
Is there a way of combing such two sql and return the results as one. Because want to avoid calling to DB everytime. I tried UNION of two statments, but only showing one result.
This is the shortest possible solution in MySQL.
SELECT SUM(status = 1) totalActive,
SUM(status = 0) totalInactive
FROM tableName
SQLFiddle Demo
and this is the CASE version
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) totalActive,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) totalInactive
FROM tableName
SQLFiddle Demo