how to calculate date range in sql using interval - sql-server

I have a two tables in one table I have a column contravention date and in other table i have NTO date column
now I want to find how can I retrieve data from tables where NTOdate is greater than 50 days of contravention date.
but when I run this query i get error as Incorrect syntax near '50'.
SELECT [ICPS].[dbo].[tickets].t_vrm
,[ICPS].[dbo].[tickets].t_zone_name
,[ICPS].[dbo].[tickets].t_street_name
FROM [ICPS].[dbo].[tickets]
INNER JOIN [ICPS].[dbo].[ticket_events]
ON [ICPS].[dbo].[ticket_events].[te_system_ref] = [ICPS].[dbo].[tickets].[t_number]
WHERE [ICPS].[dbo].[tickets].[t_camera_ticket] = '0'
AND [ICPS].[dbo].[tickets].[t_date_time_issued] >= convert(DATETIME, '2012/10/01', 101)
AND [ICPS].[dbo].[ticket_events].[te_event_code] = '300'
AND [ICPS].[dbo].[ticket_events].[te_date] > DATE (DATE_SUB([ICPS].[dbo].[tickets].[t_date_time_issued], INTERVAL 50 days))

You can use Datediff function: DateDiff(DD,'firstDate','SecondDate'). Here is the sample:
select [ICPS].[dbo].[tickets].t_vrm,[ICPS].[dbo].[tickets] .t_zone_name, [ICPS].[dbo].[tickets] .t_street_name from [ICPS].[dbo].[tickets] inner join
[ICPS].[dbo].[ticket_events] on [ICPS].[dbo].[ticket_events].[te_system_ref] = [ICPS].[dbo].[tickets].[t_number]
where[ICPS].[dbo].[tickets].[t_camera_ticket] = '0' and [ICPS].[dbo].[tickets].[t_date_time_issued] >= convert(datetime,'2012/10/01',101)
and [ICPS].[dbo].[ticket_events].[te_event_code] = '300' and
DateDiff(DD, [ICPS].[dbo].[tickets].[t_date_time_issued], [ICPS].[dbo].[ticket_events].[te_date]) >50)

Related

Return a zero value if there is no data found

I am trying to create a query to pull the number of new clients/week over 3 months. In the weeks where there was no new customers, it doesn't return anything - I would like it to return a zero value. I understand I need to create a table of all the possible weeks. But I am having trouble understanding how to create the table and join it.
SELECT distinct COUNT(DISTINCT dbo.Quotes.qmpQuoteID) AS "Qty of Quote Lines Won",
MIN(OmpOrderDate) As "WeekStarting"
FROM SalesOrders
Left Outer Join SALESORDERLINES On OMPSALESORDERID = OMLSALESORDERID
Left Outer Join QUOTELINES On OMLQUOTEID = QMLQUOTEID And OMLQUOTELINEID = QMLQUOTELINEID
Left Outer Join QUOTES On QMLQUOTEID = QMPQUOTEID left outer join organizations on dbo.Quotes.qmpCustomerOrganizationID = dbo.Organizations.cmoOrganizationID
WHERE ompOrderDate >= DATEADD(MOnth, -3, GETDATE())
AND cmocustomerprospectdate >= DATEADD(MOnth, -3, GETDATE())
AND OMLQUOTEID > 1
Group By DATEADD(week, DATEDIFF(week, 0, ompOrderDate),0)
Order by MIN(OmpOrderDate) ASC
Thanks in advance!
this returns the results :
Qty of Quote Lines Won Week Starting
1                           23/04/2020
1                           15/05/2020
2                           19/05/2020
1                           etc
2
Ideally this would look like:
Qty of Quote Lines Won Week Starting
1                            21/04/2020
0                           28/04/2020
0                           05/05/2020
1                           12/05/2020
etc - many thanks - I'm sorry my formatting is terrible

Update Column using calculation in last year from another table

I am trying to update (using Inner joins for three tables) item stats STAT for table IM_ITEM by highlighting items that sold less than 12 as "D" (Discontinue).
The 2nd table PS_TKT_HIST_LIN has the Quantity sold column QTY_SOLD for each item on each day and the date column BUS_DAT.
I also need a third table IM_INV to filter the data, I need to say the last received date LST_RECV_DAT for these items is earlier than "2019-01-01" and last sales date LST_SAL_DAT is after "2019-01-01". I used the following code
UPDATE M
SET M.STAT = 'D'
FROM
dbo.IM_ITEM AS M
INNER JOIN
IM_INV AS N
ON
M.ITEM_NO = N.ITEM_NO
INNER JOIN
dbo.PS_TKT_HIST_LIN S`
ON
M.ITEM_NO = S.ITEM_NO
WHERE
CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, N.LST_RECV_DAT))) <= '2019-01-01'
AND CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, N.LST_SAL_DAT))) >= '2019-01-01'
AND M.STAT = 'A'
AND SUM(case when DATEPART(YYYY, (BUS_DAT)) = DATEPART(YYYY, DATEADD(YYYY, -1, getdate()))
AND DATEPART(yyyy, (BUS_DAT)) = DATEPART(yyyy, DATEADD(YYYY, -1, getdate()))
then qty_sold else 0)<12
It comes with an error
Any advise please
You should use HAVING clause instead of Sum in where.
You can use CTE to achieve the value, then update accordingly.
;with cte as(
select ITEM_NO, ..
from ..
group by ITEM_NO
having .. < 12
)
update M
set SET M.STAT = 'D'
from dbo.IM_ITEM AS M
inner join cte on M.ITEM_NO = cte.ITEM_NO
You can't use an aggregate function in where clause unless defined under subquery.

SQL Server: SUM amount when date greater than eomonth

I have a view that returns this result.
I want to sum the amounts where dateValidite is great then a certain end of month (that changes)
For example last day of month is '2016/01/31' so I will sum all the amounts (MontantAuto) where DateValidite > last day of month
I'm using this query but this sum I get is incorrect
select
a.Monnaie
, a.Category
, a.Personne
, EOMONTH(DATEADD(mm,-1,a.DateValidite)) as Previous
, sum(b.MontantAuto) As mnt
from
olap.tf_autorisation_balance a
inner join
olap.tf_autorisation_balance b on a.Id = B.Id
where
EOMONTH(DATEADD(mm,-1,a.DateValidite)) < b.DateValidite
group by
a.Monnaie, a.Category, a.Personne, a.DateValidite
Change your request as :
where EOMONTH(DATEADD(mm,-1,a.DateValidite)) < CONVERT(VARCHAR(10), b.DateValidite, 103)

15 days before today in SQL Server

I am trying to find the list of specific job # from load date in our database with the specific condition and I have to use inner join as well.
i want to have last 15 days worth of Job # in my store procedure.
How do I set up the LoadDate that will change auto for last 15 days only.
Here is my query:
select pr.Job_Number,
Count(ItemCode1) as [Total Records],
si.PackageComplete
from
processed_record pr
inner join scanner_2 si on pr.ItemCode1 = si.ItemCode1
where
pr.Format_Name like '%Lin%' and pr.LoadDate >= '03/01/2016'
group by
pr.Job_Number, si.PackageComplete
order by
si.PackageComplete, pr.Job_Number
Your query should be as follow:
select pr.Job_Number,
Count(ItemCode1) as [Total Records],
si.PackageComplete
from
processed_record pr
inner join scanner_2 si on pr.ItemCode1 = si.ItemCode1
where
pr.Format_Name like '%Lin%' and pr.LoadDate >= DATEADD(DAY,-15,GETDATE())
group by
pr.Job_Number, si.PackageComplete
order by
si.PackageComplete, pr.Job_Number
GETDATE() will get the current date and DATEADD() function will add (subtract) -15 days.
You could use
CONVERT(date, DATEADD(DAY, -15, GETDATE()))
insted your fix Date Value.
With this code you recieve the current Date GetDate() and substract 15 Days DateAdd(day, -15, DateValue)
Finaly you Convert it into Date Typ Convert(date, value) otherwise you would get the current time, too.

t-sql help. need to return that current week of data for date parameter

I have a stored procedure that needs to return a weekly schedule of items being made on different days. It takes in a start date.
#StartDate DATETIME
Based on the startdate passed in, I need to return all the rows of data for that week, with the week starting on Tuesday and ending the following Monday.
Only taking in a startdate, how do I set the start of the week to Tuesday and then have my query return data from that particular week's range?
I know NOTHING of date functions in SQL.
I did see you can set DATEFIRST as 2 (which I think is Tuesday), but I don't know how to take in the date and find that week. I assume I need two variables to set the beginning of the week date and the end of the week date and return data in that range?
Here's my query that currently only returns the current date passed in:
SELECT
PS.ProductionDate,
L.Name,
S.Name,
PS.PartNo,
--PM.DisplayName,
PS.LotNo,
WC.Name,
WC2.Name,
PSS.Name,
PS.Mixing,
PS.Glazing,
PS.StartTime,
PS.[Weight],
PS.Pallets
FROM PROD_ProductionSchedule PS
--LEFT OUTER JOIN PartMaster PM on PS.PartNo = PM.Name
LEFT OUTER JOIN Location L on PS.LocationId = L.Id
LEFT OUTER JOIN PROD_Shifts S on PS.ShiftId = S.Id
LEFT OUTER JOIN PROD_WorkCenters WC on PS.OvenId = WC.Id
LEFT OUTER JOIN PROD_WorkCenters WC2 on PS.LineId = WC2.Id
LEFT OUTER JOIN PROD_ProductionScheduleStatus PSS ON PS.ScheduleStatusId = PSS.Id
WHERE PS.ProductionDate=#StartDate
AND PS.LocationId = CASE WHEN #Location = -1 THEN PS.LocationId ELSE #Location END
AND PS.PartNo = CASE WHEN #PartNo = 'ALL' THEN PS.PartNo ELSE #PartNo END
Get the Tuesday of the current week and 7 days later:
select dateadd(dd, 3 - datepart(dw, getdate()),
getdate()), dateadd(dd, 10 - datepart(dw, getdate()), getdate())
Calculate the beginning and end of the week:
declare #StartOfWeek datetime
declare #EndOfWeek datetime
if DATEPART(weekday,#StartDate) = 3 --Tuesday
begin
set #StartOfWeek = #StartDate
set #EndOfWeek = dateadd(week,1,#StartDate)
end
else if DATEPART(weekday,#startdate) > 3
begin
set #StartOfWeek = dateadd(DAY,-(DATEPART(weekday,#startdate)-3),#StartDate)
set #EndOfWeek = dateadd(DAY,10-DATEPART(weekday,#startdate),#StartDate)
end
else
begin
set #StartOfWeek = dateadd(DAY,-4-(DATEPART(weekday,#startdate)),#StartDate)
set #EndOfWeek = dateadd(DAY,3-DATEPART(weekday,#startdate),#StartDate)
end
Then you can use these in your Where clause:
WHERE PS.ProductionDate between #StartOfWeek and #EndOfWeek
First, create a calendar table with two columns that contain the first and last days of the working week for any given date. Then you can write some very clear, simple code:
declare #FirstDayOfWorkingWeek date,
#LastDayOfWorkingWeek date
select
#FirstDayOfWorkingWeek = FirstDayOfWorkingWeek,
#LastDayOfWorkingWeek = LastDayOfWorkingWeek
from
dbo.Calendar
where
[Date] = #StartDate
select *
from dbo.PROD_ProductionSchedule
where ProductionDate between #FirstDayOfWorkingWeek and #LastDayOfWorkingWeek
And depending on how you store your dates, be aware of the potential issues with using BETWEEN.
Using a calendar table is almost always preferable to using functions for date calculations because you can read and verify the dates easily; you can UPDATE your table to handle exceptions to the regular logic; your code is usually much more readable and easier to maintain; and performance can be better if you JOIN on a table rather than use nested functions.

Resources