SSRS Date Aging Expression for 0-30 DAYS - tsql

I am working on an aging report BI Project and i am getting stuck on these expressions. I am trying to Count the number of application in between NOW() and the date field. I am getting a count back but not the total number of application in the date range.
0-30 DAYS
=iif(DateDiff("d",Fields!APP_RECEVIED.Value, now())>=0 and DateDiff("d",Fields!APP_RECEVIED.Value,Now())<=30,1,0)
31-60 DAYS
=iif(DateDiff("d",Fields!APP_RECEVIED.Value, now())>=31 and DateDiff("d",Fields!APP_RECEVIED.Value,Now())<=60,1,0)
61-90 DAYS
=iif(DateDiff("d",Fields!APP_RECEVIED.Value, now())>=61 and DateDiff("d",Fields!APP_RECEVIED.Value,Now())<=90,1,0)
>91 DAYS
=iif(DateDiff("d",Fields!APP_RECEVIED.Value, now())>=91 and DateDiff("d",Fields!APP_RECEVIED.Value,Now())<=99999999,1,0)
Thanks,
Arron

Use an execute SQL task to call a script that does these calculations for you. Write the Script in SSMS, test it and make sure the data returns what you need, then put it into the task. SQL is very good at this.

Related

PostgreSQL: UNIX Time Dynamic query

I'm making a Power BI report where data that I need to show is stored in a PostgreSQL database.
In the table where I query there is data from 4 years ago until today, but for my report I only need the last week of records (I know that I can filter using Power BI but my goal is make the lighter query as possible).
The fields of the database related to time, are in UNIX Timestamp, so I'm filtering it now by this way:
SELECT
DATABASE.INCIDENT_NUMBER
,DATABASE.SUBMITTER
,DATABASE.CREATE_DATE
,DATABASE.MODIFIED_DATE
,DATABASE.CLOSED_DATE
,DATABASE.SUBJECT
FROM
DATABASE
WHERE
1643670000 < DATABASE.CREATE_DATE
ORDER BY DATABASE.INCIDENT_NUMBER, DATABASE.CREATE_DATE ASC
That is fine, but I want to improve it making a dynamic query which returns the records from last week till today, without putting a constant of UNIX timestamp.
How can I make that?
That's an excellent example why it is a bad idea to store timestamps as numbers. The correct data type is timestamp or timestamp with time zone.
If you had used the correct data type, your condition would be as simple as
WHERE current_date - 7 < database.create_date
But with numbers, you have to convert back and forth:
WHERE to_timestamp(EXTRACT('epoch' FROM current_date) - 7) < database.create_date

Automating crystal report - need to run for the last week

I have a crystal report I run weekly and manually update the dates to pull the last 7 days (not including today). I've tried to use 'in last7days' and/or {date} >= minimum(lastfullweek) + 1
and {date} <= maximum(lastfullweek) + 1. I can't get this query to run - I get an error saying a string is required here. My date format is yyyy-mm-dd. I need to update this report to run automatically - I don't want to run the report by manually updating the date range every week. Thanks in advance for your help!
You are probably comparing a string to a date. Check the data types of the elements in your expression. Most likely your {date} column is a string rather than a date.
See if you can convert it to a date (e.g. the cDate() function).
Try R-Tag Community Edition. It is free and has the option to set the dates and other values outside the report so you don't have to hardcode them inside the report.

Crystal reports date functions and formula

I'm not even sure how to start writing this logic. I have help desk issues in a SQL database with a create date and a resolved date columns. I'm trying to display only the records in which their create date is 14 days or less than the resolved date. Right now the report prompts for the start date and end date. Now I want to modify it. How do I write this logic and in which section of the formula builder? I'm using CR 2011 Standard.

Crystal Reports parameter returns a number ie 2000. How can I use a formula to count up by one

I need to have a user enter a start Year for a report that will show 10 years of stats beginning from that start year.
I have a parameter where the user inserts the year (ie 2000) as the start year, but the parameter is just recognized as a number not as part of a date or year. Which I thought was fine (I have everything I want working in the report for the first year. Now that I want to go an add a section that takes the parameter and adds one, I'm having trouble.
DateAdd seems to just work with dates and this parameter is passed as a number. I must really be missing something. Any advice would be greatly appreciated.
Try:
Year({table.dateField}) IN {?YearPrompt} TO ({?YearPrompt}+1)

Calculating days between last login and current date

Upon logging into their accounts, each user has their login date and time stored to the database. What I was looking to do however is figure out the amount of days (or preferably convert into months if greater than a month) so that if a user views their profile they can see how active the band are. Also, this could benefit me in terms of keeping active profiles top of the agenda for content on the site so that it doesn't become stale from inactive users content filling up main page content.
I'm using ColdFusion so i'd be looking for a way to find for example how many days ago #lastLogin# was from #now()#. So say if the date of the last login was 23/04/2013 and todays date is 29/04/2013 it would read "Last Active, 1 day ago." However if the last login was 23/03/2013, it would read "Last Active, 1 month ago".
Anybody know how to do this? Thanks.
P.S I currently have no code from testing this as I have no idea where to start in terms of achieving this.
Use DateDiff
<cfset days = dateDiff("d", LoginDateVariable, now()) />
It's as simple as that.
P.S I currently have no code from testing this as I have no idea where
to start in terms of achieving this.
This doesn't answer your direct question but to help you know where to get started, I would strongly suggest reviewing the built in ColdFusion functions and tags that are available to you.
Tags
Tags by function
Functions
Functions by category
Also, Google searches usually land you at the docs, just add "coldfusion" to your search string. Searching google for coldfusion date functions yields very helpful answers, the first of which are a list of all ColdFusion date functions.
Dale's answer is spot on. But I would also suggest returning it as a variable with your query. Let the SQL server do the work. It's very efficient for those types of calculations. Not that CF can't do them well, too. But it's probably more appropriate for SQL to do that lifting. Especially if you're already returning the lastLogin date.
It would be similar to the CF solution:
SELECT ...., lastLogin, DATEDIFF(d, lastLogin, GETDATE()) AS LastLoginDays
FROM ....
WHERE ....
That would give you the number of days. You'd have to decide how you wanted to define a month if you wanted to break it out by month/day. That would get a bit more complex. You could write a SQL function that could be run on both dates and give you an accurate count of days/months/years since last login.
One other thing to keep in mind: Where are the dates being generated? When you insert loginDate into the database, are you doing a now() in CF before you insert it or are you doing a getDate() in SQL when you insert it? Again, I would let the database do your date logic, but you'd want to compare the two dates from the same source. For instance, if your loginDate was a database getDate() then you may not want to compare that to a CF now(). One goes by the datetime of the SQL server and the other goes by the datetime of the CF server. They could be different.