Create Date in Google Big Query using Newly Created Column - google-bigquery

I have a view that converts fiscal year periods to calendar periods, creating a new column called "NewPeriod". I would then like to create a date using this "NewPeriod" column using the Date() function, Date(Year, NewPeriod, "1"). I am unable to use the NewPeriod in the Date function, is there a way I can accomplish this in the same view?
SELECT distinct
company_code,
Period,
Year,
CASE COMPANY_CODE
WHEN 1 THEN CASE Period
WHEN 4 THEN 1
WHEN 5 THEN 2
WHEN 6 THEN 3
WHEN 7 THEN 4
WHEN 8 THEN 5
WHEN 9 THEN 6
WHEN 10 THEN 7
WHEN 11 THEN 8
WHEN 12 THEN 9
WHEN 1 THEN 10
WHEN 2 THEN 11
WHEN 3 THEN 12
ELSE
Period
END
Else Period
END AS NewPeriod,
FROM
`table`

Related

Remove a string from certain column values and then operate them Pandas

I have a dataframe with a column named months (as bellow), but it contains some vales passed as "x years". So I want to remove the word "years" and multiplicate them for 12 so all column is consistent.
index months
1 5
2 7
3 3 years
3 9
4 10 years
I tried with
if df['months'].str.contains("years")==True:
df['df'].str.rstrip('years').astype(float) * 12
But it's not working
You can create a multiplier series based on index with "years" and multiply those months by 12
multiplier = np.where(df['months'].str.contains('years'), 12,1)
df['months'] = df['months'].str.replace('years','').astype(int)*multiplier
You get
index months
0 1 5
1 2 7
2 3 36
3 3 9
4 4 120
Slice and then use replace()
indexs = df['months'].str.contains("years")
df.loc[indexs , 'months'] = df['a'].str.replace("years" , "").astype(float) * 12

BigQuery conditional running sum

Using SQL (Standard BigQuery), I need to calculate a running average of the most recent 4 weeks of sales in which flag was FALSE. The average is actually a baseline, so it does not include the current week's sales.
week flag sales
1 FALSE 3
2 FALSE 1
3 FALSE 3
4 FALSE 0
5 FALSE 3
6 FALSE 6
7 TRUE 3
8 TRUE 1
9 FALSE 3
10 FALSE 9
11 FALSE 6
12 FALSE 4
13 TRUE 4
14 TRUE 2
15 FALSE 1
For example, week 6 has (week2+week3+week+week5)/4=(1+3+0+3)/4=7/4=1.75.
For, say, week 10, the running average should not include week 7 and week 8 since flag is true. Week 10 should be (week4+week+5+week6+week9)/4=3
The whole table should like
week avg
1 NULL
2 NULL
3 NULL
4 NULL
5 1.75
6 1.75
7 3
8 3
9 3
10 3
11 5.25
12 6
13 5.5
14 5.5
15 5.5
I've been trying to augment the answer here
SQL Select Statement For Calculating A Running Average Column
Thanks,
Jim
Below is for BigQuery Standard SQL
#standardSQL
SELECT week,
(SELECT IF(COUNT(1) = 4, AVG(sales), NULL)
FROM (
SELECT sales FROM UNNEST(arr) WHERE NOT flag ORDER BY week DESC LIMIT 4
)
)
FROM (
SELECT week, ARRAY_AGG(STRUCT(week, flag, sales)) OVER(win) arr
FROM `project.dataset.table`
WINDOW win AS (ORDER BY week ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
)
-- ORDER BY week

Tableau: How to get moving average with respect to day of week in last 4 weeks?

e.g: If I have the data as below:
Week 1 Week2 Week3
S M T W T F S S M T W T F S S M T W T F S
2 5 6 7 5 5 3 4 5 7 2 4 3 2 4 5 2 1 2 7 8
If today is Monday, my average will be (5+5+5)/3 which is 5. Tomorrow it will be (6+7+2)/3 which will be 5 again and day after it will be (7+2+1)/3 which will be 3.33
How to get this in Tableau?
First, you can use "Weekday" as a column or row (by rightclicking on the date).
Then you can simply add a Table Calculation "Moving Average" with a specific computing dimension "Week of [Date]"
=> Table Calculation Specifics <=
=> Result <=
Data source used-: Tableau Sample Superstore.
You can do the following-:
Columns-: Week(Order Date)
Rows-: Weekday(Order date)
Put Sales in text.
Right click sales>Quick Table Calculation>Moving Average
right click Sales>edit quick table calculation>
Set the following
Select Moving along-: "Table across"
Previous values-: 4

How to build SQL query for this following scenario?

I have a table that looks like this:
Name Post Like Share Comment Date
--------------------------------------------
Sita test data 1 5 2 4 28/4/2015
Munni test data 2 5 2 5 27/4/2015
Shila test data 3 1 3 1 22/4/2015
Ram Test data 4 5 0 5 1/4/2015
Sam Test data 5 4 0 2 2/4/2015
Jadu Test data 6 1 5 2 30/3/2015
Madhu Test data 7 5 0 4 10/4/2015
Now I want my result set like this:
Type Name Post Like Share Comment Date
-------------------------------------------------------------------------
Today Sita test data 1 5 2 4 28/4/2015
Last 7 Days Sita test data 1 5 2 4 28/4/2015
Last 7 Days Munni test data 2 5 2 5 27/4/2015
Last 7 Days Shila test data 3 1 3 1 22/4/2015
Last 30 Days Sita test data 1 5 2 4 28/4/2015
Last 30 Days Munni test data 2 5 2 5 27/4/2015
Last 30 Days Shila test data 3 1 3 1 22/4/2015
Last 30 Days Ram Test data 4 5 0 5 1/4/2015
Last 30 Days Sam Test data 5 4 0 2 2/4/2015
Last 30 Days Jadu Test data 6 1 5 2 30/3/2015
Last 30 Days Madhu Test data 7 5 0 4 10/4/2015
Today must have only today's post. Last 7 days must have today's post + last 7 day's post. Last 30 days column must have all the post of last 30 days.
A couple of unions with different case statements to get the date range would work.
Use union all and dateadd:
select 'Today' as Type, Name, Post, [Like], Share, Comment, [Date]
from yourtable
where [Date] = getdate()
union all
select 'Last 7 Days' as Type, Name, Post, [Like], Share, Comment, [Date]
from yourtable
where [Date] >= DateAdd(day,-7,getdate())
union all
select 'Last 30 Days' as Type, Name, Post, [Like], Share, Comment, [Date]
from yourtable
where [Date] >= DateAdd(day,-30,getdate())
BTW, terrible choice for column names (don't use reserved words).

how to get a moving average in sql

If I have data from week 1 to week 52 data and I want 4 week Moving Average with 1 week how can I make a SQL query for this? For example, for week 5 I want week1-week4 average, week6 I want week5-week8 average and so on.
I have the columns week and target_value in table A.
Sample data is like this:
Week target_value
1 20
2 10
3 10
4 20
5 60
6 20
So the output I want will start from week 5 as only week 1-week4 is available not before that.
Output data will look like:
Week Output
5 15 (20+10+10+20)/4=15 Moving Average week1-week4
6 25 (10+10+20+60)/4=25 Moving Average week2-week5
The data is in hive but I can move it to oracle if it is simpler to do this there.
SELECT
Week,
(SELECT ISNULL(AVG(B.target_value), A.target_value)
FROM tblA B
WHERE (B.Week < A.Week)
AND B.Week >= (A.Week - 4)
) AS Moving_Average
FROM tblA A
The ISNULL keeps you from getting a null for your first week since there is no week 0. If you want it to be null, then just leave the ISNULL function out.
If you want it to start at week 5 only, then add the following line to the end of the SQL that I wrote:
WHERE A.Week > 4
Results:
Week Moving_Average
1 20
2 20
3 15
4 13
5 15
6 25