Hi all,
Current my table Info is as follow:
Id | ContactId |Date |
1 | 1 | 2022/12/12
2| 1 |2021/01/01
3| 2 |2017/09/27
4| 2 |2023/09/27
5| 2 |2023/01/01
So I want to apply a filter to return the date closest to the <= current date and show it by contact but without grouping.
The result should be as follow
What is the best way to get this result?
Thanks
Hi Gonçalo,
if you only need the latest date per contact and do not need the Ids, a GROUP BY is the best solution and can be done in an aggregate:
If you also need the Id you need a sub-select. This can be done with the following SQL query:
This will work if each date only appears once per contact. Otherwise, you can also group the result by ContactId and Date and select the MIN or MAX Id:
I hope that I helped you and would be pleased if you mark my answer as solution.
I tested the 2nd solution with OS sample entities and worked fine and when using filters also worked.
When I test with my large DB in the filters it gets a timeout using
(@IsCheck = 0 OR ({Table}.[Id) IN (..solution provided..)
It's hard to analyse where there is the performance issue without knowing the hole query. Have you tried
Why without grouping ????? That seems to me to be the exact thing you should be doing
Hi @Dorine Boudry with grouping how would it look like? Using a checkbox to show the date for each contact.
see @Sebastian Krempel for the full and complete explanation on when you can just use grouping (in that case no need for an SQL widget) and when you need a sub select in an SQL node.
Without a grouping, you can use the SELECT DISTINCT Statement
Hi Goncalo,
Without using GROUP BY clause, any solution would need a sub-query, which is not an optimal solution.
Do you really don't want to use GROUP BY?
Hi @Gonçalo Aguiar
The GROUP BY query would look like this
SELECT {MyEntity}.[ContactId], min(abs(datediff(d, @CurrDate, {MyEntity}.[myDate] )))FROM {MyEntity}GROUP BY {MyEntity}.[ContactId]
I've modified your fieldname Date to myDate for the sake of understanding.
This query would select only unique ContactIDs and display the minimum date difference (be it less than or greater than current date).
I haven't tested this query, but you can use it and do the required modifications as per your actual table and field names
Did you try this solution? Wondering if you encountered any issues in this method.
Try this SQL query. Filter the last 30 days' data.
SELECT {Test}.* FROM {Test} WHERE{Test}.[Date] BETWEEN @StartDate AND @EndDate
Regards,
Dhivyaraj Sivam
Hi @Dhivyaraj Sivam thanks for the input, the user wants with only a checkbox to show the closest date to today for each contact so a range date I guess is not the best approach .
SELECT top (2) {Test}.* FROM {Test} ORDER BY ABS( DATEDIFF(day,{Test}.[Date], GETDATE()));
SELECT Top 2 {Test}.* FROM {Test} WHERE {Test}.[Date] <= CONVERT(date, getdate()) ORDER BY {Test}.[Date] DESC
Please check this one.
Thanks,
Try this SQL query.
Use Day DATEDIFF () and Order By Clause to find the closest.
SELECT Top (1) {Test}.[ContactId],{Test}.[Date], ABS(DATEDIFF(day,{Test}.[Date], GETDATE())) As daydiff FROM {Test} Order BY daydiff