How to create a crosstable showing descriptive statistics in PostgreSQL? - postgresql

I have a table contains data of year and value. I'd like to create a cross table with all the year as column headings and calculate descriptive statistics in the rows.
For instance, I'd like to calculate the mean and median value for year 2005, 2006, and 2007 separately and put them in the following table format.
2005 2006 2007
Median
Mean
To calculate the median and mean for year 2005, the code would be:
SELECT avg(vallue),percentile_cont(0.5) WITHIN GROUP (ORDER BY value)
FROM tablename WHERE year=2005;
but I don't know how to turn it into a table

in postgres "crosstab" is extension for pivot tables
for median I used this aggregate function and it was quite effective (fast and precise)

Related

Cumulative Values on Missing Data Quicksight

I'm trying to create a cumulative quarterly count divided by week. I have the underlying table that looks like this:
Date
Id
01/01/2022
X
02/01/2022
Y
The result is this
Screenshot of the problem](https://i.stack.imgur.com/9tmFA.png)
There are some white spaces due to particular weeks where there are no data. I would like to fill these blank spaces with the value of the week before (as it should be on a cumulative count) and with zero if it's the first week of the quarter.
Is there a way to solve this problem without creating any support table or any "fake data" to avoid the cell to be blank?

Spotfire Average over a column

I have a question related to Spotfire. I have a table
And what I need to get is an Average of the rate (Operational Costs/Production) grouped by Region. Mathematically it looks like this for Europe: ((Costs A 2017 + Costs B 2017)/(Production A 2017 + Production B 2017) + (Costs A 2018 + Costs B 2018)/(Production A 2018 + Production B 2018) + (Costs A 2019 + Costs B 2019)/(Production A 2019 + Production B 2019)) / 3
and do the same for Asia. So basically, I need to calculate the rate for the Region and find an average over the years. can't figure out, how to do this.
#user2538414- I would suggest you to add a pivot transformation to your original table to make it more readable and easy to work with.
Please check the below post which explains how to pivot and unpivot data tables.
Spotfire - How to group multiple columns under a single column in data table visualization
Transformed Table:
To this transformed table you can add a calculated column with the custom expression below to compute Rate (Costs/Production) by Region and year.
Sum([Costs]) over (intersect([Region],[Year])) / Sum([Production]) over (intersect([Region],[Year]))
You can further refine this custom expression to suit your requirement.
Final output table:

How to plot two sets of points on one scatter plot in Tableau

I have a data set that includes employee name (rows) and # of calls in 2016, 2016 revenue, # of calls in 2017, and 2017 revenue for each employee. Is there a way to plot (employee's 2016 calls, employee's 2016 revenue) AND (employee's 2017 calls, employee's 2017 revenue) on one scatter plot in Tableau?
I've tried to plot 2016 calls and 2017 calls (columns) on a dual axis and 2016 revenue and 2017 revenue (rows) on a dual axis, but it ends up plotting all possible (X,Y) combinations, when there should only be two points per employee (2016 data and 2017 data).
Alternatively, is there a way to plot a line through each of the 2016 points if I've calculated the slopes of the lines I want to use?
If you want one mark for each combination of employee and year, the simplest solution is to shape your data source to have one data row for each combination of employee and year.
So revise your data source to have the following columns:
Employee
Year
Calls
Revenue
Then try Employee on Detail, Year on color (as a dimension), SUM(Calls) on Columns and Sum(Revenue) on Rows
If you don't want to reshape you data as a preprocessing step, you can accomplish the same goal in the Data Source editing panel by UNIONing the original source with itself, add a Year field based on the Table Name field and creating calculated fields to have versions of Calls and Revenue that include the values only for that one year.

compute difference between 2 ranked columns in tableau

I am trying to find the difference between 2 columns in tableau. The catch though here is that each column is ranked based on a value. The difference i need is between these 2 ranked columns
The rank is computed using the table calculations rank function. Attaching the picture for more information
I am assuming "current" and "prior" are calculated fields.
Just create a new calculated field, here I'll call it "Result". In this field just minus your one from the other so:
[Current - Prior]
Then pull this new field into your measures values on your sheet.

How to Calculate YTD (Jan to prev month) in a single column in tableau

Original post - https://community.tableau.com/thread/206909
I have a report in which I have sales per month in the column and commodities in the row. The data show actual sales and future estimates for each month.
Need to calculate Year-To-Date (YTD) total for 2016 (from Jan to Previous month) and have it in a single column at the end of the actual values.
I already created a calculated field - YTD
IF
YEAR([DATE]) = YEAR(NOW())
AND
MONTH([DATE])< MONTH(NOW())
THEN
[VALUE/UNIT]
ELSE
0
END
But when I add to the view, it creates a another section for YTD with sum for each month till April.
Can someone please help me in how to achieve this in Tableau?
There are couple of ways to achieve this view in Tableau
1. To create calculated field for each Month and YTD and add measure names in Row & Measure values in Text
2. Make union of 2 queries - one that select all the correct values & second that have YTD calculation in month column. Then use pivot it