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.
Related
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 ;
SELECT BVDID, max(case when CompanynameLatinalphabet='' then NULL else CompanynameLatinalphabet end) as CompanynameLatinalphabet, idField1, idField2 FROM `imported_companies_BVD` where BVDID=103327
group by BVDID, CompanynameLatinalphabet
Hi everyone. In the below statement, I am getting:
BVDID | CompanynameLatinalphabet | idField1 | idField2
103327 | NULL | 100 | 1
103327 | CASEIFICIO SOCIALE DI RAV... | NULL | 1
How can I prevent the row with NULL be retrieved? I can not use IS NOT NULL in the where statement, as there are other columns that have data in the row.
The whole idea is merging the whole thing, there are values in BOTH ROWS that should be merged into as single row.
Thanks if you can help!
The expected result should be:
BVDID | CompanynameLatinalphabet | idfield1 | idfield2
103327 | CASEIFICIO SOCIALE DI RAV... | 100 | 1
Why use case statements in max function? If value is blank just return blank instead of null
If you want to exclude the rows with NULL in the column CompanynameLatinalphabet you can do:
select *
from (
SELECT
BVDID,
max(case when CompanynameLatinalphabet='' then NULL
else CompanynameLatinalphabet end) as CompanynameLatinalphabet
FROM `imported_companies_BVD`
where BVDID=103327
group by BVDID, CompanynameLatinalphabet
) x
where CompanynameLatinalphabet is not null
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;
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
I have this table [Table 1]
cid | arrived | date_arrived
The [arrived field can have a value of [T] or [F], the value is [F] the date arrived field is NULL
1 records may appear only up to maximum of 2 (1 record for arrived=T and another record for arrived=F) But there are also records that may appear only once
1 | T | 2012-02-01
2 | F | [Null]
1 | F | [Null]
3 | T | 2012-03-05
I need a query that will show something like this
cid | arrived | not_arrived
1 Yes Yes
2 No Yes
3 Yes No
This works:
SELECT
cid,
SUM(arrived = 'T') > 0 as arrived,
SUM(arrived = 'F') > 0 as not_arrived
FROM [Table 1]
GROUP BY cid;
You can try it here: http://sqlfiddle.com/#!2/2b5a7/1/0
try
select cid,
case when find_in_set('T', group_concat(arrived)) > 0 then 'yes' else 'no' end as arrived,
case when find_in_set('F', group_concat(arrived)) > 0 then 'yes' else 'no' end as not_arrived
from table1
group by cid