What I need is a data item expression which outputs the starting date of the current quarter of last year. Finding the current year is easy, as is subtracting 1 year from that date. But beyond that I get stuck.
I currently have an ugly if expression for each quarter like:
if (extract(month,current_date) in (10,12,12))
then ((extract(year,_add_years (current_date,-1))||'-10-01'))
But no matter what I do I can't concatonate the year and date into string I can convert to a date object. The above code gives the error:
The operation "add" is invalid for the following combination of data types: "integer" and "character"
Trying to cast the integer as a character using cast() I get this error. I also get this error when trying to turn a character array into a date:
The operation "condexp" is invalid for the following combination of data types: "character" and "integer"
Trying to use SQL Server specific functions (it is a SQL Server database) just gives me an error that those functions are unavailable for local processing, so I can't seem to use SS date arithmatic, and I can't find anything particularly applicable in Cognos' built in date functions.
How can I manipulate a date to add a year to a known day/month combination and use that as a date object?
I'm using cognos 10.1.1, but it would work.
You can get the value by using the following code:
_make_timestamp(
extract(year, _add_years(current_date, -1)),
(floor((extract(month,current_date)-1)/3)*3+1),
01
)
The following is the simple way to get the starting month of the quarter.
(floor((extract(month,current_date)-1)/3)*3+1),
and you can convert timestamp to date or string.
cast(
_make_timestamp(
extract(year, _add_years(current_date, -1)),
(floor((extract(month,current_date)-1)/3)*3+1),
01
),
date
)
,
cast(
cast(
_make_timestamp(
extract(year, _add_years(current_date, -1)),
(floor((extract(month,current_date)-1)/3)*3+1),
01
),
date),
varchar(10)
)
plus, you can use extract function when you get a day value.
extract(day, _first_of_month (current_date))
I would go with SQLSERVER built-in functions tht exists in Cognos.
Here is an expression that works for me:
DATEADD({month},(3)*((DATEPART({quarter},[FullDateAlternateKey]))-1),
DATEADD({YEAR}, DATEDIFF({YEAR}, 0, dateadd({year},-1,[FullDateAlternateKey])), 0))
The FullDateAlternateKey field is my date field (from ADVERTUREWORKS DW DB).
If you still have problems, try to isolate the problem by trying this expression on new simple list report.
Related
I have a table with the following (with other fields removed)
CREATE TABLE if NOT EXISTS request_audit (
user_id text,
request_body text,
lookup_timestamp TIMESTAMP
PRIMARY KEY ((user_id), lookup_timestamp)
) WITH CLUSTERING ORDER BY ( lookup_timestamp DESC);
I create a record with the following
INSERT INTO request_audit (user_id, lookup_timestamp, request_body) VALUES (?, ?, toTimestamp(now()))
I am trying to retrieve all rows within the last 24 hours, but I am having trouble with the timestamp,
I have tried
SELECT * from request_audit WHERE user_id = '1234' AND lookup_timestamp > toTimestamp(now() - "1 day" )
and various other ways of trying to take a day away from the query.
Cassandra has a very limited date operation support. What you need is a custom function to do date math calculation.
Inspired from here.
How to get Last 6 Month data comparing with timestamp column using cassandra query?
you can write a UDF (user defined function) to date operation.
CREATE FUNCTION dateAdd(date timestamp, day int)
CALLED ON NULL INPUT
RETURNS timestamp
LANGUAGE java
AS
$$java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
c.add(java.util.Calendar.DAY_OF_MONTH, day);
return c.getTime();$$ ;
remember that you would have to enable UDF in config. Cassandra.yml. Hope that is possible.
enable_user_defined_functions: true
once done this query works perfectly.
SELECT * from request_audit WHERE user_id = '1234' AND lookup_timestamp > dateAdd(dateof(now()), -1)
You couldn't do it directly from CQL, as it doesn't support this kind of expressions. If you're running this query from cqlsh, then you can try to substitute the desired date with something like this:
date --date='-1 day' '+%F %T%z'
and execute this query.
If you're invoking this from your program, just use corresponding date/time library to get date corresponding -1 day, but this depends on the language that you're using.
I am using sql as language for my notebook in databricks.
Want to get the day of week from the date given.
For doing this i used to_char(date,'fmday'). Getting error as function is not registered as temporary or permanant in databricks. Is there a way to get the name of day by other means.
Date is in format yyyymmdd
You are getting that error because to_char is not a SparkSQL function. You can see the list of functions in the ScalaDocs here: https://spark.apache.org/docs/latest/api/scala/org/apache/spark/sql/functions$.html
If your date is a DateType, you can do dayofweek(date) in SparkSQL.
get the name of the day
Being as you want to get the name of the day, you can use the date_format function with the argument 'EEEE' to get the day name, eg Monday. If you want to pass in an integer (eg numbers between 1 and 7) then you could just code a CASE statement, something like:
%sql
SELECT
dayofweek( CAST( '2018-12-31' AS DATE ) ) AS d,
date_format( CAST( '2018-12-31' AS DATE ), 'EEEE' ) AS dayname,
CASE dayofweek( CAST( '2018-12-31' AS DATE ) )
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
WHEN 6 THEN 'Saturday'
WHEN 7 THEN 'Sunday'
ELSE 'Unknown'
END AS caseTest
NB I have coded the CASE to start the week from Day 1 - Monday, which is different to the dayofweek default; this might be one reason to do that, ie you want a different default.
My Results:
I got a way to get the name of day of week as below
date_format(to_date('20170821','yyyyMMdd'),'EEEE')
Now i want to pass a column of integer datatype, but when i pass it to query getting null as output. Could someone please help
I have a few things I want to accomplish with Presto. I currently getting some data in the following formats
date 16-Jan-2018
num 1000
I want to write a query that can convert these values to
2018-01-16
1,000
For the date you could do the following:
select date_parse('date 16-Jan-2018','date %d-%b-%Y')
For the second field, you would have to split it first with split(string, delimiter), then cast the second array element to INTEGER.
Here is the full answer:
SELECT date_parse(date_string,'date %d-%b-%Y') as parsed_date,
CAST(
split(int_string, ' ')[2] AS INTEGER
) as parsed_int
FROM (VALUES ('date 16-Jan-2018', 'int 1000'))
AS t(date_string, int_string)
I would like to calculate the sum of open positions in a receivables account. The entries in the accounting system provide three relevant columns in the source table to that end:
booking date
due (=pay) date
amount due
I would like to have a measure that I can use for a graph, showing the total of all open positions on each day.
An open position is an amount booked with a booking date before "today" and with a due date after "today".
I tried the following approach in my Power Pivot model (with three calendar tables):
booking date related to "calendar table 1"
due date related to "calendar table 2"
Date columns of "calendar table 1" and "calendar table 2" related to a third "calendar table main"
For that formula I am getting an error message:
Hm, not sufficiently proficient in PowerPivot to solve this problem.
SumAmt:=
SUM( Source_Table[Amount] )
OpenPositions:=
CALCULATE(
[SumAmt]
;FILTER(
VALUES( Source_Table[Booking_Date] )
;Source_Table[Booking_Date] < MAX( Calendar_Main[Calendar_Date] )
)
;FILTER(
VALUES( Source_Table[Due_Date] )
;Source_Table[Due_Date] > MAX( Calendar_Main[Calendar_Date] )
)
)
Your error is pretty self-explanatory. If you use a direct column reference in CALCULATE() you can only reference a single column. You are referencing two, Calendar_Main[Calendar_Date] and either Source_Data[Booking_Date] or Source_Data[Due_Date]. This is simply not allowed, so it throws the error.
The workaround is simply to wrap complex filtering logic in table expressions and use those as arguments to CALCULATE(). Pretty much, unless you are hard-coding a literal predicate for a single column, you should be using some sort of table expression, like FILTER(), as your arguments to CALCULATE().
What we do is call FILTER() twice to check the dates. We use MAX()s because we cannot perform comparisons between column references, we need to perform inequality comparisons between scalars.
Since we're FILTER()ing over Source_Data[Booking_Date] and Source_Data[Due_Date], the references to these are evaluated in row context and refer to the value of the current row in FILTER()'s iteration. The reference to Calendar_Main[Calendar_Date] is just a column reference, so we wrap it in MAX() to get a scalar value for our inequality. The MAX() refers to the current filter context coming in from the pivot table, which would be the current row label or column label.
If you aggregate to the month level, this will give you essentially the closing balance, since we're using MAX()s. At the month level the value will be identical to that on the last date of the month.
Finally, with the inequalities you've set up, you're ignoring anything opened on the current day or due on the current day. I'd expect you want [Booking_Date] <= [Calendar_Date] and [Due_Date] > [Calendar_Date].
Short version: Is it possible to query for all timeuuid columns corresponding to a particular date?
More details:
I have a table defined as follows:
CREATE TABLE timetest(
key uuid,
activation_time timeuuid,
value text,
PRIMARY KEY(key,activation_time)
);
I have populated this with a single row, as follows (f0532ef0-2a15-11e3-b292-51843b245f21 is a timeuuid corresponding to the date 2013-09-30 22:19:06+0100):
insert into timetest (key, activation_time, value) VALUES (7daecb80-29b0-11e3-92ec-e291eb9d325e, f0532ef0-2a15-11e3-b292-51843b245f21, 'some value');
And I can query for that row as follows:
select activation_time,dateof(activation_time) from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e
which results in the following (using cqlsh)
activation_time | dateof(activation_time)
--------------------------------------+--------------------------
f0532ef0-2a15-11e3-b292-51843b245f21 | 2013-09-30 22:19:06+0100
Now lets assume there's a lot of data in my table and I want to retrieve all rows where activation_time corresponds to a particular date, say 2013-09-30 22:19:06+0100.
I would have expected to be able to query for the range of all timeuuids between minTimeuuid('2013-09-30 22:19:06+0100') and maxTimeuuid('2013-09-30 22:19:06+0100') but this doesn't seem possible (the following query returns zero rows):
select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time>minTimeuuid('2013-09-30 22:19:06+0100') and activation_time<=maxTimeuuid('2013-09-30 22:19:06+0100');
It seems I need to use a hack whereby I increment the second date in my query (by a second) to catch the row(s), i.e.,
select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time>minTimeuuid('2013-09-30 22:19:06+0100') and activation_time<=maxTimeuuid('2013-09-30 22:19:07+0100');
This feels wrong. Am I missing something? Is there a cleaner way to do this?
The CQL documentation discusses timeuuid functions but it's pretty short on gte/lte expressions with timeuuids, beyond:
The min/maxTimeuuid example selects all rows where the timeuuid column, t, is strictly later than 2013-01-01 00:05+0000 but strictly earlier than 2013-02-02 10:00+0000. The t >= maxTimeuuid('2013-01-01 00:05+0000') does not select a timeuuid generated exactly at 2013-01-01 00:05+0000 and is essentially equivalent to t > maxTimeuuid('2013-01-01 00:05+0000').
p.s. the following query also returns zero rows:
select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time<=maxTimeuuid('2013-09-30 22:19:06+0100');
and the following query returns the row(s):
select * from timetest where key=7daecb80-29b0-11e3-92ec-e291eb9d325e and activation_time>minTimeuuid('2013-09-30 22:19:06+0100');
I'm sure the problem is that cqlsh does not display milliseconds for your timestamps
So the real timestamp is something like '2013-09-30 22:19:06.123+0100'
When you call maxTimeuuid('2013-09-30 22:19:06+0100') as milliseconds are missing, zero is assumed so it is the same as calling maxTimeuuid('2013-09-30 22:19:06.000+0100')
And as 22:19:06.123 > 22:19:06.000 that causes record to be filtered out.
Not directly related to answer but as an additional addon to #dimas answer.
cqlsh (version 5.0.1) seem to show the miliseconds now
system.dateof(id)
---------------------------------
2016-06-03 02:42:09.990000+0000
2016-05-28 17:07:30.244000+0000