Can I create a time dimension from a datetime column in fact table in my database? - olap

I am trying to create my first OLAP Cube.
My fact table has a date_time column in the format "yyyy-MM-dd hh:mm:ss" and I would like to use this column to create a dimension so the user can segment the results in months.
The tutorials I've found always have a time dimension table, which is not my case (I only have the date_time column directly in my fact table).
Is it possible to use the column to segment the results in months?
(I am using mondrian schema workbench to create the schema)

Related

Teradata Partitioning Usage in Views

I have a table which is partitioned on a date column.
TRANS_DT between '2010-01-0' and '2030-12-31' EACH INTERNAL '1' MONTH.
A view is created on the table where the TRANS_DT is CAST as CAST(CAST(TRANS_DT AS DATE FORMAT 'MMM-YYYY') AS VARCHAR(8)) to denote date like 'MAR-2021'.
When SQL's are running on the VIEW by applying condition as TRANS_DT='MAR-2021', the explain plan states that a full table scan is performed instead of Partition Level Scan.

Formatted local time in a virtual generated column of SQLite

I want to store date in milliseconds, but also I want to see the formatted representation of it.
In order to not waste the drive space it makes sense to use a virtual generated column for this.
I wrote it:
DROP TABLE IF EXISTS example;
CREATE TABLE IF NOT EXISTS example (
time INTEGER,
formatted_time GENERATED ALWAYS AS (strftime('%Y.%m.%d %H:%m', time/1000, 'unixepoch')) VIRTUAL
);
INSERT INTO example (time) VALUES (1605960000000);
INSERT INTO example (time) VALUES (1615413202000);
It works, but I can't set the second modifier of strftime(format,timestring,modifier,modifier...) to get the local time. (It returns UTC time by default.)
When I use:
formatted_time GENERATED ALWAYS AS (strftime('%Y.%m.%d %H:%m', time/1000, 'unixepoch', 'localtime')) VIRTUAL
It throws Result: non-deterministic use of strftime() in a generated column when I insert a data.
While it works as expected:
select strftime('%Y.%m.%d %H:%m', 1615413202000/1000, 'unixepoch', 'localtime');
How to create a virtual column with formatted local time?
It's not possible.
Based on this answers:
Creating generate column based on today's date in SQLite
Computed column 'Month' in table cannot be persisted because the column is non-deterministic
SQLite requires to the value of a generated column would be the same on any machine in any time zone (be deterministic). Even for a virtual generated column.
In my case for time value 1615413202000 the formatted_time would be different for the same table opened in different time zones, so the the table would be "non-deterministic".
As a workaround it possible to create a view:
CREATE VIEW example_view AS
SELECT time, strftime('%Y.%m.%d %H:%m', time/1000, 'unixepoch', 'localtime') as formatted_time_local
FROM example;
(based on Shawn's answer)

Format date from lookup table

My report has a data lookup table with dates in dd/mm/yyyy formart. When I bring it into Crystal it changes the data type to date-time instead of just date.
I tried converting to just a date within Crystal, but when I run a lookup based on parameters (10-1-2016 - 10-31-2016) I get a running cycle of dates. As soon as it hits 10-31-2016 it starts over from 10-1-2016.
I tried setting it to not provider duplicate values to no avail. How I could be doing this better?
If you just need to display the date without the time value, you can format the field when it displays in your report as "System Default Short Format" in the Format Editor. (Date and Time tab)
Otherwise leave the date as-is and create a seperate Formula Date({table.Field}) to use as "just the date". This way you keep the original datetime value as-is, but can use your new Formula when the time value needs to be removed.

SQLite: select all rows made in a specific month

i want to get all entries from a SQLite table, which have the timestamp from the same month.
For example, the user can type in "July" and then i want to get all entries made in the 7. month.
The current "time"-column is a simple string and in the Format (DD.MM.YYYY HH:MM:SS)
Is there a way to do this with SQLite or will i need to use code in my program?
Assuming that your time strings have a fixed length, you could use a query like this:
SELECT * FROM MyTable WHERE time LIKE '__.07%';
However, you should always stored dates in one of the supported date/time formats so that you are able to use the built-int date/time functions.

How do I pull values between two datetimes at specific interval in MySQL?

I have an application that writes temperature values to a MySQL table every second, It consists of the temperature and a datetime field.
I need to pull these values out of the table at specific intervals, every second, minute, hour etc.
So for example I will need to pull out values between 2 datetime fields and show the temperature at the hour for each of them.
One option I've considered is to create a temporary table that holds a list of the time intervals generated using MySQL INTERVAL and then joining that against the main table.
I'm just wondering if there's some time and date functions in MySQL that I'm overlooking that would make this easier?
Thanks.
You could use between for your date, and then a conditional WHERE clause using time() that looks at the structure of the timestamp. If it has 00:00 (for instance, 16:00:00) within it, take it, if not, leave it.
Example (untested):
SELECT temp, date
FROM temperatures
WHERE (date BETWEEN '2009/01/03 12:00:00' AND '2009/01/04 12:00:00')
AND (time(date) LIKE '%:00:00')
ORDER BY date ASC
LIMIT 10

Resources