calculating percentage of average in tsql - tsql

I have a set of records associated with IDs. there may be any number of records in this association, with currency values. one of these values is flagged as selected. I need to calculate an average of all the associated currency values, then take a percentage between this average and the lowest value, grouped by ID. all the data needed is in one table:
input:
table x: ID, Selected, DollarAmt
output:
view y: ID, Average, Percentage
I'm having problems creating this query(view) and it's driving me nuts. can anyone at least point me in the right direction?
Thanks all.

You can use this query:
select Id,
AVG(DollarAmt) Average,
AVG(DollarAmt)/MIN(DollarAmt) Percentage
from TableX
group by Id
But i´still don´t understand the need of the "Selected" variable in TableX
Regards

Related

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

Displaying change in moving average on map

I am trying to show the change in moving average by county on a map.
Currently, I have the calculated field for this:
IF ISNULL(LOOKUP(SUM([Covid Count]),-14)) THEN NULL ELSE
WINDOW_AVG(SUM([Covid Count]), -7, 0)-WINDOW_AVG(SUM([Covid Count]), -14, -7)
END
This works in creating a line graph where I filter the dates to only include 15 consecutive dates. This results in one point with the correct change in average.
I would like this to number to be plotted on a map but it says there are just null values.
The formula is only one part of defining a table calculation (a class of calculations performed client side tableau taking the aggregate query results returned from the data source)
Equally critical are the dimensions in play on the view to determine the level of detail of the query, and the instructions you provide to tell Tableau how to slice up or layout the query results before applying the table calc formula. This critical step is known as setting the “partitioning and addressing” for the table calc, sometimes also as setting the “compute using”. Read about it in the online help for table calcs. You can experiment with using the Edit Table Calc dialog by clicking on the corresponding pill.
In short, you probably have to a dimension, such as your Date field to some shelf - likely the detail shelf, and the set the partitioning and addressing, probably to partition by county and address by state.
If you have more than a couple of weeks of data, then you’ll get multiple marks per county. You may need to decide how to handle that on your map.

Pivot table with each column having the previous columns value deducted from it

I have a pivot table with a dimension of 'companyname', column of 'month' and a measure of SUM(number_of_sales).
I want to change the measure expression so each column shows the number of sales minus the number of sales in the previous month, so sort if the opposite of a cumulative, the idea being to show how the number of sales per month has increased month on month.
How would i go about this, i'm not even sure where to begin?
Was thinking maybe something like
SUM(IF(column=column(1),number_of_sales,IF(Column()=column(2),
But im having no luck.
Is anyone able to point me in the right direction?
Thanks!
Try to use Above function:
SUM(number_of_sales) - (Above(Sum(number_of_sales),1,1)).

How do I make a calculated field for total cost by quarter and location in Tableau?

I have Tableau 9.1 and have the costs per transaction in a column over the course of 3 years. I also have the dates and locations for all the transactions.
How do i make a calculated field for total cost at a location for a given quarter assigned to each observation?
Generally that depends on how you want to display these values.
An easy way would be via a LOD calculation:
{fixed DATETRUNC('quarter',[Date]), [Location]: SUM([Cost])}
This calculates the SUM([Cost]) for each quarter of [Date] and each [Location]

Tableau - Comparing one against an average of many on same worksheet

I'm looking to compare a single Fortune 500 company's revenues (selected by the end user from a list of all Fortune 500 companies) to an average revenue for all Fortune 500 companies over time. I would like the averages to show up as bars and the single company revenues to show as a line graph (both on the same chart).
I understand how to set two different chart types on the same view. What I don't understand is how to write the calculations and parameters to give the end user the functionality of choosing a single company and comparing it against all others.
Any suggestions on how to do this? Happy to clarify further if needed.
Thank you in advance!
I don't have Tableau right now to test, but I believe that kind of chart can be achieved using 2 measures.
Well, you need to create the measures then. The average of all is pretty straight forward, it's AVG(Revenue).
The single company revenue can be retrieved using parameters. Create a parameter like [Company chosen], and put the list of all Fortune 500 (you can load from field, which helps a lot).
Then create a calculated field [Company revenue]:
IF [Company] = [Company chosen]
THEN [Revenue]
END
And just use the sum or avg of this measure.
Put both the measures on the worksheet and set the right chart
I am using the superstore data that ships with Tableau.
You can create an LOD calculation to calculate the average:
SUM({FIXED [Order Date (Months)] : SUM([Sales])}) / SUM( {COUNTD([Sub-Category])})
This is calculating the total sales at each month and summing the sales and then dividing that by the number of points for each month (thereby giving you an average of sales per month).
FYI, I submitted this question to Tableau. They replied with a workbook using the Superstore data with the following instructions:
Add Year(Order Date) to the column.
Add Sales to the row.
Create a parameter called Select Country with the following settings:
Name: Select Country
Data Type: String
Allowable Values: List
Add from field: Country/Region
Select OK.
After creating the parameter, Right Click on the parameter in the data pane and select Show Parameter Control.
Create a calculated field Country Sale called with the following formula:
IF [Country / Region] = [Select Country] THEN [Sales] END
Add Country Sale to the Rows Shelf.
Go to Sum(Country Sale) tab on the Marks Pane and select Line from the drop down.
Right Click on the Country Sales axis and select Dual Axis.
Right Click on the Country Sales axis and select Synchronize Axis.