Wanting a datediff formula to get the difference in seconds between each trasntype - sql

Was hoping to get some insight into what I should include in a Datediff formula to calculate the difference in time between columns and rows. Using the "timestamp" column as the date, and the "transtype" column as the modifier. So something along the lines of "timestamp (TR) - Timestamp (FIN)". I've attached a screenshot for what the data looks like.
I'm essentially trying to find a formula that calculates the time between each "transaction" or "transtype", per "incident" or "inci_id".
Thank you for your time and assistance.
Data example

Related

Not sure how to create a SQL formula that subtracts timestamps in the same column to get the seconds between actions

Need assistance with creating a "datediff" type of formula.
I have timestamps in the {incilog} table, in different rows across the same column {incilog.timestamp} that I need to subtract from one another, but only if the {incilog.transtype} equals "TR" from the row that has the {incilog.transtype} equal "FPS".
Logic is: If {incilog.transtype} equals "TR" and {incilog.transtype} equals "FPS", then subtract {incilog.timestamp} from {inciog.timestamp} in the corresponding row.
Basically, I need to get how many seconds it took from "TR" to "FPS".
THANK YOU FOR YOUR TIME AND ASSISTANCE!
EXAMPLE OF DATA
timestamp
inci_id
transtype
descript
8/22/2022 12:14:46 AM
2022264051
TR
Time Received
8/22/2022 12:17:00 AM
2022264051
FPS
Fire Pri. Started

Subtracting Date/Time from two different tables in SQL

I have two tables with a time column (year-day-month hr:min:sec)
Let's say name of table 1 is plc and column name Collect
Name of table2 is Adm and column name Disc
I want to subtract the time of Collect (2005-01-03 18:10:05) from the disc column (2005-01-03 20:15:10) in day, hours, minutes.
Any help would be appreciated!
I would surely go for the datediff function.
Have a look at this link:
https://msdn.microsoft.com/en-us/library/ms189794(v=sql.90).aspx
If you want, you can select difference in minutes, and then, with the minutes, calculate the days and hours
Use Date() or Datepart() function to fetch the specific part of the date and subtract the two.
You can get more details of the above two at the following link:
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql

Formatting Day Time X Axis in SSRS Charts

I am working on a SSRS report which compares data of 2 given months. I'm categorising the chart based on Day and time in the following format 1, 6:00 AM. I get this column from the T-SQL itself. But the axis does not come properly. It looks like below now which doesn't make sense. I want it to be in order from 1st to 30th with the time component.
I guess I need some kind of sorting on X-axis with respect to date time. Please help!
After deleting from sorts from chart I'm getting some extra repitive dates after comparing all 30 days of both months. Data from the query looks alright to me!
Thank you!
Ok. Large query
You X-axis are show value in format date,hh mm tt Right ?
Then you want to sort them with day number 1 - 30.
From your query I suggest you add 1 field is like this CAST(SampleCollected AS DATE) [orders] and use this field in Order in Query or Sort on SSRS (not recommend ) and if you use Order in Query must delete sort condition on chart sort.
But if result still not you want try to add MONTH(SampleCollected) As MonthG to order again like this
ORDER BY MONTH(SampleCollected),CAST(SampleCollected AS DATE)
Hope it's Help.

Average of DATE field in Teradata

I have a column of data in [DATE] format. It is a record of the first time an order was purchased. I am attempting to query the average date in this column. Meaning, I want to know what the average "first purchase" is.
Purchase_dt
01-01-2014
02-01-2014
03-05-2014
I need something to show what the average purchase_dt is.
Cheers
You can use a AVG on a DATE column, in fact it's using the same algorithm Tripp Kinetics mentioned. But it's probably using an INT as intermediate result, which soon results in a "numeric overflow"
For a larger number of rows you'll need to do the calculation manually like:
DATE '1900-01-01' + CAST(AVG(CAST(trans_date-DATE '1900-01-01'AS BIGINT)) AS INT)

Sql Queries for finding the sales trend

Suppose ,I have a table which has all the billing records. Now I want to see the sales trend for a user given time duration group by each 3 days ...what should be the sql query regarding this?
please help,Otherwise I am gone ...
I can only give a vague suggestion as per the question, however you may want to have a derived column with a standardised date (as per MS date format, just a number per day) that you could then use a modulus (3) on so that days are equal per 3 day period. You can then group and aggregate over this column to get the values for a 3 day period. Obviously to display the date nicely you would have to multiply back and convert your column as well.
Again I'm not sure of the specifics, but I think this general idea could be achieved to get a result (may well not be the best way so it would help to add more to the question...)