to group my results by varying distances from an object - postgresql

I created a successful query that counts the number of objects + average the prices of the counted objects. the query looks like this:
select count (product_id), avg (price)
from current_table
Now, I want to group the results by DISTANCE column that is already exist in the table BUT IN PARTICULAR DISTANCE GROUPING like this:
groups of 0-10 km, 10-20 km, 20-30 km etc. lets say about 10 different distance groups.
How can I do it?
thanks!!!!

Related

Select random rows from Postgres table weighted towards value in column

Users are presented with 2 random items from an assets table to vote on.
There is a votes_count column in the assets table that counts each time users vote.
When choosing the 2 random items, I'd like to weight that more towards a lower value in votes_count. So items with a lower vote count have a higher probability of being selected randomly.
How can I do this with Postgres?
I've used various methods for selecting random records (RAND(), TABLESAMPLE BERNOULLI(), TABLESAMPLE SYSTEM()), but those don't have the weighting that I'm after.
I'm running PostgreSQL 13, FWIW.
I suggest you the following query :
SELECT *
FROM assets
ORDER BY random()/power(votes_count,1) DESC
LIMIT 2
It orders in a random way the rows of your table while dividing by the votes_count put the lower votes in a better position than the higher votes in the DESC order.
Replacing the 1 power by any real number n will increase the probability of selecting the lower votes when n > 1, and decrease the probability of selecting the lower votes when 0 < n < 1.

Tableau: how to make a count if in a for loop?

I'm just starting off Tableau and would like to do a count if in a for loop.
I have the following variables:
City
User
Round: takes values of either A or B
Amount
I would like to have a countif function that shows how many users received any positive amount in both round A and round B in a given city.
In my dashboard, each row represents a city, and I would like to have a column that shows the total number of users in each city that received amounts in both rounds.
Thanks!
You can go for a simple solution that works.
Create a calculated field called "Positive Rounds per User" using the below formula:
// counts the number of unique rounds that had positive amounts per user in a city
{ FIXED [User], [City]: COUNTD(IIF([Amount]>0, [Round], NULL))}
You can use the above to create another calculated field called "unique users":
// unique number of users that have 2 in "Positive Rounds per User" field
COUNTD(IIF([Positive Rounds per User]=2, [User], NULL))
You can combine the calculation of 1 and 2 into one but it gets complex to read so better to split them up

Power BI: Finding average of averages and STDEV.P of averages

All,
My overall objective is to find outliers within an aggregated data set vs the underlying detail for different date ranges. The issue I am having is that Power BI is averaging the SalesPerDay and finding the STDEV.P at the daily level which is the grain of the raw data. I need to first find the average Sales, then find the average of those averages for that "rolled up" data set. Same with STDEV.P. Need to find the STDEV of the "rolled up" averages. Screenshot below depicting how I need the tool to aggregate.
I have brought the Sales column into my dashboard, dimentionalized by user, and set to AVERAGE to get average SalesPerDay.
Then I created the new measure
newavg = CALCULATE(AVERAGE(SalesPerDay[Sales]),ALLSELECTED())
Which is finding the overall average, but at the daily level vs the aggregated level.
I also tried
newSTDV = CALCULATE(STDEV.P(AVERAGE(SalesPerDay[Sales])),ALLSELECTED())
But you cannot find the STDEV.P of a calculation.
Thank you.
What you are looking for is the iterator functions, which take a table or column of data as a grouping, and then applies a calculation on that group.
Example of one would be SUMX. In the example below, it would do a grouping based on Product. Within each product it would get the total of qty and multiply it by the sum of x. It would then sum the results of that calculation into a total.
SUMX( VALUES( table1 [ Product ] ), [Qty] * [x] )
There also being averagex, minx, maxx, plus for the statistical functions there is STDEVX.P and STDEVX.S

P&L: Sum subgroups of a division (revenue/expense) and then subtract to get net profit

I'm trying to create a basic P&L. I've put together the following table:
I'd like to sum up Total Expenses and Revenues by year and then subtract the two to get Net profit. I know I can use the show totals section to see Total Revenue and Expenses, but I want to learn the principal of summing up subgroups of dimensions (in this case the dimension is Category, and the subgroups are Revenue and Expense).
Ultimately, I'd like to insert a net profit at the bottom of the table. I've tried many LOD expressions to no avail.
The net profit should be:
dropbox link to workbook: https://www.dropbox.com/s/nyjo1urzjke1p5x/Animals.twb?dl=0

Calculate the Average of multiple Averages

I need to calculate the average of multiple averages within Crystal Reports with specific criteria. My report averages scores for multiple courses by a developer. I need to get the average value of the averages, based on the number of courses for that developer:
Course 1 10 responses Course 1 Average 42.86
Course 2 12 responses Course 2 Average 39.36
How do I create a formula that give me the correct Average ((42.86 + 39.36)/2) of those two courses for that developer?
Try a Running Total for that field, where the Type of Summary is set to "Average". Specify that the total should reset every time it reaches a new Developer group.
Sometimes Crystal won't let you summarize a summary. If it gives you guff, you can achieve the same effect with Shared Variables. Simply add each average in turn to the shared variable, then divide by a distinct count of averages. The final Formula field would look something like:
Shared NumberVar sum;
Shared NumberVar count;
sum/count