Created date dimension unable to slicing through cube - sql-server

My RDBMS is SQL Sserver, and I'm working on SSAS.
Fact: I have a fact called fact
date | payment
2014-01-04 00:00:00 |198000.00
2015-01-02 00:00:00 |381400.00
2017-01-01 00:00:00 |70500.00
2019-01-03 00:00:00 |891000.00
2015-02-02 00:00:00 |22000.00
...
and then I created time dimension on SSAS by creating time dim
- Generating table in data source
- I choose 5 time periods = year, halfyear, quarter, month, date
- I choose regular calendar
It creates this much column
[PK_Date]
,[Date_Name]
,[Year]
,[Year_Name]
,[Half_Year]
,[Half_Year_Name]
,[Quarter]
,[Quarter_Name]
,[Month]
,[Month_Name]
,[Day_Of_Year]
,[Day_Of_Year_Name]
,[Day_Of_Half_Year]
,[Day_Of_Half_Year_Name]
,[Day_Of_Quarter]
,[Day_Of_Quarter_Name]
,[Day_Of_Month]
,[Day_Of_Month_Name]
,[Month_Of_Year]
,[Month_Of_Year_Name]
,[Month_Of_Half_Year]
,[Month_Of_Half_Year_Name]
,[Month_Of_Quarter]
,[Month_Of_Quarter_Name]
,[Quarter_Of_Year]
,[Quarter_Of_Year_Name]
,[Quarter_Of_Half_Year]
,[Quarter_Of_Half_Year_Name]
,[Half_Year_Of_Year]
,[Half_Year_Of_Year_Name]
,[Fiscal_Year]
,[Fiscal_Year_Name]
,[Fiscal_Half_Year]
,[Fiscal_Half_Year_Name]
,[Fiscal_Quarter]
,[Fiscal_Quarter_Name]
,[Fiscal_Month]
,[Fiscal_Month_Name]
,[Fiscal_Day]
,[Fiscal_Day_Name]
,[Fiscal_Day_Of_Year]
,[Fiscal_Day_Of_Year_Name]
,[Fiscal_Day_Of_Half_Year]
,[Fiscal_Day_Of_Half_Year_Name]
,[Fiscal_Day_Of_Quarter]
,[Fiscal_Day_Of_Quarter_Name]
,[Fiscal_Day_Of_Month]
,[Fiscal_Day_Of_Month_Name]
,[Fiscal_Month_Of_Year]
,[Fiscal_Month_Of_Year_Name]
,[Fiscal_Month_Of_Half_Year]
,[Fiscal_Month_Of_Half_Year_Name]
,[Fiscal_Month_Of_Quarter]
,[Fiscal_Month_Of_Quarter_Name]
,[Fiscal_Quarter_Of_Year]
,[Fiscal_Quarter_Of_Year_Name]
,[Fiscal_Quarter_Of_Half_Year]
,[Fiscal_Quarter_Of_Half_Year_Name]
,[Fiscal_Half_Year_Of_Year]
,[Fiscal_Half_Year_Of_Year_Name]
After the dimension created I link it to the fact.
They are successfully built. After I browse the cube this comes out.
month |payment
2014-01-04 00:00:00 |40982534943.62
2015-01-02 00:00:00 |40982534943.62
2017-01-01 00:00:00 |40982534943.62
2019-01-03 00:00:00 |40982534943.62
2015-02-02 00:00:00 |40982534943.62
....
Which possibly not the result I need
Why is this happening? I have change the datatype of time dimension to be the same as the date type in fact.

i solve it by change the pk column name from time dimension same as fact date column. After i reload the project and remake it the relationship automatically made. when the fk column name was different as the fact column i make it manually.
solution :
make the column name the same. whether you change the dimension column into fact or change fact column into dimension.

Related

How can I get data for a window of 24 hours from given timestamp

Let's say I have a table with date-timestamp column in a table and every time I pass a date and time to it, I want to get the data for last 24 hours from that timestamp.
say, on querying for TIMESTAMP 23/03/2019 18:00:00
it should filter out and give results for the following period:
22/03/2019 18:00:01 to 23/03/2019 18:00:00
You may use an Interval expression to go back 1 day.
where timestamp_column > :v_timestamp - INTERVAL '1' DAY
AND timestamp_column <= :v_timestamp --The date you want to pass.

T-SQL Pivoting approach

I have a View that has a structure similar to the following:
Id Name State ZipCode #Requests AmtReq Price Month Year
1 John IN 46202 203 33 $300 1 2015
1 Jane IN 46202 200 45 $100 2 2015
...
Queries require reports to be generated for given quarters (1st quarter will include the first three months ...) grouped by state
The result should look like this:
Ist Quarter ...
January February ...
State ZipCode #Requests AmtReq Price #Requests AmtReq Price ...
IN 46202 203 33 45 200 45 100
I feel that this can be done using pivoting but I do not have experience with it. I tried with single column pivoting and had some success, but not in this scale.
Another approach would be to create a stored procedure that will generate the data for me and then just fix some formating (e.g., the first two rows) in the client. Any suggestions on how to approach this problem?
I am using SQL Server as a DBMS.
If you have MS Excel on your machine then you can export the view to Excel and summarize it to a pivot table. From there you can create table and diagrams as you needed.

SQL Query for SSRS report

I would like to create an ssrs report as below --
I have the following columns to be displayed-
| Tickets | Tickets |
| scanned on| scanned on|
Attraction | Hour | 09/08/2014| 09/09/2014| 09/10/2014| Day 4| Day 5| Day 6| 09/14/2014
Monday Tuesday Wednesday ...................... Sunday
U Mueseum | 9:00 AM | 10 | 40 |
10:00 aM |
..
..
..
..
..
23:00 AM
I will get the Start Date and End Date from the user. Now my problem is that I want a query for 7 days starting from the Start Date selected by the user to the End Date and for each hour i.e. for 1 day it would be 24 hrs, so 24*7 hours in total. When I display the value for scans in my current sql query it displays just for one day. How can I do it for 7 days, and the value of scans for that date should be displayed in the respective week day ie. Monday or Tuesday and so on. I am not able to get as to for each date and each hour the scan value changes, so I am confused and mixing up all here. The values for each hour on each day should be different and there is only one scan column, so how will the distinct values show up in the table.
I used the pivot table to convert the name of the week day from rows to individual columns.
Then the problem arises for ssrs report. How can this be executed in an ssrs report where the rows are for each hour and the columns displays the dates of the week selected. How can I achieve that in ssrs? I am getting currently for only 24 hrs, but i want the report to run for all 24 hrs for 7 days and should display the value side by side for each hour in each week day column.
Thank you.
Pass in one parameter into your stored procedure called #StartDate.
In SQL create the #EndDate like this
DECLARE #EndDate DATETIME
SET #EndDate = DATEADD("d",7,#StartDate)
Return your date information in an hour and a day column. Then use the grouping feature in ssrs to display the data.
I hope this helps you get a bit further with your issue.
Bobby
You could do this pretty easily with a Date Dimension table and a PIVOT operator. Give that a try.

DateTimeOffset Sample in SQL 2008R2

I have a doubt related to using datetimeoffset in SQL server 2008 r2.
Suppose I have a web application, the db server in Spain and the client is in Canada. I don't know the time zone between Spain and Canada but suppose its 5 hours more in Spain.
So user in Canada wants to add a new Employee at 23:00 PM, when clicks save, it calls a stored procedure that inside call the function SYSDATETIMEOFFSET() to fill the column CreatedDate that has a datatype datetimeoffset(7).
In this case, what will be the datetime saved in the database? and How should I do to show the right datetime to the user in Canada that wants to check the CreatedDate? Is there any good example to check this?
According to MSDN:
The data is stored in the database and processed, compared, sorted, and indexed in the server as in UTC. The time zone offset will be preserved in the database for retrieval.
In your example, the data will be stored in a binary format that can be translated to 14 Nov 2013 23:00 -5:00, which means the date and time that is local plus the time offset of -5 hours to UTC (let's suppose this is the offset for Canada).
When you store this type of value, you have to provide the offset yourself, the system doesn't do it automatically.
Because the data is stored as UTC time, it makes comparison, sorting, etc. of data easy, while you can always retrieve the original time offset.
You should generally store client's local time with offset when storing information about events that need to be compared across time zones.
More on datetimeoffset on MSDN.
Example
Create a table and insert data
create table dto (dto datetimeoffset(7))
insert into dto values (GETDATE()) -- inserts date and time with 0 offset
insert into dto values (SYSDATETIMEOFFSET()) -- current date time and offset
insert into dto values ('20131114 08:54:00 +10:00') -- manual way
When I select the data, I get
2013-11-14 07:56:17.2300000 +00:00 -- current time, no offset so useless in this case
2013-11-14 07:56:17.2338125 +11:00 -- current time with my local offset (in Australia)
2013-11-14 08:54:00.0000000 +10:00 -- manually inserted data
You can simply store the UTC date in your DB
Select GetUTCDate()
For Example, DB Server is in Spain and a client in CANADA
adds a new Employee Joining date in System. (UTC Date) 14 Nov 2013 23:00
so if you want to show the right DateTime to the user in Canada you need to add/subtract your current time zone offset like if in Canada it's UTC-4 so
Select Cast('14 Nov 2013 23:00' as Datetime) as [UTC TIME]
Select DateAdd(hour,+2,Cast('14 Nov 2013 23:00' as Datetime)) as [Time In Spain]
Select DateAdd(hour,-4,Cast('14 Nov 2013 23:00' as Datetime)) as [Time In Canada]
Result
14 Nov 2013 23:00
15 Nov 2013 01:00
14 Nov 2013 19:00
you can make some function to do this...

Adding rows for recordsets that have incomplete date ranges

This one is tricky for me, can't figure it out.
We have a system where we calculate inventory numbers on the fly. If there's a month where a customer doesn't have any orders, there's no record for that month but the beginning inventory is still calculated and displayed as a rolling calculation based on the previous transaction data.
I'm now pulling this data but need to "fill in the blanks" so to speak.
For example, the table has the following fields:
MonthYear DATETIME
WarehouseID INT
Quantity DECIMAL(18,2)
If I put all this in a temporary table to do calculations, I'll end up with something like this:
2010-01-01 00:00:00.000 135 1000.00
2010-04-01 00:00:00.000 135 2000.00
2010-07-01 00:00:00.000 135 3000.00
2010-06-01 00:00:00.000 235 1000.00
2010-07-01 00:00:00.000 235 2000.00
2011-02-01 00:00:00.000 135 1000.00
2011-03-01 00:00:00.000 135 2450.00
etc., etc.
What I need to do is for each warehouse, if a record exists for that year, add a blank row for any months that aren't in the table.
In the example above for warehouse 135 I need to add a record for 02, 03, 05, 08, etc.
Is there any easier way of doing this rather than with cursors and loops?
Thanks.
Search Recursive CTE. Then LEFT JOIN. Gotta bolt out the door. Sorry for quick answer.

Resources