Finding the Average of a Column With Multiple Group Bys PostgreSQL - sql

I am new to Postgres and would like to query the following table to get the average % of the total allocation at a user level from the following table:
e.g. I would like to get
Column 1 Columns 2
Label Avg Percent of the total from each user for a given label
Chickens 0.347
Goats 0.335
Does anybody understand what I am asking and would be able to point me in the right direction?

Are you just looking for group by?
select label, avg(percent_of_total)
from t
group by label;

Related

Calculating the mode/median/most frequent observation in categorical variables in SQL impala

I would like to calculate the mode/median or better, most frequent observation of a categorical variable within my query.
E.g, if the variable has the following string values:
dog, dog, dog, cat, cat and I want to get dog since its 3 vs 2.
Is there any function that does that? I tried APPX_MEDIAN() but it only returns the first 10 characters as median and I do not want that.
Also, I would like to get the most frequent observation with respect to date if there is a tie-break.
Thank you!
the most frequent observation is mode and you can calculate it like this.
Single value mode can be calculated like this on a value column. Get the count and pick up row with max count.
select count(*),value from mytable group by value order by 1 desc limit 1
now, in case you have multiple modes, you need to join back to the main table to find all matches.
select orig.value from
(select count(*) c, value v from mytable) orig
join (select count(*) cmode from mytable group by value order by 1 desc limit 1) cmode
ON orig.c= cmode.cmode
This will get all count of values and then match them based on count. Now, if one value of count matches to max count, you will get 1 row, if you have two value counts matches to max count, you will get 2 rows and so on.
Calculation of median is little tricky - and it will give you middle value. And its not most frequent one.

Can you sum rows from one query's result set and use that total in another query?

I have a query that will sum scores from the top users of a website:
SELECT SUM(score)
FROM hacker_news
GROUP BY user
HAVING score > 200;
This produces a result set with 4 rows with 4 integers.
I want to sum these 4 rows and divide it by the sum of scores from the entire table.
I have tried running it using a subquery and a temporary
any help in writing the simplest query possible would be much appreciated. Ideally I'd like to know if this doable without creating a temporary table and joining
If you want the proportion of totals scores based on those with more than 200, then you can use conditional aggregation:
SELECT SUM(CASE WHEN score > 200 THEN score ELSE 0 END) / SUM(score)
FROM hacker_news

How to get Min Max as out put from single column grouped by user in select query

I am not good in SQL raw queries. Please help to get Min Max value grouped by the user_id.
Scenario is:
Table has multiple values in column "B" against user_id and i want to out put the column "B" twice as Min/Max in single output. Required output will looks like:
User_id, MaxB, MinB
1 5 2
2 10 3
Consider the Table has 5 entries for both user(1, 2) but the out put prints it as a single row with max and min of B. Thanks in advance. Suggest the link if i am repeating this question as i was unable to find the solution.
Select Max(B) as MAXB, Min(B) as MinB, User_ID
from TableName
group by User_ID

sql count or sum?

I'm a newbie programmer, I want to sum a value of employee's attendance record
Anyway, what should I choose? COUNT or SUM?
I tried to use COUNT functions like this...
SELECT COUNT(jlh_sakit) AS sakit FROM rekap_absen
It shows value changed to "1" for 1 Record only.
And I try to use SUM functions like this...
SELECT SUM(jlh_sakit) AS sakit FROM rekap_absen
It shows all values changed ALL value to "1"
I want to display only 1 person for each sum
(e.g : John (2 sick, 2 permissions, 1 alpha)
Can you help me please?
If you are using any aggregate function like min/max/sum/count you should use group by. Now your question says "what should I choose? COUNT or SUM?" Assuming you have person_name, jlh_sakit which means sick/permission/alpha in your case you could use
select person_name, count(jhl_sakit) as attribute
from rekap_absen
group by person
This will give you output like:
person_name attribute
John 2
King 5
In order to sum by specified column, use group by statement.
SELECT SUM(sick),SUM(alpha),SUM(permissions),person FROM rekap_absen group by person
It will group your sums according to person.
You may name your sums like:
SELECT SUM(sick) as sick,SUM(alpha) as alpha,SUM(permissions) as permissions,person FROM rekap_absen group by person
Assuming that you have table rekap_absen with columns: person,sick,alpha,permissions

Retrieve names by ratio of their occurrence

I'm somewhat new to SQL queries, and I'm struggling with this particular problem.
Let's say I have query that returns the following 3 records (kept to one column for simplicity):
Tom
Jack
Tom
And I want to have those results grouped by the name and also include the fraction (ratio) of the occurrence of that name out of the total records returned.
So, the desired result would be (as two columns):
Tom | 2/3
Jack | 1/3
How would I go about it? Determining the numerator is pretty easy (I can just use COUNT() and GROUP BY name), but I'm having trouble translating that into a ratio out of the total rows returned.
SELECT name, COUNT(name)/(SELECT COUNT(1) FROM names) FROM names GROUP BY name;
Since the denominator is fixed, the "ratio" is directly proportional to the numerator. Unless you really need to show the denominator, it'll be a lot easier to just use something like:
select name, count(*) from your_table_name
group by name
order by count(*) desc
and you'll get the right data in the right order, but the number that's shown will be the count instead of the ratio.
If you really want that denominator, you'd do a count(*) on a non-grouped version of the same select -- but depending on how long the select takes, that could be pretty slow.