Tableau Query for using DateDiff and DateTrun at same time - sql

I am new to tableau and i need to find all row set filter on basis of given time frame and then grouping the result of same minute.I can crack this problem in Sql as following
select count(x)
from table t
where datediff(30, min, date)
group by datepart(min, date)
How to fix it in tableau ?

I'm not really sure of what you want but this will give you the number of record per minute, for the records whose date is in the last 30 minutes:
select datepart(MI, date) , COUNT(*) as NumberOfRecordsInMinute
from table t
where date >= dateADD(MI, -30, getdate())
group by datepart(MI, date)
order by datepart(MI, date)

It isn't obvious what your SQL is trying to do here as the standard SQL datediff function works like this datediff(datepart,startdate,enddate) and returns the difference between two dates in the units specified.
If that is what you want, then the Tableau function datediff does much the same job and datediff('minute',start date,enddate) will return the number of minutes between two datetimes and it will be grouped already because the result is discrete.
You can then count the number of records matching each minute difference.

Related

SQL Code to Determine IF Test Date of (A) is Within N Days of Test Date (B) then Return Members Row with Both Dates

I'm writing a SQL query in Teradata to determine which members had an eGFR and uACR test done within 4 days of each other. All of the tests are being pulled in correctly, but not sure who to go about this in the WHERE clause.
SQL Code Sample
I only need data returned where this is true.
I tried this
AND [uACR_2b_2_DATE] <= ([uACR_2b_1_DATE] + 4 Days)
also
other attempt
sample code2
In sql server there is DATEDIFF to determine the distance, this works with days, hours, etc.
DECLARE
#dt1 DATETIME = '20230102 23:59:59'
,#dt2 DATETIME = '20230103 00:00:00'
SELECT difference_ABS = ABS( DATEDIFF(DAY, #dt1, #dt2) )
This ignores the time part, and gives the absolute (no negatives) of the distance of the date part. So in this example You get one day distance, although the two timestamps are one minute apart. If this is what You would like to use, just put it in a WHERE-CLAUSE like so
ABS( DATEDIFF(DAY, uACR_2b_1_DATE, uACR_2b_2_DATE) ) <= 4

Show time difference in minutes

I need to calculate the time differences on a given day. I tried something like that but not works.
CONVERT(TIME, DATEADD(MINUTE, DATEDIFF(MI, MIN(CreatedOn),
MAX(CreatedOn)), 0), 108) AS WorkingTime
Thanks guys
You can try this way, since you don't show the error you get I don't know what detail you can have, but to get the time difference you can get it like this as I leave you here below
DECLARE #MaxDate DATETIME=DATEADD(HOUR,20,GETDATE()) --here you could get the maximum of your table separately
DECLARE #MinDate DATETIME=GETDATE() --here you could get the minimum of your table separately
SELECT DATEDIFF(MINUTE,#MinDate,#MaxDate) AS WorkingTime
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=09128de0d996e85a21992197b524a208
Your question is not entirely clear, but I think this is what you're trying to do.
SELECT CAST(WorkingTime AS DATE),
MIN(WorkingTime) AS min,
MAX(WorkingTime) AS max,
DATEADD(HOUR, DATEDIFF(HOUR, 0, MIN(WorkingTime)), 0) AS truncatedmin,
DATEDIFF(MINUTE,DATEADD(HOUR, DATEDIFF(HOUR, 0, MIN(WorkingTime)), 0),MAX(WorkingTime)) AS [difference]
FROM YourTable
GROUP BY CAST(WorkingTime AS DATE)
The column called "difference" should show the number of minutes difference between the truncatedmin and max columns.
From this column I need to calculate the working time for each day, for this I need to select the first time on a given day and then round it to the full hour (06:08:20 to 06:00:00) and then calculate the differences between the last time in day, which will give me the working time in minutes.
My column

Group data in intervals

I need to group data in intervals of 15 (or X) minutes in sql.
For example, i have the next scenario.
The result that i expect to obtain is
I tried using Lag function but i dont get what i want, because it add interval to each row and continues grouping.
Thanks in advance and apologies for my bad english.
If you want the intervals to be calendar based -- i.e. four per hour starting at 0, 15, 30, and 45 minutes, then you can use:
select id, min(begin_date), max(begin_date)
from t
group by id, convert(date, begin_date),
datepart(hour, begin_date), datepart(minute, begin_date) / 15;
Note that begin date and end date have the same value, so I just used begin_date in this answer.

SQL Query to limit results to yesterday and a specific 8 hour period

Have searched extensively through this site (and others) to try and narrow down the best way to accomplish a SQL query that will return a set of time stamped data results from yesterday within an 8 hour shift window. Couple of challenges are that this is being programmed in a proprietary (DG-LOGIK) data visualization tool that uses a JDBC connector to the SQL database so some of the standard datetime functions aren't recognized.
The query that I have successfully gotten to work is:
SELECT MAX(TimeOfSample) as TimeOfSample, SUM(SampleValue) as SampleValue FROM {trend_log}
WHERE TimeOfSample >= GETDATE()-1 AND (DATEPART(HOUR,TimeOfSample)>=07 AND DATEPART(HOUR,TimeOfSample)<=15)
GROUP BY
DATEPART(YEAR, TimeOfSample),
DATEPART(MONTH, TimeOfSample),
DATEPART(DAY, TimeOfSample),
DATEPART(HOUR,TimeOfSample),
(DATEPART(MINUTE,TImeOfSample) / 15)
ORDER BY TimeOfSample
Challenge: If you are within today's time period of the shift both today and yesterday results return. I tried limiting TimeOfSample = GETDATE()-1, but I get an error return on the equal comparison. How can I limit this query to return only yesterday?
I think this is the where clause you want:
WHERE TimeOfSample >= CAST(GETDATE()-1 as date) AND
TimeOfSample < CAST(GETDATE() as date) AND
(DATEPART(HOUR,TimeOfSample) >= 07 AND DATEPART(HOUR, TimeOfSample) <= 15)
You need to specify the lower and upper bounds of yesterday.

SQL Server- selecting X amount of days between two dates

I have a table with two Columns Date Created and Date Modified and I need to select all of the items where the date modified is more than 5 day past the created date.
I can compare the two columns fine but have not found out how to get it to know how many days between the two.
Thanks for the help.
You can use Datediff function from SQL and specify that you want "day" as datepart.
See msdn documentation about this function.
As JamesZ stated, "day" as datepart will only check if we are past 5 days without checking if 5 days really elapsed. So I added both in the select statement. Just use the one you want.
SELECT NbDays = DATEDIFF(DAY, DateCreated, DateModified),
*
FROM [YourTable]
WHERE DATEDIFF(DAY, DateCreated, DateModified) > 5
Or
SELECT NbDaysElapsed = DATEDIFF(MILLISECOND, StartDateTime, ENDDateTime) / 86400000,
*
FROM [YourTable]
WHERE (DATEDIFF(MILLISECOND, StartDateTime, ENDDateTime) / 86400000) > 5