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
Related
Picture 1 is the table containing the data
Picture 2 is my command
Why is it only returning 1 row and not the same for all the subjectIDs?
How do I make it return a row for each subjectID?
Thanks
p.s Please keep things simple I need to do this with basic sql.
You are running an AVG command. This will aggregate the results, since an average has to be run on multiple rows of data.
If you want to have it grouped in a different way, you can do this with a GROUP BY clause. This will return a row for every distinct values for the column(s) specified in the GROUP BY clause, with the calculated average and so on.
It would look similar to the following:
SELECT subjectid, AVG(result)
FROM Results
GROUP BY subjectid
could you please explain why mysql count function without providing any table name gives 1 as value?
SELECT COUNT(*);
Result: 1
Because in mysql select constant_value command is valid (such as select 2 will return 2) and will return 1 row. Count() function without group by will collapse the resultset and count the number of items in the resultset. In this case 1 row would be returned and count(*) counts that.
Normally all selects are of the form SELECT [columns, scalar computations on columns, grouped computations on columns, or scalar computations] FROM [table or joins of tables, etc]
Because this allows plain scalar computations we can do something like SELECT 1 + 1 FROM SomeTable and it will return a recordset with the value 2 for every row in the table SomeTable.
Now, if we didn't care about any table, but just wanted to do our scalar computed we might want to do something like SELECT 1 + 1. This isn't allowed by the standard, but it is useful and most databases allow it (Oracle doesn't unless it's changed recently, at least it used to not).
Hence such bare SELECTs are treated as if they had a from clause which specified a table with one row and no column (impossible of course, but it does the trick). Hence SELECT 1 + 1 becomes SELECT 1 + 1 FROM ImaginaryTableWithOneRow which returns a single row with a single column with the value 2.
Mostly we don't think about this, we just get used to the fact that bare SELECTs give results and don't even think about the fact that there must be some one-row thing selected to return one row.
In doing SELECT COUNT() you did the equivalent of SELECT COUNT() FROM ImaginaryTableWithOneRow which of course returns 1.
Reference
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.
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)
could you please explain why mysql count function without providing any table name gives 1 as value?
SELECT COUNT(*);
Result: 1
Because in mysql select constant_value command is valid (such as select 2 will return 2) and will return 1 row. Count() function without group by will collapse the resultset and count the number of items in the resultset. In this case 1 row would be returned and count(*) counts that.
Normally all selects are of the form SELECT [columns, scalar computations on columns, grouped computations on columns, or scalar computations] FROM [table or joins of tables, etc]
Because this allows plain scalar computations we can do something like SELECT 1 + 1 FROM SomeTable and it will return a recordset with the value 2 for every row in the table SomeTable.
Now, if we didn't care about any table, but just wanted to do our scalar computed we might want to do something like SELECT 1 + 1. This isn't allowed by the standard, but it is useful and most databases allow it (Oracle doesn't unless it's changed recently, at least it used to not).
Hence such bare SELECTs are treated as if they had a from clause which specified a table with one row and no column (impossible of course, but it does the trick). Hence SELECT 1 + 1 becomes SELECT 1 + 1 FROM ImaginaryTableWithOneRow which returns a single row with a single column with the value 2.
Mostly we don't think about this, we just get used to the fact that bare SELECTs give results and don't even think about the fact that there must be some one-row thing selected to return one row.
In doing SELECT COUNT() you did the equivalent of SELECT COUNT() FROM ImaginaryTableWithOneRow which of course returns 1.
Reference