Subtracting Date/Time from two different tables in SQL - ssms

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

Related

SQL - Returning max count, after breaking down a day into hourly rows

I need to write a SQL query that helps return the highest count in a given hourly range. The problem is that in my table, it just logs orders as they come and doesn’t have a unique identifier that separates hours from hours.
So basically, I need to find the highest number of orders (on any given hour), from 7/08/2022, - 7/15/2022, have a table that does not distinguish distinct hour sets, and logs orders as they come.
I have tried to use a query that combines MAX(), COUNT(), and DATETIME(), but to no avail.
Can I please receive some help?
I've had to tackle this kind of measurement in the past..
Here's what I did for 15 minute intervals:
My datetime column is named datreg in my database log area.
cast(round(floor(cast(datreg as float(53))*24*4)/(24*4),5) as smalldatetime
I times by 4 in this formula, to get 4 intervals inside my 24 hour period.. For you it would look like this to get just hourly intervals:
cast(round(floor(cast(datreg as float(53))*24)/(24),5) as smalldatetime
This is a little piece of magic when it comes to dashboards and reports.

SQL sum amount that lies between two dates

I have the following table in SQL:
Start - End - Amount **per day**
06.07.2020 10.07.2020 10
08.07.2020 08.07.2020 5
08.07.2020 15.07.2020 20
02.07.2020 06.07.2020 3
Now I want to filter this table by the calendar week. Let's say "where [calendar week] = cw28". cw28 is from the 06th of july to the 12th of july.
With that I'd like to have the sum of the amount of the days that lie between those two dates. One single number.
I'm using MS SQL Server (SQL Express).
I can't figure out how to distinguish (and break down) if one day lays between the two date values or not. And if yes how much I need to sum up.
I tried to make a picture in excel to create a logic from this:
"Logic" in Excel
Can anyone help me with this? :)
Thx and Best!,
Max
Not sure about your exact requirement. But below is the query to get the sum of values between two dates.
select sum(amount_of_days) from table where date_column between '06-JUL-2020' and '07-JUL-2010';
Change the column name and table name according to your requirement

SQL Substract min from max on a set of values where criteria is found in another table

and I want for each unique CardID and for each Day individually to calculate MAX(Hour) - MIN(Hour) and sort them ascending by the values of Day column with a single entry of the CardID, number of hours worked and the day they registered the hours.
I am using Microsoft Access, so I need a query for that.
Thanks in advance.

How to extract dates with datatye DATETIME from colum A in table X and put them into Table Y while changing datatype into DATE

Long title, easy meaning:
How is it possible to extract from a date like "2014-04-04 10:47:30.000", which is stored in one column, it's components like year, month and day?
I'm not interested in the time.
For example, I have a table called "Incidents". Inside the table we got a column called "IncidentID" and a column called "ReportingDate", in which dates like the above-mentionend are stored. Let's say we have about 50k Incidents, therefore we have also 50k dates.
A year has 365 days. I want to query for the count of the Incidents, which were reported on different dates - for instance on the 5th of October 2013.
So: How can I get the components of the date and put them into another table while having own columns for the components and how can I query for the Incidents as well?
I guess at first I have to change the datatype of the date from DATETIME to DATE, but I'm not quite sure how to go further. May anyone help me while giving me a code and explains me what it does for a sql-noob? :-)
To achieve this
I want to query for the count of the Incidents, which were reported on
different dates - for instance on the 5th of October 2013.
you haven't do this:
I guess at first I have to change the datatype of the date from
DATETIME to DATE, but I'm not quite sure how to go further.
Just query
SELECT
IncidentID
FROM incidents
WHERE ReportingDate >= '20131005'
AND ReportingDate < '20131006'

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...)