How to add plus 1 day to a timestamp in Cassandra SQL? - cassandra

I am trying to add 1 day to a timestamp field.
Below is what I have tried but not working:
select studentid, (course_date + 1) as nextday from students where studentid=123;
select studentid,(course_date/DAY+1DAY) as nextday from students where studentid=123;
Kindly help.

Yes, you can specify a time component along with the date arithmetic. In this case, changing 1 to 1d (one day) solves this:
> SELECT studentid,course_date + 1d as nextday FROM students WHERE studentid=1;
studentid | nextday
-----------+---------------------------------
1 | 2022-01-04 06:00:00.000000+0000
(1 rows)
For more information, I recently wrote a blog post on the use of Arithmetic Operators in Apache Cassandra 4.0 as well as a short video on this topic! Below is a more comprehensive list of the valid time duration codes:

Related

How to get last 4 weeks dates in power query (Excel or Power BI)

Using Power Query coding language AKA “m”, write a statement to add a custom column that shows a bit value of whether a Delivery Date is in the past 4 weeks including up to the current date.
Please #help
Result in blank query:
5/13/2021
5/12/2021
5/11/2021
5/10/2021
5/09/2021
Until
4/19/2021
If I understand your requirement correct, you need the custom column code-
if Duration.Days(DateTime.Date(DateTime.LocalNow()) - [Delivery date]) <= 28 then 1 else 0

FY vs last FY Cognos 11

I am looking to generate report in Cognos 11 for attended appointments for this FY vs last FY for the same time frame. For example of I am running report on 1st June 2021, I should get data for sum of appointments of April and May 2021 and sum of appointments of April and May 2020.
Please let me know how can I set the date filter.
Thanks
You didn't provide any context. There are many ways to do this. Just providing a filter expression won't work because I don't know what the rest of your report looks like.
Describing such a general solution for Cognos in plain text is not simple. I'll also assume that, since you are using Cognos, the database structure is a star schema containing a time dimension with columns named [FiscalYear] and [FiscalMonthNumber] that are integers. Also, that your fiscal year runs from January 1 to December 31.
Using Cognos functions:
[FiscalYear] * 100 + [FiscalMonthNumber]
between
_year(_add_months(current_date, -2)) * 100 +
_month(_add_months(current_date, -2))
and
_year(_add_months(current_date, -1)) * 100 +
_month(_add_months(current_date, -1))
or
[FiscalYear] * 100 + [FiscalMonthNumber] + 100
between
_year(_add_months(current_date, -2)) * 100 +
_month(_add_months(current_date, -2))
and
_year(_add_months(current_date, -1)) * 100 +
_month(_add_months(current_date, -1))
Using Cognos macros:
[FiscalYear] * 100 + [FiscalMonthNumber]
between
#timestampMask (_add_months($current_timestamp, -2), 'yyyymm')#
and
#timestampMask (_add_months($current_timestamp, -1), 'yyyymm')#
or
[FiscalYear] * 100 + [FiscalMonthNumber] + 100
between
#timestampMask (_add_months($current_timestamp, -2), 'yyyymm')#
and
#timestampMask (_add_months($current_timestamp, -1), 'yyyymm')#
The advantage of using the Cognos macro is that the value is computed once before sending to the database server, rather than the database server computing it for every row.
Assumption that you have a data item like invoice date, etc, for the example this will be [Date] but you could change the filter to what makes sense based on your data items
The filter could use a date prompt with a parm i.e. PrmDate or use Current_date
see explanation below
Filter should look like:
([Date] between [CurrentStart] and [CurrentEnd])
OR
([Date] between [PriorStart] and [PriorEnd])
Create some data items to make this a little easier
Offset, note: the offset allows you to change your starting month/period
This would be for January
extract(month, current_date) -1
This would be for April
extract(month, current_date) -4
CurrentStart
_first_of_month (_add_months(current_date, -[Offset]))
CurrentEnd, note: if you don't want a prompt, just swap ?PrmDate? with Current_date
_add_months(?PrmDate?,-1)
PriorStart
_add_years([CurrentStart], -1)
PriorEnd
_add_years([CurrentEnd],-1)

How do I get the Month End Personal Time Off Days used?

Using the Start Date and End Date of PTO - Personal Time Off Days Used only count days used up to end of prior month, excluding weekends and U.S Holidays in that certain month. Example of a Holiday is Sept 7th 2015 in the United States.
My goals are:
Create a Data Item Month End Personal Time Off Days used.
Of course it should be getting the number of PTO Days USED from the prior month only.
Exclude weekends in that certain month. So if the Resource takes a Leave on Friday and Monday, Saturday and Sunday should not be excluded in the computation.
How to exclude U.S Holidays, if this is possible that's great but if it's not possible then I'm okay with numbers 1, 2 and 3.
I have created a Data Item column that captures the PTO days used. But this is good for Year to date.
Case when [PTO Info].[PTO Audit].[PTOAuditTypeId] = 31571
and [PTO Info].[PTO Audit].[TimeOffTypeId] = 31566
then [PTO Info].[PTO Audit].[PTODays]
when [PTO Info].[PTO Audit].[PTOAuditTypeId]=31572
and [PTO Info].[PTO Audit].[TimeOffTypeId] = 31566
and [PTO Info].[PTO Audit].[PTODays] < 0
then abs([PTO Info].[PTO Audit].[PTODays] )
else 0 end
I'm not sure if the query below can help.
A calendar table is really going to help you out here. Assuming it has one record per calendar date, you can use this table to note weekends, holidays, fiscal periods vs Calendar periods, beginning of month/end of month dates. A number of things that can help simplify your date based queries.
See this question here for an example on creating a calendar table.
The main point is to create a data set with 1 record per date, with information about each date including Month, Day of Week, Holiday status, etc.
Without a calendar table, you can use database functions to generate your set of dates on the fly.
Getting the Month number for a date can be done with
extract([Month], <date field goes here> )
Getting a list of values from nothing will be required to generate your list of dates (if you don't have a calendar table with 1 record per date to use) will vary depending on your source database. In oracle I use a 'select from all_objects' type query to achieve this.
An example from Ask Tom:
select to_date(:start_date,'dd-mon-yyyy') + rownum -1
from all_objects
where rownum <=
to_date(:end_date,'dd-mon-yyyy')-to_date(:start_date,'dd-mon-yyyy')+1
For Sql Server refer to this stackoverflow question here.
Once you have a data set with your calendar type information, you can join it to your query above:
join mycalendar cal on cal.date >= c.PTOStartDate
and cal.date <= c.PTOEndDate
Also note, _add_days is a Cognos function. When building your source queries, try and use Native functions, like in oracle you can 'c.PTOStartDate + a.PTODays'. Mixing Cognos functions with native functions will sometime force parts of your queries to be processed locally on the Cognos server. Generally speaking, the more work that happens on the database, the faster your reports will run.
Once you have joined to the calendar data, you are going to have your records multiplied out so that you have 1 record per date. (You would not want to be doing any summary math on PTODays here, as it will be inflated.)
Now you can add clauses to track your rules.
where cal.Day_Of_Week between 2 and 6
and cal.Is_Holiday = 'N'
Now if you are pulling a specific month, you can add that to the criteria:
and cal.CalendarPeriod = '201508'
Or if you are covering a longer period, but wanting to report a summary per month, you can group by month.
Final query could look something like this:
select c.UserID, cal.CalendarPeriod, count(*) PTO_Days
from dbo.PTOCalendar c
join myCalendar cal on on cal.date >= c.PTOStartDate
and cal.date <= c.PTOEndDate
where cal.day_of_week between 2 and 6
and cal.Is_Holiday = 'N'
group by c.UserID, cal.CalendarPeriod
So if employee with UserID 1234 Took a 7 day vacation from thursday June 25th to Friday July 3th, that covered 9 days, the result you get here will be:
1234 201506 4
1234 201507 3
You can join these results to your final query above to track days off per month.

Tableau report count total number of articles based on article's publish and unpublish date, filter by parameter control date range

I am newbie to Tableau reporting and need your help to enlighten me on Tableau.
I am looking for a way to count total number of articles based on article's publish and unpublish date, filter by parameter control date range.
I have provided the sample data and sample report for the challenge I am facing.
Basically every article will have publish and unpublish date, I need to count total number of article for each day, based on the date range user select from parameter control.
E.g. If user choose to show article number from 1st Jun to 5th Jun, report X-axis will only from date from 1st Jun to 5th Jun.
I have tried to use calculated field, but seems only can calculate for a day.
2 challenges I see:-
1) How to loop through date range for parameter control
2) How to aggregate total article each day based on publish data and unpublish date
Sample data:-
[ArticleNo][Publish Date][Unpublish Date]
110 2014-May-01 2014-Jun-03
111 2014-Jun-02 2014-Jun-03
112 2014-Jun-03 2014-Jun-30
Sample report format, bar/line graph:-
Report date range : 2014-Jun-1 to 2014-Jun-5 (Parameter Control)
Number of Article Published
^
4 |
3 | X
2 | X X
1 | X X X X X
---------------------------------------------->
| 1-Jun 2-Jun 3-Jun 4-Jun 5-Jun
Report Date Range
I tried to use the following calculated field, but seems can only calculate for a day:-
My calculated field
IF ([Date From] >= [Publish Date] and [Unpublish Date] <= [Date To]) THEN
DATEDIFF('day',[Date From],[Unpublish Date]) + 1
ELSEIF ([Date From] >= [Publish Date] and [Unpublish Date] >= [Date To]) THEN
DATEDIFF('day',[Date From],[Date To]) + 1
ELSEIF ([Date From] <= [Publish Date] and [Unpublish Date] <= [Date To]) THEN
DATEDIFF('day',[Publish Date],[Unpublish Date]) + 1
ELSEIF ([Date From] <= [Publish Date] and [Unpublish Date] >= [Date To]) THEN
DATEDIFF('day',[Publish Date],[Date To]) + 1
END
Thanks for your help first. Cheers.
One solution is to first reshape your data to make the reporting easier, possibly using a custom SQL data connection or data blending or pre-processing.
The custom SQL approach can use a UNION ALL clause to combine a table with itself; data blending can do similar tricks on the client side by having two similar copies of the connection.
Here is a long but good thread discussing a similar, but not identical, problem set.
http://community.tableausoftware.com/thread/120614
Hope this gets you started - no time to write more detail right now.

Quaterly,Weekly Date Functions in Google Bigquery

I am able to get Yearly and Daily,Day of week data from my table using functions here.
But i need ways to achieve two more types of functions Quaterly and Weekly, Like DatePart in SQL Offers. Suggest any methods of achieving it.
The function UTC_USEC_TO_WEEK is mentioned on the same page that you have linked to. This will help you to get the week day.
For quarter, a query something like this may work...
select INTEGER(INTEGER(SUBSTR(date_time, -14, 2))/3) AS QUARTER, count(date_time) as count
from company.summary GROUP BY QUARTER
I have the date time string as year-month-day + time format like...
2012-07-01 23:59:59
Try to use this
EXTRACT(QUARTER FROM date) as quarter
You could use the STRFTIME_UTC_USEC(timestamp_usec,'date_format_str') using the %m format to receive the month (1-12) and an if statement. An example would be:
IF(STRFTIME_UTC_USEC(timestamp_usec,'%m') IN (1,2,3),'Q1',IF(STRFTIME_UTC_USEC(timestamp_usec,'%m') IN (4,5,6),'Q2',IF(STRFTIME_UTC_USEC(timestamp_usec,'%m') IN (7,8,9),'Q3','Q4')))
standard SQL
CASE
WHEN EXTRACT(MONTH FROM date) BETWEEN 7 AND 9 THEN 'Q1'
WHEN EXTRACT(MONTH FROM date) BETWEEN 10 AND 12 THEN 'Q2'
WHEN EXTRACT(MONTH FROM date) BETWEEN 1 AND 3 THEN 'Q3'
WHEN EXTRACT(MONTH FROM date) BETWEEN 4 AND 6 THEN 'Q4'
END AS quarter

Resources