Crosstab Query Month YTD - sql

I am looking for a solution to convert the individual monthly averages to monthly averages from the beginning of the year. In other words from January to said month.
I used the cross tab wizard to group the rating of an employee into months in the column header. Employees are in the row header. The values of the ratings is then averaged.
My issue is this just shows the average rating of an employee for each month. I need a solution that would show me the average of each month if it included all results from the begging of the year (i.e. February would include January's and February's ratings).
TRANSFORM Avg(CSS_Table.[Emp_Rating]) AS AvgOfEmp_Rating
SELECT CSS_Table.[Emp]
FROM Rating_Table
GROUP BY Rating_Table.[Emp]
PIVOT Format([Survey_Date],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

One possible strategy to attack this
Create one query for each month that calculates the monthly YTD Average.
Create a Union query that joins them all together (i.e. make sure to use the same column names for each query).
Feed the union query into the Pivot.

Related

How to add one more measure in MDX where clause?

Below query is perfectly working and bringing rolling 12 months data for selected measure. Now how do I add one more measure to same MDX query so that query fetch rolling 12 months for 2 measures?
Thanks in advance for your help.
Working query with single measure
select
non empty({lastperiods(12,[Time].[By Fiscal Year].[Period].&[Jul-21])}) on columns,
[Customer].[CustomerName].[CustomerName].MEMBERS on rows
from(
select
([CustomerNamedSet]) on columns
from [CSIS]
where ({[Time].[By Fiscal Year].[Period].&[Jul-21]},
{[Measures].[measure1]})
)
enter image description here
Modified MDX query by adding one more measure in where clause (Not working)
select
non empty({lastperiods(12,[Time].[By Fiscal Year].[Period].&[Jul-21])}) on columns,
[Customer].[CustomerName].[CustomerName].MEMBERS on rows
from(
select
([customerNamedSet]) on columns
from [CSIS]
where ({[Time].[By Fiscal Year].[Period].&[Jul-21]},
{[Measures].[measure1],[Measures].[measure2]})
)
Expected results::
enter image description here

Year wise Average days SQL

Today i have below problem while perform an sql query. Please find below data.
I perform SQL query on my table and get the below resulted output. i perform Group by on ID, Name, Week, Year, Days now i want the Days column as average of All Days based on year column. means there is multiple value of year is exist so i need Avg of Days data in all rows of DAYS for particular row. expected result as per below.
Thanks in Advance!!!
Write in comment if you have any query.
You can use OVER:
SELECT
*,
AVG(Days) OVER (PARTITION BY LEFT(Year, 4)) AvgDays
FROM
Tbl
Note: Just grouped by year (2016)

Aggregating 15-minute data into weekly values

I'm currently working on a project in which I want to aggregate data (resolution = 15 minutes) to weekly values.
I have 4 weeks and the view should include a value for each week AND every station.
My dataset includes more than 50 station.
What I have is this:
select name, avg(parameter1), avg(parameter2)
from data
where week in ('29','30','31','32')
group by name
order by name
But it only displays the avg value of all weeks. What I need is avg values for each week and each station.
Thanks for your help!
The problem is that when you do a 'GROUP BY' on just name you then flatten the weeks and you can only perform aggregate functions on them.
Your best option is to do a GROUP BY on both name and week so something like:
select name, week, avg(parameter1), avg(parameter2)
from data
where week in ('29','30','31','32')
group by name, week
order by name
PS - It' not entirely clear whether you're suggesting that you need one set of results for stations and one for weeks, or whether you need a set of results for every week at every station (which this answer provides the solution for). If you require the former then separate queries are the way to go.

SQL Statement for MS Access Query to Calculate Quarterly Growth Rate

I have a table named "Historical_Stock_Prices" in a MS Access database. This table has the columns: Ticker, Date1, Open1, High, Low, Close1, Volume, Adj_Close. The rows consist of the data for each ticker for every business day.
I need to run a query from inside my VB.net program that will return a table in my program that displays the growth rates for each quarter of every year for each ticker symbol listed. So for this example I would need to find the growth rate for GOOG in the 4th quarter of 2012.
To calculate this manually I would need to take the Close Price on the last BUSINESS day of the 4th quarter (12/31/2012) divided by the Open Price of the first BUSINESS day of the 4th quarter (10/1/2012). Then I need to subtract by 1 and multiply by 100 in order to get a percentage.
The actual calculation would look like this: ((707.38/759.05)-1)*100 = -6.807%
The first and last days of each quarter may vary due to weekend days.
I cannot come up with the correct syntax for the SQL statement to create a table of Growth Rates from a table of raw Historical Prices. Can anyone help me with the SQL statment?
Here's how I would approach the problem:
I'd start by creating a saved query Access named [Stock_Price_with_qtr] that calculates the year and quarter for each row:
SELECT
Historical_Stock_Prices.*,
Year([Date1]) AS Yr,
Switch(Month([Date1])<4,1,Month([Date1])<7,2,Month([Date1])<10,3,True,4) AS Qtr
FROM Historical_Stock_Prices
Then I'd create another saved query in Access named [Qtr_Dates] that finds the first and last business days for each ticker and quarter:
SELECT
Stock_Price_with_qtr.Ticker,
Stock_Price_with_qtr.Yr,
Stock_Price_with_qtr.Qtr,
Min(Stock_Price_with_qtr.Date1) AS Qtr_Start,
Max(Stock_Price_with_qtr.Date1) AS Qtr_End
FROM Stock_Price_with_qtr
GROUP BY
Stock_Price_with_qtr.Ticker,
Stock_Price_with_qtr.Yr,
Stock_Price_with_qtr.Qtr
That would allow me to use the following query in VB.NET (or C#, or Access itself) to calculate the quarterly growth rates:
SELECT
Qtr_Dates.Ticker,
Qtr_Dates.Yr,
Qtr_Dates.Qtr,
(([Close_Prices]![Close1]/[Open_Prices]![Open1])-1)*100 AS Qtr_Growth
FROM
(
Historical_Stock_Prices AS Open_Prices
INNER JOIN Qtr_Dates
ON (Open_Prices.Ticker = Qtr_Dates.Ticker)
AND (Open_Prices.Date1 = Qtr_Dates.Qtr_Start)
)
INNER JOIN
Historical_Stock_Prices AS Close_Prices
ON (Qtr_Dates.Ticker = Close_Prices.Ticker)
AND (Qtr_Dates.Qtr_End = Close_Prices.Date1)

MDX- Divide Each row by a value based on parent

I am in a situation where I need to calculate Percentage for every fiscal year depending on distinct count of the rows.
I have achieved the distinct count (fairly simple task) for each year city-wise and reached till these 2 listings in cube.
The first listing is state wide distinct count for given year.
Second listing is city wise distinct count for given year with percentage based on state-wide count for that year for that city.
My problem is that I need to prepare a calculated member for the percentage column for each given year.
For eg, In year 2009, City 1 has distinct count of 2697 and percentage raise of 32.94%. (Formula used= 2697/8187 ).
I tried with ([Measures].[Distinct Count])/(SUM(ROOT(),[Measures].[Distinct Count])) but no luck.
Any help is highly appreciated.
Thanks in advance.
PS: City wide sum of year 2009 can never be equal to statewide distinct count of that year. This is because we are calculating the distinct count for city and state both.
You need to create a Region Hierarchy for this, like State -> City. The create a calculation like below. Then in the browser put your Hierarchy on the left and the sales and calculated percentage in values.
([Dim].[Region].CurrentMember, [Measures].[Salesamt]) /
iif(
([Dim].[Region].CurrentMember.Parent, [Measures].[Salesamt]) = 0,
([Dim].[Region].CurrentMember, [Measures].[Salesamt]),
([Dim].[Region].CurrentMember.Parent, [Measures].[Salesamt])
)