How to create a calculated column with unique values ​for every field - sql

I'm currently facing a problem working with a CV on SAP HANA.
Basically my goal is to create a calculated field on every row, based on the content of another field of the same row.
More specifically, I have a column of dates with the current format (YYYYMMDD) and I have to automatically create an associated field based on current criterias:
the date with the current month and year has to be associated with 0;
every month after the current one, has to be, in order, increased by one (example: for 20220930 I have 0, for 20221030 I have 1, for 20221230 I have 3.. for 20231230 I have 15, this until 600);
if more than one row has the same date, the calculated field has to be the same, that means that, for example, if more than one row has 20221030, I need to have 1 on all of them).
My first idea was to use a rank node, but the outcome is not as I expected.
Attached, there is a screenshot of a data preview of my attempt (just showing the dates and rank_field columns..).
Any advice is welcome, thanks.
Example of data

Related

I need to convert total date to month only

I set up a google forms form for my work where my employees pass information and this information is recorded in a spreadsheet. The information, when recorded, automatically inserts a date and time in the first column of form responses. However, when I enter the code = month (a1), it always returns the answer "1" or "January" and this information does not match the date entered in the column. How do I fix this?
If you are entering '=month(a1)' for every row, then you are always taking the month of the top left cell in the sheet. You would need to adjust the row number for the row you are in.
You could use something like '=month(now())' to ensure you are always getting the month of the current date.

Limiting data on monthly basis from start date to system date dynamically in Tibco spotfire

I've tried limiting data on monthly basis in spotfire and it's working fine.
Now I'm trying to do like getting the records from the current date to month start date.
For suppose if the current date is Sept 21, then i should get the records from Sept 21 to Sept-01(dynamically).
I have a property control to input the number of months.
The easiest way to do this is with Month and Year. For example, in your visualization:
Right Click > Properties > Data > Limit Data Using Expressions (Edit)
Then, use this expression:
Month([TheDate]) = Month(DateTimeNow()) and Year([TheDate]) = Year(DateTimeNow())
This will limit the data to only those rows with the current Year/Month combination in your data column. Just replace [TheDate] with whatever your date column name is.
In other places, you can wrap this in an IF statement if you'd like. It's redundant in this case, but sometimes helps with readability.
IF(Month([TheDate]) = Month(DateTimeNow()) and Year([TheDate]) = Year(DateTimeNow()),TRUE,FALSE)
#san - Adding to #scsimon answer. If you would like to precisely limit values between 1st of the current month to current date, you could add the below expression to 'Limit data using expression' section.
[Date]>=date(1&'-'&Month(DateTimeNow())&'-'&year(DateTimeNow())) and [Date]<=DateTimeNow()

Date Time is not being pulled from the database where numbers from the same column are being pulled in my report

I have two columns:
The first column can only have values 1 or 2.
if 1, then my second column has a number.
if 2, then my second column has a Date Time -( something like this 1/1/2014 12:00:00 AM ).
What I am doing:
I have a tablix with 2 columns..
First column is named a and second is named b
under first I want numbers and under second I want the date time. Basically want to split the second column based on what's on first column i.e. 1 or 2
I have used expressions:
=IIF(Fields!first_column.Value=1, Fields!second_column.Value,nothing)
and for second
=IIF(Fields!first_column.Value=2, Fields!Second_column.Value,nothing)
it works prefectly for the number and gives result whereas the column where there should be the date time gives blank with no results. I have tried switching them, using numbers on both and using dates on both column. but every attempt the numbers work the date time doesnt work.
If I was too wordy and didnt make sense, please let me know and I can answer your specific qustions.
Thank you.

Is it possible to return part of a field from the last row entered into a table

I am proposing to have a table (the design isn't settled on yet and can be altered dependent upon the views expressed in reply to this question) that will have a primary key of type int (using auto increment) and a field (ReturnPeriod of type Nchar) that will contain data in the form of '06 2013' (representing in this instance June 2013).
I would simply like to return 06 or whatever happens to be in the last record entered in the table. This table will never grow by more than 4 records per annum (so it will never be that big). It also has a column indicating the date that the last entry was created.
That column seems to my mind at least to be the most suitable candidate for getting the last record, so essentially I'd like to know if sql has a inbuilt function for comparing the date the query is run to the nearest match in a column, and to return the first two characters of a field.
So far I have:
Select Mid(ReturnPeriod,1,2) from Returns
Where DateReturnEntered = <and this is where I'm stuck>
What I'm looking for is a where clause that would get me the last entered record using the date the query is run as its reference point(DateRetunEntered of type Date contains the date a record was entered).
Of course there may be an even easier way to guarantee that one has the last record in which case I'm open to suggestions.
Thanks
I think you should store ReturnPeriod as a datetime for example not 06 2013 as a VARCHAR but 01.06.2013 as a DATETIME (first day of 06.2013).
In this case, if I've got your question right, you can use GETDATE() to get current time:
SELECT TOP 1 MONTH(ReturnPeriod)
FROM Returns
WHERE DateReturnEntered<=GETDATE()
ORDER BY DateReturnEntered DESC
If you store ReturnPeriod as a varchar then
SELECT TOP 1 LEFT(ReturnPeriod,2)
FROM Returns
WHERE DateReturnEntered<=GETDATE()
ORDER BY DateReturnEntered DESC
I would store your ReturnPeriod as a date datatype, using a nominal 1st of the month, e.g. 1 Jun 2013, if you don't have the actual date.
This will allow direct comparison against your entered date, with trivial formatting of the return value if required.
Your query would then find the latest date prior to your date entered.
SELECT MONTH(MAX(ReturnPeriod)) AS ReturnMonth
FROM Returns
WHERE ReturnPeriod <= #DateReturnEntered

How to have a database counter that resets monthly?

I have an application that needs to generate an unique identifier per item, this identifier is a mix of some initials, the date and 4 digits that increment for each new item. The tricky part is that the last 4 digits should reset monthly.
As an example, lets say we have UID "SP-20121218-0001", the last 4 digits "0001" will increment up until January, and then the first item created in January should have "SP-20130101-0001".
Any thoughts?
create a table with fields Year, Month, CurrentCount. write a stored procedure that selects a row for the current year and month, if it does not exists creates it and increments the number.
To me this sounds like the perfect use case for a stored function.
In postgresql you would do this by having a sequence and then using a function to select a value from the sequence in combination with working out the current date and when the date turned over, resetting the sequence.
The best way though would be to not bother resetting the sequence and to just go with a unique number supplemented by the date.
One possible option is to base the last 4 digits on a database sequence and then have a separate process that will reset that database sequence when a new month starts.