I'm trying to do a case statement with three columns which are 30_Pst_Due_dt, 60_Pst_Due_dt, 90_Pst_Due_dt. I need it to do when 30_pst_Due_dt is between 1 and 29 then '1-29 days'. When 60_Pst_Due_dt is between 30 and 59 then '30-59 days', when 90_Pst_Due_dt between 60 and 89 then '60-89 days'.
I have wrote the following in informatica but I got an Pm parse Error:
IIF(30_Pst_Due_dt between 1 and 29, '1-29 days', IIF(60_Pst_Due_dt between 30 and 59, '30 - 59 days', IIF(90_Pst_Due_dt between 60 and 89, '60 - 89 days')))
Between doesn't work in infa. You need to use old school >= and <=.
Since you mentioned pst_due is a date column, add few variable ports to calculate date difference with sysdate. And then use that in the iif clause to calculate final bucket.
v_30_Pst_Due_dt(int)= date_diff(30_Pst_Due_dt,SYSDATE,'D')
v_60_Pst_Due_dt(int)= date_diff(60_Pst_Due_dt,SYSDATE,'D')
v_90_Pst_Due_dt(int)= date_diff(90_Pst_Due_dt,SYSDATE,'D')
Out_bucket (string)=
IIF(v_30_Pst_Due_dt >= 1 and v_30_Pst_Due_dt <=29, '1-29 days',
IIF(v_60_Pst_Due_dt >= 30 and v_60_Pst_Due_dt <=59, '30-59 days',
IIF(v_90_Pst_Due_dt >= 60 and v_90_Pst_Due_dt <=89, '60-89 days'
)))
Pls make sure to add rest of your port just like 30_past_due calculation.
Related
I'm working with DB2 database in which the time field is the following string:
12104102000000
where it means, from the left:
> 1 --> is the century 21 --> is the year 04 --> is the month 10 --> are
> the hours 20 --> are the minutes 00 --> are the seconds 000 --> are
> the milliseconds
Well, the problem is that this field is synchronized with the reference time, so respect to Italy (where I'm working) is two hours back.
I have a query where usually I extract the date field with substr function and the starting part is:
select substr("Message_Time",4,2) || '/' || substr("Message_Time",6,2) || '/' || '20' || substr("Message_Time",2,2) as "Date",
substr("Message_Time",8,2) || ':' || substr("Message_Time",10,2) as "Hour"
I need to add to hour substr only, the 2 hour to bring the date to Italy.
But I think the issue is at the change of the day, because if I have e.g. 20th of April, 22:40, it corrisponds to 21st of April, 00:40 in Italy.
How should I do in order to make it working?
Please tag which DB2 you use.
If DB2LUW or DB2 for I, you can use
to_date('12104102000000', 'YYYMMDDHH24MISSFF1') - 100 YEARS + CURRENT TIMEZONE
I guess it works with DB2 for Z but I'm not sure.
Try this, if you want your result to be in the same string form:
SELECT
SUBSTR (D, 1, 1)
|| SUBSTR
(
TO_CHAR
(
TO_DATE(CAST(19 + SUBSTR(D, 1, 1) AS CHAR(2)) || SUBSTR(D, 2), 'YYYYMMDDHH24MISSFF3') + 2 HOURS
, 'YYYYMMDDHH24MISSFF3'
)
, 3
)
FROM (VALUES '12104102240000') T (D)
I am trying to use a field in my data called priority in order to drive a numerical value for the DATE_ADD function. Essentially, the priority determines how many days before the issue is out of SLA.
I am trying to use this priority by saying:
pseudo code - If priority=p0, DATE_ADD (date, INTERVAL 1 day) Else If priority=p1, DATE_ADD (date, INTERVAL 15 day)
Here is my code I am trying:
SELECT
jira.jiraid as `JIRA / FR`,
jira.priority as `Priority`,
DATE_FORMAT(jira.created,"MM/dd/Y") as `Date Jira Created`,
DATE_FORMAT(DATE_ADD(jira.created, INTERVAL
CASE jira.status
WHEN "P0" THEN 1
WHEN "P1" THEN 15
WHEN "P2" THEN 40
WHEN "P3" THEN 70
ELSE 70
END day),"MM/dd/Y") as `Date when Out of SLA`
FROM jira
Does hive support this type of if/else statements?
You do not need to use INTERVAL in Hive for adding days. date_add function accepts integer days. Calculate interval in the subquery, this will work and look cleaner:
select
s.jiraid as `JIRA / FR`,
s.priority as `Priority`,
DATE_FORMAT(s.created,'MM/dd/Y') as `Date Jira Created`,
DATE_FORMAT(DATE_ADD(s.created, s.days_interval),'MM/dd/Y') as `Date when Out of SLA`
from
(
SELECT
j.jiraid,
j.priority,
j.created,
CASE j.status
WHEN 'P0' THEN 1
WHEN 'P1' THEN 15
WHEN 'P2' THEN 40
ELSE 70
END as days_interval
FROM jira j
)s;
Though you can calculate case statement inside date_add function, placing case statement as a function parameter and it should also work.
Here is the picture of how I would like it to be displayed
Here is what I have so far when use the DATE_ADD function in MySQL Workbench 6.3, but I have been struggling to output the hrs:mins:secs (you don't see the code for that output because I didn't write it here). I know that I can use INTERVAL HOUR_SECOND to display hrs:mins:secs but I don't understand how it works.
SELECT DATE_ADD('2017-01-26', INTERVAL 31 DAY) AS '31 Days';
I know this SELECT statement above will output 31 days from the specified date indicated above, but what do I need to do to output the hrs:mins:secs along with the 31 days from the date in the SELECT statement?
You can do it using DATE_FORMAT(). Examples:
Add function
/*
* Adds a particular interval to given date
*/
SELECT DATE_ADD('2017-01-26', INTERVAL 31 DAY) AS '31 Days';
Output:
31 Days
2017-02-26
Format function
/*
* Formats a particular date object
*/
SELECT DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
AS 'Now';
Output:
Now
Jan 28 2017 04:29 PM
Format and add:
/*
* Adds and Formats date object
*/
SELECT DATE_FORMAT(DATE_ADD('2017-01-26', INTERVAL 31 DAY),'%b %d %Y %h:%i %p')
AS 'Formatted date';
Output:
Formatted date
Feb 26 2017 12:00 AM
Additionally, here is the sqlfiddle for the example: http://sqlfiddle.com/#!9/9eecb7d/92948
Also check the various formats to further tailor to your needs: http://www.w3schools.com/sql/func_date_format.asp
I have a problem which seems to be very simple to solve but I can't. In my Fact table I have a Timestamp field which is a smalldatetime Type. This fact is linked to a Time dimension via its fulldate_Fk (also SmallDatetime). So What I would like to have is to compare the timestamp with the FullDate_FK from the fact to create a calculation like this:
iif([Dim Time].[Date].CurrentMember.MemberValue <=
[Fact].[Timestamp].CurrentMember.MemberValue
,[measures].[YTD Actuals]
,[measures].[YTD Actuals]+[measures].[YTD Com])
But it is not working at all. All [Dim Time].[Date] seem to be evaluated as < than the Timestamp.
P.S: The Timestamp is the last date when the data have been loaded in the DB (in my case 31/08)
Here the result I got:
MONTH | YTD Actuals | YTD Com | Calculation;
JAN , 10 , 10 , 10;
FEB , 20 , 10 , 20;
MAR , 40 , 20 , 40;
MAY , 60 , 30 , 60;
JUN , 70 , 50 , 70;
JUL , 85 , 50 , 85;
AUG , 120 , 55 , 120;
SEP , 120 , 60 , 120;
OCT , 120 , 70 , 120;
NOV , 120 , 80 , 120;
DEC , 120 , 90 , 120;
From August, I should have the sum of Actuals YTD and Com YTD in the calculation, but I still have the Actuals YTD only?
Extra Info
I'm using PivotTable just by dragging attributes in Excel. Month in rows and measures (the 2 YTD and the new calculated member)
If you build a new calc which is:
[Fact].[Timestamp].CurrentMember.MemberValue
What does it return when you add it to your PivotTable? Null? I suspect the CurrentMember is the All member so MemberValue is null. But let's test that.
Do all rows in the fact table have the same Timestamp or are there many different timestamps?
If your fact table has 10000 rows are you expecting the IIf calc will be evaluated 10000 times (once for each row)? That's not the way MDX works. In your PivotTable that has 12 rows the IIf calc gets evaluated 12 times at the month grain.
If you want the calculation to happen on each of the 10000 rows then write the calculation in SQL and do it in a SQL view before it gets to the cube.
To make the calc work as you intend in the cube consider doing the following. Add a new column in your DimTime SQL table called Today Flag. It should be updated during the ETL to be Y only on the row which is today and should be N on other rows. Then add that column as a new attribute to your Dim Time dimension. You can make it Visible=False. Then go to the Calculations tab and flip to the Script view and replace your current [Measures].[Calculation] calc with this:
Create Member CurrentCube.[Measures].[Calculation] as
[measures].[YTD Actuals];
Scope({Exists([Dim Time].[Month].[Month].Members,[Dim Time].[Today Flag].&[Y]).Item(0).Item(0):null});
[Measures].[Calculation] = [measures].[YTD Actuals]+[measures].[YTD Com];
End Scope;
I've seen threads where the document has Start Date and End Date "widgets" where users type in their dates, however, I'm looking for a dynamic solution, for example on the table below, when I select a date, say "1/1/2004", I only want to see active players (this would exclude Michael Jordan only).
Jersey# Name RookieYr RetirementYr Average PPG
23 Michael Jordan 1/1/1984 1/1/2003 24
33 Scotty Pippen 1/1/1987 1/1/2008 15
1 Derrick Rose 1/1/2008 1/1/9999 16
25 Vince Carter 1/1/1998 1/1/9999 18
The most flexible way is to IntervalMatch the RookieYr * RetireYr dates into a table of all dates. See http://qlikviewcookbook.com/recipes/download-info/count-days-in-a-transaction-using-intervalmatch/ for a complete example.
Here's the interval match for your data. You'll can obviously create your calendar however you want.
STATS:
load * inline [
Jersey#, Name, RookieYr, RetirementYr, Average, PPG
23, Michael Jordan, 1/1/1984, 1/1/2003, 24
33, Scotty Pippen, 1/1/1987, 1/1/2008, 15
1, Derrick Rose, 1/1/2008, 1/1/9999, 16
25, Vince Carter, 1/1/1998, 1/1/9999, 18
];
let zDateMin=37000;
let zDateMax=40000;
DATES:
LOAD
Date($(zDateMin) + IterNo() - 1) as [DATE],
year( Date($(zDateMin) + IterNo() - 1)) as YEAR,
month( Date($(zDateMin) + IterNo() - 1)) as MONTH
AUTOGENERATE 1
WHILE $(zDateMin)+IterNo()-1<= $(zDateMax);
INTERVAL:
IntervalMatch (DATE) load RookieYr, RetirementYr resident STATS;
left join (DATES) load * resident INTERVAL; drop table INTERVAL;
There's not much to it you need to load 2 tables one with the start and end dates and one with the calendar dates then you interval match the date field to the start and end field and from there it will work the last join is just to tidy up a bit.
The result of all of that is this ctrl-t. Don't worry about the Syn key it is required to maintain the interval matching.
Then you can have something like this.
Derrick Rose is also excluded since he had not started by 1/1/2004