Create day, date, time in DB - Question - database

Create_date -> Assume it will record as 5/2/2009. For search purposes when searching create date can we search by individual month or year from this or do we need to even record a create_day, create_month, crate_year for this? Once of the search filters for user content will be like example -> "Show content from last 2 weeks, last month, last year, current month only"
create_day -> While we record numeric date, many times for we may need to display the text day (ie: Thursday) to show when the object was created. Like we see on social websites "created on Thursday, Jan 29, 2009 at 3:45pm". To get this output, do we need to record a create_day for all objects / activities or is it calculated on each page load at application level?
create_time -> What time are we storing in DB? User local time or a fixed time? This is a global website. If fixed then let's say I do GMT by default. Then next question is how to display the correct time to a user so it matches his local time? Must factor into calculation that in US time changes twice a year (day light savings). OR, maybe record time always in GMT but display it to users based on their detected time zone,but then that means calculating time from timezone on each page load?
And since I am here, a side Q-> Any difference between lookup and reference table? How to distinguish these two tables -> "Account_status" which has values Active, Confirmed, etc... AND another table "City" which has city names. First table is a system ID table used in back end only. The city table is ID table used by users to select city from. Are these both lookup or reference or same/different?

Create_date You'll usually want to use the DMBS underlying date storage type which stores all pieces of a date (and time if need be, like MySQL datetime). This opens up the ability to use date processing functions within the DBMS. For the filter example, you would calculate what the date was at 2 weeks ago. You may also get have a function to specify such a string in the DBMS. PHP's strotime() allows this.
Create_day The text output is either automatic (MySQL datetime for instance) or it's easy to render whichever piece you need. (PHP date() can do this). It's generally a Good Idea to store a time stamp (datetime) for every record.
Create_time If it's a global app you'll want to use UTC/GMT. Every user will have their offset applied to the times and as well as their form submissions (such as searching). PHP's DateTime objects allow specifying a timezone name as the offset. The user can choose from a list but in it's standard form that's a big list and isn't much towards usability if it doesn't even match their city (http://us.php.net/manual/en/timezones.php). The other option is getting the user's computer's timezone via Javascript.
Lookup Table Yes those are both lookup tables.

Related

Is there a way to add the current timestamp to a Google Data Studio report?

I created a simple report to track the funds raised for our primary school. Just a pie chart that picks data from a Google Sheets being maintained by the treasurer.
There is now a request to add a timestamp on the screen (the report is being shared by taking a screenshot and sharing in various social media platforms to report progress).
After some Googling I couldn't find a function that returns current time. Something like =now() in Sheets. However, it was recommended to try create the time in the source data.
So I created a field that stores the value of the current time in Google Sheets using this function:
=(now()-date(1970,1,1))*86400
I also set the Sheets to refresh every minute. The Sheets works perfectly.
On the Studio side I added a calculated field to display the time in my preferred format:
TODATE(Amount,'SECONDS','%H:%M:%S on %d %b %Y')
Again, this conversion works perfectly.
The problem is that the time refresh doesn't work on the report side. No matter how many times I refresh the data it still doesn't pick the updated time from the source. Yet the source sheet has the updated time.
As far as I can tell, the worksheet time update has to be triggered manually for the report to be updated.
That negates the whole purpose of the timestamp.
Sharing the report directly from Google Studio is not a practical option for now. Still, I have shared the report.
0) Summary
Use either:
#1 New Recommended Approach: Using Scorecards
#2 Original Suggestion: Using Tables
1) New Recommended Approach: Scorecards
It can be achieved using the CURRENT_DATETIME function (released on the 17 Sep 2020 Google Data Studio update to Dates and Times).
The below looks at three use cases using Scorecards which are aggregated by MAX or MIN (in the below scenario either aggregation would display the same Date Time); the fields will automatically update based on the chosen Data Freshness settings (for example, the Google Sheet used in this Report is set to refresh every 15 minutes) and can also be manually updated if required (by clicking on the refresh icon at the top of the report or using the shortcut keys Ctrl + Shift + E):
1.1) UTC
The default function would display a value based on UTC:
CURRENT_DATETIME()
1.2) Time Zone
A Time Zone could also be specified; for example, the below would display the the EST Time Zone:
CURRENT_DATETIME("EST")
1.3) Location
A location can also be specified, based on the TZ database name, for example, Colombo, Sri Lanka would be:
CURRENT_DATETIME("Asia/Colombo")
Added an Editable Google Data Studio Report and a GIF to elaborate:
2) Original Suggestion: Tables
The below looks at three use cases (outlined above) created using Tables.
Added an Editable Google Data Studio Report and a GIF to demonstrate:
There isn't a function to do that yet but hopefully will come soon (see:https://issuetracker.google.com/issues/78200216 which is assigned) however with a little careful design, you could achieve it using date rather than timestamp utilising a date filter.
If you don't have a date field in your data than you could simply set this to TODAY.
If you do have a date field then use the advanced date to set a start date of your field's earliest date and max date of TODAY.
You could then use some shapes / formatting to cover up what isn't needed.
Hardly ideal but maybe a stop gap?
There is a variable TODAY() that doesn't seem to be documented in their Function documentation but that works in calculated fields and may help you

Query in Sql server

I have a table which contains data of ledger. it contains field Account name,debit credit.
the data entered in the table at end of month ie. sum of all the days.
Here my problem is that i want to display data of two months i.e current month and the back month. the real problem is that it will be displayed in crosstab
Accname dated debit credit
------- ----- ------ ------
Account name will be in both the months and i want to display one entry from both the month and its corresponding data ie credit and debit field will fill. The query will work on two conditions fields the account name and dated which is to be checked.
Is it possible to use case statement in the query
PLease Help
SQL Server/SQL does not display anything by default. So it doesn't really matter in what form you return the data from your query, as long as it is then visualized the way you want it. Just write your query to return the required data and then care about the visualization.
It might also help us if you told us how you're planning to visualize the data.
To answer your question: Yes, you can use CASE within a query as in
SELECT
CASE WHEN <Condition1> THEN <Value1>
WHEN <Condition2> THEN <Value2>
ELSE <Value3>
END AS <FieldName>

storing time and day of week

Challenge :
I have a requirement in which I have to implement recurring events. I am storing day of the week , time and the date range for which the event will reoccur.
Possible solution:
Storing time and day of week as string and enumeration.
Storing current and setting the time and day for that day ?
Question :
What is the best practice on storing time and day of week ?
EDT: I am using SQL Server Database
Another alternative is to have computed columns representing the parts that you're after.
CREATE TABLE dbo.foo
(
bar DATETIME,
bar_day_of_week AS DATEPART(WEEKDAY, bar),
bar_time AS CONVERT(TIME, bar)
);
INSERT dbo.foo(bar) SELECT GETDATE();
SELECT bar, bar_day_of_week, bar_time FROM dbo.foo;
This best approach might dependson the database you are using. But, there are two general approaches, both quite reasonable.
The first is to store dates and times in the native format for the database. Most databases have functions to extract day of the week and time from a date time type. You would write your queries using these functions. Typically, you would store these as one field, but sometimes you might want to separate the date and time portions.
The second is to have a calendar table. A calendar table would have a date or dateid as a key, and then contain columns for what you want to know about it. Day of the week would be an obvious column.
A calendar table is the preferred solution in some situations. For instance, if you are going to internationalize your application, then being able to get day of the week from a table makes it easier to get the string English, Greek, Chinese, Russian, Swahili, or whatever your favorite language is. Or, if you want to keep track of specific holidays, then a calendar table can store this information as well. Or, if you have a business running on a strange financial calendar (such as 5-4-4), then a calendar table is a good choice.
In either case, you do not need to store redundant date information in the table. You should derive it, either from a built-in function or by looking up what you want in a reference table.

How to arrange a daily time log entry form in Microsoft Access 2007 with Weekdays & Dates in the column headings

The objective is to create a weekly form, in Microsoft Access 2007, that allows employees to select their name from a list, the date of the first day of the week, and then create all daily time longs for the week in single form. The form needs to have a week view like the form (an Excel mockup) shown here:
Once entered, the data is to be written to the Project Time Log table shown here:
When the employee selects the "Week Starting" value, the column headings in the able below need to update. Is this possible? What also has me stumped is how to enter project hours for the week in a single row that will result in creating up to 6 records in my database. Finally, how does one set up validation on the "Week Starting" field so that the employee can only select Mondays?
I guess this is where I admit that I am just getting started with MS Access. However, with some experience in database design and Excel I am finding everything but advanced form building to be fairly straightforward.
So, can someone point me in the right direction? Do I need to use a Pivot Table to make this work? What is a Modal Dialogue? Could it be useful here? Any suggestions would be greatly appreciated.
The easiest way may be to create a table used solely for dataentry that can reside in the front-end for each employee.
DETable
EmployeeID
WeekStarting
ProjectID
Workcode
Mon
Tue
<...>
Sat
You can clear down the table and then append the relevant projectIDs and EmployeeID with a command button or suitable event.
The labels showing Mon, Tue etc can be updated to show the relevant date after WeekStarting is selected.
A suitable set of queries, or a UNION query will allow you to append the data to the main table.

about date in database question

i need to find data between 2 date's and time's.
i use one field for date , and one field for time.
is it be better to use only one field for date & time ?
i see that it came in dd/mm/yyyy hh:mm:ss format that
can contain date and time.
this question is for acceess and for sql-server
thank's in advance
In nearly all circumstances, date and time are needed together. Both Access and SQL server have a date/time data type. In Access, even if you specify the format as time, you can show a date. This is because all datetime data is stored as a number, and time is the decimal portion:
Say I store data: 10:31:46, I can type lines in the immediate window that illustrate the storage of datetime, like so:
?CDec(DlookUp("TimeFormattedField", "Test"))
0.438726851851852
?Year(DlookUp("TimeFormattedField", "Test"))
1899
?Format(dlookup("F4", "Table2"),"dd/mm/yyyy")
30/12/1899
This is because zero (0) is a valid date.
It is very easy to get the different portions of a datetime field, so store datetime as a single fields, because that is what you are going to get, anyway.
I like to store date and time separately. In general, I almost never need time in my apps. One case where I store them separately is in some of my logging routines. This is mostly because I only ever query on dates and never on date+time.
If you need to query on both date and time, then storing them separately is really problematic, because then you have to concatenate two fields for comparison, and that means your criteria won't use any indexes on the two fields. This may not be an issue for a few thousand records, but for anything above that, it can quickly become quite a performance drag. It's also a major issue if you're using a server back end, since all the rows will have to be pulled across the wire, instead of Access/Jet/ACE being able to hand off the selection to the server.
depends on the requirement. If you are using sql server 2008+ then if you store in separate is not a problem, as well as it is as easy option to write the query

Resources