How to have a condition within COUNT - mysql

Im trying to get the proportion of two numbers by having a condition within the count clause, but it just returns the same number no matter what the condition is:
SELECT COUNT(type=2)/COUNT(type=3) as Proportion
Is there a way to do this?

You should use SUM instead of COUNT.
SELECT SUM(type=2)/SUM(type=3) as Proportion
because count counts both the failed and true conditions (1&0 are returned for the condition for true&false), while sum sums up only true values.

Related

Difference between SUM() and COUNT() in MySQL

I was solving this Leetcode question(1173. Immediate Food Delivery I) and I am kinda confused on why I am not getting the right answer when using COUNT(). but when I use SUM(), I am getting the right answer. I specify a condition for both SUM and COUNT and from my understanding, they should both be adding or counting when order_date=customer_pref_delivery_date, is that mean the condition only working for SUM()?
Query that return the right answer(33.33):
select round(sum(order_date=customer_pref_delivery_date)/count(delivery_id)*100,2) as immediate_percentage
from Delivery
Query that return the wrong answer(100.00)
select round(count(order_date=customer_pref_delivery_date)/count(delivery_id)*100,2) as immediate_percentage
from Delivery
So basically SUM() function will return the sum of the expression inside of it and COUNT() will returns the number of non-NULL values of elements or rows that the query have returned.
In this case with your query you are looking to get the percentage of the immediate delivery orders so your formula to implement this is ((<numberofimmediate>/<totalamount>)*100). At first you can think that COUNT() would be the right approach, but that actually will return the complete number of elements in your query so to avoid this we will use SUM() to just acumulate the 'ones' that complete the condition ( preferred and estimated are equals so will return 1). If you want to see this in a more visual way you can do:
SELECT delivery_id,order_date=customer_pref_delivery_date FROM Delivery
This will return the delivery id and 1 if the condition is true but 0 if its false. Then you can use both of functions to see what is the effect on each one:
SELECT COUNT(order_date=customer_pref_delivery_date) FROM Delivery;
SELECT SUM(order_date=customer_pref_delivery_date) FROM Delivery;
COUNT()query will return, again, the complete amount of data present in the comparation (Counting 1 and 0)
SUM()query will return the sum of all the values present in the comparation (Adding 1 and 0)
Hope this gives you a better understanding of what's going on with COUNT and SUM on your queries
You can check this fiddle
Obviously, when you choose Count() it counts how many fields have numbers for example: we have 5 rows, every row has a number (random numbers, let's say 1,2,3,4,5) so the count here will be 5 while the Sum() gives you the sum of variables in this field (in our example: 1+2+3+4+5 = 15).
In the query that gives you the wrong answer, you simply divide count by count (for example 5/5 *100) which will always give you 1.
I hope that you understand

SQL STATEMENT Values getting multiplied by 3

SUM(ISNULL(FlagHrs,0.00) + ISNULL(BackFlagSoldHrs,0.00)) AS Ttl_EmpFlagHrs
Using this statement in a select statement but the values I am getting in Ttl_EmpFlagHrs are getting multiplied by 3, Flaghrs and BackFlaghrs are the different columns present in a certain table.
Since there is no sample data or full query, you are forcing us to answer based on speculation.
Based on your question, Objective is to add FlagHrs and BackFlagSoldHrs and when its null, count it as zero.
Since you are using SUM function, which is a group function, result of multiple rows will be added and presented as a single value.
Based on your question, it is highly likely that, single row is repeated 3 times (could be result of a join) and hence sum is showing up as 3 times.
You can verify this by removing SUM function and checking rows actually selected as result of your query.
Solution:
Verify Join condition to ensure correct rows are selected in your query
If theory of adding it up multiple times is true, you can use AVG function in place of SUM function, the result will be equivalent of single row (rather than 3x)
See if you need to use DISTINCT keyword to remove duplicate rows (if its not desired result)

MSSql ISNULL query

select ISNULL(c.name,'any') from (select Name from Orders where ID = '123')
select ISNULL((select Name from Orders where ID = '123'),'any')
Orders table have two columns
1. ID
2. Name
and data in Orders is
ID Name
121 abc
124 def
First Query is not returning any result whereas second query is giving any as result. What is the difference
The first form uses a subquery as a table source, in its FROM clause; it can return between zero and many rows.
For each of the rows that the subquery returns, the ISNULL expression is evaluated. But if the subquery returned no rows, then the final output contains no rows.
The second form uses a SELECT without a FROM clause - which will always produce a result set containing exactly one row. It then also uses a scalar subquery (by introducing a subquery in a location where a scalar value is expected) - that has to either produce zero or one results. If the subquery produces zero results, then NULL is substituted.
So, the differences between the two are that the first query can return between zero and many rows, and the ISNULL expression is evaluated for each row. Whereas the second query always produces exactly one row, and if the subquery returned multiple results, an error is produced.

max,min aggregate functions in mysql does not returns row assosiated with that max or min value

I am using max function in query. It returns me max value for that column but it not returns other columns associated with that max value. It returns max value for that column and for other columns returns values from first row in which where clause matched. I find way through order by desc and limit 1 but Is it right way for that ?
Any help in finding right way. Thanks in Advance

MySQL: Count two things in one query?

I have a "boolean" column in one of my tables (value is either 0 or 1).
I need to get two counts: The number of rows that have the boolean set to 0 and the number of rows that have it set to 1. Currently I have two queries: One to count the 1's and the other to count the 0's.
Is MySQL traversing the entire table when counting rows with a WHERE condition? I'm wondering if there's a single query that would allow two counters based on different conditions?
Or is there a way to get the total count along side the WHERE conditioned count? This would be enough as I'd only have to subtract one count from the other (due to the boolean nature of the column). There are no NULL values.
Thanks.
You could group your records by your boolean column and get count for each group.
SELECT bool_column, COUNT(bool_column) FROM your_table
WHERE your_conditions
GROUP BY bool_column
This will obviously work not only for bool columns but also with other data types if you need that.
Try this one:
SELECT
SUM(your_field) as positive_count,
SUM(IF(your_field, 0, 1)) as negative_count
FROM thetable
If they are all either 0 or 1 and you dont mind 2 rows as result you can group by that field and do a count like so:
select field, count(field)
from table
group by field
A simple group clause should do the trick :
SELECT boolField, COUNT(boolField)
FROM myTable
GROUP BY boolField