I have a Table Like so
user_id
status
1
true
1
true
2
false
2
true
Im Trying to Find the distinct count user_id where They are true and where they are false.
the query i have now is
SELECT
COUNT( DISTINCT CASE WHEN `status` = 'true' THEN 1 END ) AS `trues`,
COUNT( DISTINCT CASE WHEN `status` = 'false' THEN 1 END ) AS `false`,
FROM table
What is the difference between just removing Distinct from count and doing
SELECT DISTINCT
COUNT( CASE WHEN `status` = 'true' THEN 1 END ) AS `trues`,
COUNT( CASE WHEN `status` = 'false' THEN 1 END ) AS `false`,
FROM table
From the MySQL docs...
count
Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement.
count(distinct)
Returns a count of the number of rows with different non-NULL expr values.
For example, if you have 1, 1, 1, 2, 3, null count will count 1,1,1,2,3 for 5 and count(distinct) will count 1,2,3 for 3.
select distinct
DISTINCT specifies removal of duplicate rows from the result set.
The key difference between select distinct count() and select count(distinct) is with select count(distinct) the distinct filter happens while counting, with select distinct count() the distinct filter happens after counting.
SELECT
COUNT( DISTINCT CASE WHEN `status` = 'true' THEN 1 END ) AS `trues`,
COUNT( DISTINCT CASE WHEN `status` = 'false' THEN 1 END ) AS `false`
FROM table;
This will always be 1 or 0.
The case statements will always result in 1 or null, so the results can only be something like 1,1,null,1,null. There's only 1 distinct non-null value to count, so the result is 1. If there's no matches it's null, null, null, there's no distinct non-null values so the result is 0.
SELECT DISTINCT
COUNT( CASE WHEN `status` = 'true' THEN 1 END ) AS `trues`,
COUNT( CASE WHEN `status` = 'false' THEN 1 END ) AS `false`,
FROM table
Here, because there is a single row, the distinct has no effect; there's only one row and it's always going to be distinct.
But if there were multiple rows, perhaps because there's a group by, it would only return those results which are distinct. For example, let's say without the distinct we get...
trues false
------------
1 1
1 1
1 2
2 2
2 2
Then select distinct will only return those rows which are distinct.
trues false
------------
1 1
1 2
2 2
Try it.
Related
I have a query:
select AVG(tickets_number)
from TABLE_ONE
where ofd_id = :ofdId and launch_id in
(select id from TABLE_TWO where is_actual = 1 and is_matching_completed = 1)
But I need to calculate average value only if there are at least 10 rows from this query, otherwise I have to return NULL.
How can I do this?
Try this case expression.
select CASE WHEN COUNT(tickets_number) >= 10
THEN AVG(tickets_number)
ELSE NULL END average
from TABLE_ONE ...
The aggregate query groups by attribute A3 and then performs a COUNT(A4) but it doesn't consider the NULL values in the attribute A4.
For a regular count, don't include the column name:
count(*)
For count distinct, just add the extra value back in:
count(distinct a4) + (case when count(a4) <> count(*) then 1 else 0 end)
This can be simplified in MySQL to:
count(distinct a4) + (count(a4) <> count(*))
Or, if you know there is value that won't exist in the column:
count(distinct coalesce(a4, ' <NULL>'))
I am trying replace null with 0 with following statement but returns no recrods instead of of catid supplied and 0.
select ifnull(count(*),0) as days, catid from mytable where Id=48 and catId=7
group by mytable.catId;
As far as I know, COUNT(*) does never return NULL. It returns 0 if there is no record.
count(*) never returns NULL, so you don't need any conditional logic:
select count(*) as days, catid
from mytable
where Id = 48 and catId = 7
group by mytable.catId;
Perhaps your issue is that the query is returning no rows. If so, you can leave out the group by. Then the query will always return one row:
select count(*) as days, catid
from mytable
where Id = 48 and catId = 7 ;
So I'm trying to write a mysql script to find the number of consecutive repeats in 'value' column of this table.
id value result
-- ----- ------
1 1 0
2 1 1
3 2 0
4 3 0
5 3 1
So in this case I want get the value 2
Get the next value using user variables,
GROUP so consecutive values more than 2 are not counted again,put all in a subquery,and use a simple CASE to increment the value you need in case value=next value.Add salt and pepper.
SELECT SUM(CASE WHEN y.value=y.next_value THEN #var+1 ELSE #var END) consecIds
FROM
(SELECT t.id, t.value, next_id, n.value next_value
FROM
(
SELECT t.id, t.value,
(
SELECT id
FROM table1
WHERE id > t.id
ORDER BY id
LIMIT 1
) next_id
FROM table1 t,(SELECT #var:=0)x
) t LEFT JOIN table1 n
ON t.next_id = n.id
GROUP BY t.value,n.value)y
FIDDLE
SELECT COUNT(DISTINCT column_name) FROM table_name;
DISTINCT will erase duplicated repetitions from specified column in result.
COUNT will count the rows in result.
The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column.
I have a a table with a column groups INTEGER NULL. It has values
groups
5
7
<NULL>
If I do a select sum(groups) form table_name
I would get 12. How can I get null, when the column being summed has a null.
One option:
CASE WHEN COUNT(*) = COUNT(groups) THEN SUM(groups) ELSE NULL END
select
case when exists (select groups from table where groups is null) then null
else select sum(groups) from table
end as grp_sum