Select last measure date for each ID at the same work date - sql-server

I have a table like the next one and I would like to obtain the last "measure date" for every "Work date" of a same "ID".
At the end I would like to have this result:
In that example, the last two rows of the initial table disappears in the final one because I just want the last "To do" measure entered in the table for every work date of a same ID.
As you see in the first table, for a same ID I can have 2 differents measure_date for a same work date. However I need only the last measure date a same work date by ID. In that case, I need to get the last measure date to get the good "To Do" to achieve my job .
The table can have a lot of different Work Date and 100 of ID, which are the same for every Work date.
How can I do that?

From what I read the query should go something like this,
SELECT id, work_date, max(measure_date), todo FROM tablename GROUP BY id, work_date;
But notice that the todo value will be quite random (or can have unexpected results) so leave it out from the query. But like with the comments, there is not enough information and it does look like excel.

Related

Is there a way of using a row level formula to filter down to opportunities to the end of the month?

I am trying to use a row level formula to filter down a series of opportunities to ones which occur at the end of the month, or within 2/3 days of the end of the month.
I have the following columns: "Opp name", offer submission date, "closed date"
Purpose of the exercise: I would like to identify opportunities which have an offer submission date at the end of the month. Currently, I have filtered the report to last month.
What I would like to do: I would like to filter the data down using a row level formula, so that I have all the opportunities which have an offer submission date around the end of the last month NOT just in the last month.
Please could someone advise me as to the syntax for such a row level formula. Huge thanks in advance!
**** Edit****
This is now what my formula looks like.
And the results are:
As you can see, the records which should be highlighted as 1 (and therefore 'True') aren't. Any help would be hugely appreciated.
My formula runs on Case Last Modified Date, you'll have to change field name. And mine's "DateTime" really, if your custom field is Date only - you don't need DATEVALUE().
For illustration let's say anything after 20th is month's end
IF(DAY(DATEVALUE(LAST_UPDATE)) > 20, 1, 0)
Looks promising:
You can decide to make it a Text formula or maybe really just display the day of the month and filter / sort by it... Doesn't matter much, all yours?

SSRS - Next/Previous Record Button Based on Date

I have a report in SSRS that shows various tablixes based on a date parameter. The user will select a date and the report will populate based on that date. This works fine. However, I would also like the user to be able to click on a "next record" button (and a "previous record" button) and the parameter (and report) will change based on the next date in the database.
The way I would normally accomplish this is by creating a text box with an action. For example, I would create a text box "Next Record", go into text box properties > Action > Go to report >, and send a new parameter that normally would be something like Parameters!survey_id.Value + 1. This works well if I want to increment the numeric ID by a static number (ie, increment by 1). I assume it works for dates too if I want to increment the day by 1 day; but how do I do this if the next record is not one day?
One way I thought would work is to create a row_number field in the dataset that provides the values to the parameter. Here is the code:
select distinct sm.survey_date
,row_number() Over (order by survey_date) as increment
from survey_main as sm
group by sm.survey_date --sometimes there are multiple surveys in one day
order by sm.survey_date desc
which gives the results
survey_date increment
2019-09-16 194
2019-08-24 193
2019-01-14 192
My thought was I could use the increment field in the text box action to find the next survey_date, but I cannot figure out the code (or if it's possible).
Is there a way to make that work? Are there any other suggestions or workaround that you can think of?
Thank you!
There are a few ways to do this, but this is the way I would approach it.
If the reports dataset was something like (this could be refactored but it easier to digest like this)
DECLARE #nextDate date
SELECT #nextDate = MIN(sm.survey_date)
FROM survey_main sm
WHERE sm.survey_date >= #reportParameterDate -- this is the date passed in from the report
SELECT *
FROM survey_main sm
WHERE sm.survey_date = #nextDate
So, first time you run the report, if you passed in 2019-08-24 then you would get data for that date, if you passed in 2019-08-25 you would get data for 2019-09-16
Then on the action for the textbox "button" the date parameter would be and expression something like
=DATEADD("d", 1, FIRST(Fields.survey_date.Value, "myDatasetName"))
This will add 1 to the survey_date that is in the dataset (these should all be the same, hence FIRST() will be fine to use).
Going back to the first run, we set the parameter as normal to 2019-08-24 and get data for that date, now when we click the 'Next Record' button it will take the FIRST survey_date (which will be 2019-08-24) and add 1 day to it and pass it to the report via the action. So we will be passing 201-08-25. The reports dataset will get the MIN survey_date that is >= the date we passed in, in this case 2019-08-25, and return 2019-09-16 which we stick in #nextDate which subsequently filters the data returned.
Hope that makes sense.

Extract data by day from SQL Server

I need to get all the values from a SQL Server database by day (24 hours). I have timestamps column in TestAllData table and I want to select the data which only corresponds to a specific day.
For instance, there are timestamps of DateTime type like '2019-03-19 12:26:03.002', '2019-03-19 17:31:09.024' and '2019-04-10 14:45:12.015' so I want to load the data for the day 2019-03-19 and separately for the day 2019-04-10. Basically, it is needed to get DateTime values with the same date.
Is this possible to use some functions like DatePart or DateDiff for that?
And how can I solve such problem overall?
As in this case, I do not know the exact difference in hours between a timestamp and the end of the day (because there are various timestamps for 1 day) and I need to extract the day itself from the timestamp. After that, I need to group the data by days or something like this and get block by block. For example:
'2019-03-19' - 1200 records
'2019-04-10' - 3500 records
'2019-05-12' - 10000 records and so on
I'm looking for a more generic solution not supplying a timestamp (like '2019-03-19') as a boundary or in a where clause because the problem is not about simply filtering the data by some date!!
UPDATE: In my dataset, I have about 1,000,000 records and more than 100 unique dates. I was thinking about extracting the set of unique dates and then kind of run a query in the loop where the data would be filtered by the provided day. It would look in such a way:
select * from TestAllData where dayColumn = '2019-03-19'
select * from TestAllData where dayColumn = '2019-04-10'
select * from TestAllData where dayColumn = '2019-05-12'
...
I might use this query in my code, so I may run it in the loop from Scala function. However, I am not sure that in terms of performance it would be ok to run separate unique dates extraction query.
Depending on whether you want to be able to work with all the dates (rather than just a subset), one of the easiest ways to achieve this is with a cast:
;with cte as (SELECT cast(my_datetime as date) as my_date, * from TestAllData)
SELECT * FROM cte where my_date = '2019-02-14'
Note when casting datetime to date, times are truncated, ie just the date part is extracted.
As I say though, whether this is efficient, depends on your needs, as all datetime values from all records will be cast to date, before the data is filtered. If you want to select several dates (as opposed to just one or two), however, it may prove overall quicker, as it reads the whole table once and then gives you a column upon which you can much more efficiently filter.
If this is a permanent requirement, though, I would probably use a persisted computed column, which effectively would mean that the casting is done once initially and then only again if the corresponding value changed. For a large table I would also strongly consider an index on the computed column.

Getting minimum values out of calculated table

I have:
a table with user names
a table indicating actions with columns for user name, action time, action name. Named events unique_events
I started collecting data on January. I want to have a column in my table of user names which indicates how long it has been since a user first used my application and the first of January.
So if a user first logged in in January, the value of the row with that user's name will be 0. If one logged in on March it will be 2.
I tried:
Column = DATEDIFF(01-01-2016, MIN(SELECTCOLUMNS(FILTER('events unique_events','events unique_events'[User Name] = Users[User Name]),"DatedTime", [DatedTime])),MONTH)
which returns an error saying the Min function needs a column reference.
I also tried the same with FirstDate instead of MIN which returned an error saying FirstDate can't be used with summarize functions.
Any other ideas on how to achieve this, or fix what I tried?
(for simplicity, I will call your table 'Events', and user login dates field 'User_Login_Date').
First, define your app start date as a measure:
App_Start_Date:= DATE(2016, 1, 1)
Then, define measure that finds min differences between Application Start Date and User Login dates:
User_Start_Diff=: MINX(Events, DATEDIFF([App_Start_Date], Events[User_Login_Date], Month))
Drop this measure into a pivot table against user names, and you should have your desired result.
How it works:
1) MINX goes record by record and calculates date differences for each customer login. It then finds minimum in the results;
2) When you drop the measure into a pivot table, it splits MINX results by customer, and recalculates min for each of them separately. You don't need to do the grouping.
Creation of [Start_Date] measure is not technically necessary but a matter of good style - don't hardcode values in your formulas, always create measures. You will thank yourself later when you need to make a change.

Criteria to get last not null record present

I have a daily record table where records are stored date wise. I am using hibernate criteria to access data. How do i get the last date till which records are present continuously (date wise continuity) by providing a date range. For example, say records are there from 21-09-2012 to 25-09-2012 , again from 27-09-2012 to 31-09-2012. I want to form a query using criteria to get record of date 25-09-2012 because for 26-09-2012 there are no records (by passing date ge 21-09-2012 and date le 31-09-2012) . I want to know the last date till which records are present continuously. Say the table has three fields - 1.recordId (AI) 2.date 3.Integer record.
Its not a proper solution to your question. But it may be scenario specific.
How about getting the data for a date range and show then on a calender. Change the color of date if the corresponding value is null.
I think HQL will be better way to this in Hibernate:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

Resources