I Have One Time_Table Which include following columns.
id int (identity),
date datetime,
month Int,
Quarter int ,
Year int
in Analysis services - How Can we Bind Month Columnn To Month Name In Cube .
Actually , I Want Month Name Should Be Dispaly instead of Month Number (I Have Month Number In My Time Table and There Is Not Jan , Feb But I Want To Dispaly Jan, Feb.. Instead Of 1,2 ...)
You need to add a column with the Month Name in your table Time_Table and use it as the NameColumn for your Month attribute.
Same thing for quarter and year, you need columns with the labels that you want to see.
Related
I have a table (holidays) containing all the holidays in a year:
I need to make a table (t1) where 1 column contains all the 365 days within a year, and another column 'description' containing a name of a holiday for all dates that are within a +/- 7 day range of a holiday, and 'None' for all dates that are outside of this range.
I was able to use generate_series to create the first-column (my_date: all 365 days within a year) and used a left-join to try to create the second column (description)
WITH all_dates AS
SELECT my_date::date
FROM generate_series('2020-01-01', '2020-12-31', '1 day'::interval) my_date)
SELECT all_dates.my_date,
holidays.description
FROM all_dates
LEFT JOIN holidays
ON all_dates.my_date = holidays.holiday_date
To create the table below.
However, I need it to be such that 2020-01-02, 2020-01-02, ..., 2020-01-08 would have the description 'New Year's Day' as these dates are within a 7-day range of New Year's Day, and so forth for other days within a 7-day range of other holidays (e.g., rows for dates between 2020-12-18 to 2020-12-31 would have the description 'Christmas')
I'm also unsure about how to handle days that are within a 7-day range of more than one holiday (Father's Day and Independence Day have overlapping dates that are within a 7-day range). I need it to be such that there is only one row per day in 2020.
Any help would be appreciated!
Try something like below. You already have the ranges around the holidays, so use that in the join. Then, use distinct on to restrict results to one per calendar date, and adjust the order by to keep the holiday you want.
with all_dates as (
select my_date::date
from generate_series('2020-01-01',
'2020-12-31', '1 day'::interval) my_date
)
select distinct on (all_dates.my_date)
all_dates.my_date,
holidays.description
from all_dates
left join holidays
on all_dates.my_date
between holidays.holiday_date_min7
and holidays.holiday_date_plus7
order by all_dates.my_date,
abs(all_dates.my_date - holidays.holiday_date), -- keep closest
all_dates.my_date - holidays.holiday_date -- prefer upcoming holiday
I'm trying to get Weekly MTD and YTD values based of hourly data, but I'm having difficulties achieving this.
This is the data I'm working with:
max(Date) - Last day of the week
ISOWeek - Week in question
Value - The data I'm trying to sum
SELECT MAX(ISOWeek) AS [ISOWeek]
,MAX(Date) AS [Date]
,SUM(Value1) AS [MTD]
FROM Table1
GROUP BY ISOWeek, FORMAT(Date,'yyMM')
ORDER BY ISOWeek DESC
This is what that query returns:
ISOWeek Date MTD
29 2020-07-19 367529
28 2020-07-12 367138
27 2020-06-30 103290
27 2020-07-05 266755
26 2020-06-28 346588
25 2020-06-21 337168
This is what I would like to get:
ISOWeek Date MTD
29 2020-07-19 261515
28 2020-07-12 184104
27 2020-07-05 103414
26 2020-06-28 432114
25 2020-06-21 346588
The data has to be grouped by ISOWeek, if it's a week that dips into two months, I'm only interested in the MTD of the month in which the week ends. We have hundreds of values, so the plan is to create a MTD view and a YTD view. If I can get some help with the MTD one, I can get the other one done.
I'm nearly sure that what I'm after has to do with a WHERE clause and DATEADD but I'm not too sure what it should say.
Thank you for taking the time.
I don't really follow the rules you would like to apply, but per dates apply the formula to get weekstart/monthend or what you need. Place the date instead of the current date in the example.
Then group by the modified date.
You could build a date dimension where you have the required dates in some columns (first day of month, first day of week,etc.). This way you get a table with all the dates and the matching result for each.
It might be easier/faster to join it on the requried column.
declare #monthstart date,
#monthend date,
#weekstart date
;
select #monthstart=datefromparts(year(current_timestamp),month(current_timestamp),1);
select #monthend=EOMONTH(getdate(),0);
select #monthstart,#monthend,EOMONTH(getdate(),1) as next_month, EOMONTH(getdate(),-1) as previous_month;
select cast(DATEADD(d,1-DATEPART(WEEKDAY,current_timestamp),CURRENT_TIMESTAMP) as date) as Sunday,
cast(DATEADD(d,2-case when DATEPART(WEEKDAY,current_timestamp)=1 then 8 else DATEPART(WEEKDAY,current_timestamp) end,CURRENT_TIMESTAMP) as date) as Monday
;
I want to use Redshift to count the number of Mondays in a given time range. I've tried using date_part, which returns the day of the week. I can't use a simple count as there are multiple instances on the same day.
if you have dates table reference you can use the following code
select count(distinct my_table.date)
from my_table
where
date_part(dow,my_table.date)=1
and my_table.date between '2015-01-01' and '2016-01-01'
in this case the query will count all Mondays during 2015,
you can change the dates range the the day week .
date_part(dow,my_table.date)=1 -- Monday
date_part(dow,my_table.date)=2 -- Tuesday
and so on
if you don't have dates table , you should create Cartesian product
I'm new to Spotfire and I need to populate all cells in a column with a value taken from the last day of the month. Example as below.
Original data:
Required output is that there will be another column that takes 32212 for the month of November ie this column to take the volume that corresponds to the last date 11/20/2009 and for the month of December it will take 12231.
Here is what I did:
Created a calculated column to find the last entry of the month
Max([Date]) OVER UID
and UID is an unique id for each month integer(Year([Date])*100 + Month([date]))
Another calculated column to find the value for the last entry in each month
if(([Date]) = ([Max Date per Month]),[Volume],0)
Now this is the problem. I tried to use
Sum(if(([Date])=([Max Date per Month]),[Volume],0)) OVER ([Max Date per Month])
but Sum doesn't work.
Any idea?
What do you mean by "Sum doesn't work"?
Here is what I did:
Created two calculated columns as
Month([Date])
Year([Date])
Created Last day in month column which simply gives me latest date in each month
Last([Date]) OVER ([Year],[Month])
Created column which calculates what you need
Sum(if([Last Day in Month]=[Date],[Volume],0)) OVER ([Year],[Month])
I use tsql to sum weekly data (starting date is Monday).
Last week spanned across the boundary of 2012 and 2013, which means that I get two weekly sums.
Is there a way to group the data within tsql so I get one weekly sum for 2012-12-31 to 2013-01-06?
Example data and sql:
http://sqlfiddle.com/#!6/52b25/13/0
No link example below. Hypothetical table has one column, "created" of datetime type. How many rows for each week? I'd like to get two result rows, one for the last solid week of 2012 and one for the week that 2013 starts in (which starts on Monday 12/31/2012).
select
min(created) as 'First Date',
count(*) as 'Count',
datepart(wk,created) as 'Week Number'
from items
group by datepart(wk,created), datepart(yy,created)
order by min(created)
One workaround is calculate week number manually
select
min(created) as 'First Date',
count(*) as 'Count',
datediff(dd,'2012-01-02',created)/7 AS week_number
from items
group by datediff(dd,'2012-01-02',created)/7 -- You may replace '2012-01-02' to any date that is Monday (start day of the week)
order by min(created)