How can I find Average accurately in my query - postgresql

I want average but its showing null. I want the average of score present in different ids
select avg(score*100)from daily_stats1 where id=10 and id=11

Just get rid of null values using coalesce function:
select avg(coalesce(score, 0)*100)from daily_stats1 where id=10 and id=11

Related

How to get SUM and AVG from a column in PostgreSQL

Maybe I'm overlooking something, but none of the answers I found solve my problem. I'm trying to get the sum and average from a column, but everything I see is getting sum and average from a row.
Here is the query I'm using:
SELECT product_name,unit_cost,units,total,SUM(total),AVG(total)
FROM products
GROUP BY product_name,unit_cost,total
And this is what I get:
It returns the exact same amounts. What I need is to add all values in the unit_cost column and return the SUM and AVG of all its values. What am I missing? What did I not understand? Thank you for taking the time to answer!
AVG and SUM as window functions and no grouping will do the job.
select product_name,unit_cost,units,total,
SUM(total) over all_rows as sum_of_all_rows,
AVG(total) over all_rows as avg_of_all_rows
from products
window all_rows as ();
The groups in your query contain just one row if total is a distinct value. This seems to be the case with your example. You can check this with a count aggregate (value = 1).
Removing total and probably unit_cost) from your select and group by clause should help.

Finding median value per category

I have a data table with columns "field" and "conc" (which is short for concentration). I am trying to output the each type of field (the categories are cosmos, egs etc) along with the associated median value of the conc statistic for each field type.
This is what I have tried:
SELECT field, percentile_cont(0.5)::numeric FROM galaxies GROUP BY conc LIMIT 5;
ERROR: function percentile_cont(numeric) does not exist
LINE 1: SELECT field, percentile_cont(0.5)::numeric FROM galaxies GR...
However, I am getting this error and am not exactly sure how to go about extracting the field name with the median value for conc for each field type
As indicated in the documentation for PostgreSQL, WITHIN GROUP (ORDER BY...) is mandatory for the ordered-set aggregate functions like percentile_cont.
If you want the median of the conc, then grouping by conc is surely the wrong thing to do. So you might want something like this:
SELECT field, percentile_cont(0.5) within group (order by conc)::numeric
FROM galaxies GROUP BY field LIMIT 5;

Using COUNT in Tableau to count observations by group

Thanks in advance for any advice you can offer! I'm building a Tableau dashboard to explore housing affordability and school quality in different neighborhoods in my area. A user will select their occupation and see a graph of neighborhoods plotted based on school quality and housing affordability. To explore housing affordability, I'm using county level assessor data with the valuation of every property matched to neighborhoods.
The goal is to display the percentage of homes in an area that are affordable given the median occupational wages for the job a user selected. Right now, I'm trying to use a calculated field with COUNT([Parcels]<[Occupation])/COUNT([Parcels]), but I need to find a way to count the number of properties in each specific neighborhood below the cut off value.
Does anyone know of a way to count elements of a particular group in this way in Tableau?
I'm on a Mac, using Tableau Desktop, and doing the back end analysis work in R. Thank you!
You seem to misunderstand what the function COUNT() does. You are certainly not alone. Count() behaves in Tableau almost identically to how it does with SQL.
Count([some field]) returns the number of data rows where the value for [some field] is not null. It does not not return the number of rows where [some field] evaluates to true, or a positive number, or anything else.
If [some field] always has a non-null value, then Count([some field]) is the same as SUM([Number of Records]). If [some field] is always null, then Count([some field]) is zero. Count() is not like Excel's CountIf function.
If you want to count data rows that meet a condition, you could try COUNT(if [condition] then 1 end) Since the missing ELSE case defaults to null values, that expression will count rows where [condition] is true.
So one way to get the percentage of affordable homes is count(if [affordable] then 1 end) / count(1) assumes each Data row represents a home. Then format your field to display as a percentage. Another option is to learn to use quick table calcs
If you want to display the number of rows in a given visualized table you could also use SIZE()
Source, official docs:
https://help.tableau.com/current/pro/desktop/en-us/functions_functions_tablecalculation.htm#size

Accessing current row value with lag function

I want to calculate the difference between the previous and the current column and make it a new column named increase. For this, I'm using the lag window function. The value of the first column is not defined since no previous column exists. I know that a 3rd parameter specifies the default value. However, it depends. For the first row, I want to use the value of another column e.g. the one of count from that current row. This assumes that 0 is increased to count for the first row which is what I need. Specifying the column name as 3rd argument for the lag function does not work correctly and neither does using 0. How can it be done? I'm getting strange results such as quite a random result or even negative numbers.
SELECT *, mycount - lag(mycount, 1) OVER (ORDER BY id, messtime ASC) AS increase FROM measurements;
Window functions cannot be nested either:
ERROR: window function calls cannot be nested
There is another issue with your query: So far your results are in random order, so you may think you are seeing problems that don't exist.
Add ORDER BY id, messtime to your query to see the rows in order. Now you can compare one row with its predecessor directly. Are there still issues? If so, which exactly?
SELECT *, "count" - lag("count", 1) OVER (ORDER BY id, messtime) AS increase
FROM measurements
ORDER BY id, messtime;
COUNT is a reserved word in SQL. It seems the DBMS thinks you want to nest COUNT and LAG somehow.
Use another column name or use quotes for the column:
SELECT *, "count" - lag("count", 1) OVER

iPhone sqlite desc vs asc?

How come in my code when i use #"select Score from tblMed order by Score desc limit 10" everything works fine, but when I change "desc" to "asc" the scores don't show up???
Because:
ORDER BY score ASC
...means that the lowest values will be at the top of the list - adding LIMIT 10 to the query will return the first 10 records with the lowest scores, so I imagine you have entries with score values of zero and/or null.
Do you have NULL scores in your records?
If you change the ordering to asc, then it would start with NULLs before selecting records with an actual value.