I'm new here and kind of a noob to sql as well. I need help on this logic. The issue is in the "dateadd" function I believe. I need to get the date range of the current date (August 2014) to 2 years ago (August 2012). One of my mates said I could use a max function but he is not sure how to either. Not sure what to do..please guide me. Thanks!
--count of EDI exceptions daily with current status
select count(InvoiceUniqueness) as [Nbr], CONVERT(varchar(12), ProcessDate, 101) as [Date],
case currentstatus when 1 then 'production' when 2 then 'exception' when 3 then 'archive' end as [CurrentStatus]
from PreProcessTransLog
where (CONVERT(DATETIME, ProcessDate, 102)) >= dateadd (yy,-2,getdate()) and InitialStatus =2
group by CONVERT(varchar(12), ProcessDate, 101), currentstatus
order by CAST (CONVERT(varchar(12), ProcessDate, 101) as smalldatetime) desc
Results from the Query:
6 10/11/2014 production
12 10/10/2014 production
3 09/30/2014 production
2 09/28/2014 production
34 09/27/2014 production
39 09/26/2014 production
150 08/02/2014 exception
40 08/01/2014 production
62 08/01/2014 archive
437 08/01/2014 exception
60 07/31/2014 production
54 07/31/2014 archive
46 07/31/2014 exception
61 07/30/2014 exception
113 07/30/2014 production
98 07/30/2014 archive
7 07/29/2014 exception
130 07/29/2014 archive
80 07/29/2014 production
84 07/28/2014 production
....
....
....
As you can see I'm getting a dates after August like September and October. I just want to current date.
Thanks!!
Can you just add in ProcessDate <= today's date?
SELECT COUNT(InvoiceUniqueness) AS [Nbr] ,
CONVERT(VARCHAR(12), ProcessDate, 101) AS [Date] ,
CASE currentstatus
WHEN 1 THEN 'production'
WHEN 2 THEN 'exception'
WHEN 3 THEN 'archive'
END AS [CurrentStatus]
FROM PreProcessTransLog
WHERE CONVERT(DATETIME, ProcessDate, 102) >= DATEADD(yy, -2, GETDATE())
AND ProcessDate <= GETDATE()
AND InitialStatus = 2
GROUP BY CONVERT(VARCHAR(12), ProcessDate, 101) ,
currentstatus
ORDER BY CAST (CONVERT(VARCHAR(12), ProcessDate, 101) AS SMALLDATETIME) DESC
Related
I had a business requirement which i need to convert the list dates into date range
Logic will be to create Date range upto continuous date if date is not in continuity then new range
will be created
Below is the sample table
ID Module Employeeid Date
--------------------------------------------
11 M1 9 2019-10-01 00:00:00.000
12 M1 9 2019-10-02 00:00:00.000
13 M1 9 2019-10-03 00:00:00.000
14 M2 9 2019-10-04 00:00:00.000
15 M2 9 2019-10-05 00:00:00.000
16 M2 9 2019-10-08 00:00:00.000
17 M2 9 2019-10-09 00:00:00.000
Requried Output
Module Employeeid Start Date End Date
---------------------------------------------------
M1 9 2019-10-01 2019-10-03
M1 9 2019-10-04 2019-10-05
M1 9 2019-10-08 2019-10-09
Below is the query which i have tried and it is working but we need to add from date and to date filter
so after adding filter OutPut is not proper
WITH mycte
AS (SELECT *,
Dateadd(day, -Row_number()
OVER (
partition BY [[module]
ORDER BY [Date]), [Date]) AS grp
FROM [to_shiftschedule]
WHERE employeeid = 535
)
SELECT Min([Date]) AS[StartDate],
Max([Date]) AS[EndDate],
[employeeid],
[module]
FROM mycte
where CONVERT(VARCHAR, Date, 103) >= CONVERT(VARCHAR, '09/01/2020', 103)
AND CONVERT(VARCHAR, Date, 103) <= CONVERT(VARCHAR, '23/01/2020', 103)
GROUP BY[employeeid], [module], grp
ORDER BY[startdate] DESC;
If the problem is with date filter, which is this section, I assume
where CONVERT(VARCHAR, Date, 103) >= CONVERT(VARCHAR, '09/01/2020', 103)
AND CONVERT(VARCHAR, Date, 103) <= CONVERT(VARCHAR, '23/01/2020', 103)
, the reason might be in comparing varchars instead of plain dates.
I suggest you use the following condition:
where Date >= '2020-01-09' and Date <= '2020-01-23'
By the way, I also advise you to edit your final SQL clause, because it is unclear which column you named as "Date".
Please let me know whether it helped or not.
Is possible for my script code to return outstanding days on one column per created_Date?
What I would like my code to do is, have the total days such as 90_days, 120_days, 180_days, 365_days, or, 1 year by the side of Created_Date.
My query lists all data correctly per column name, however, I would like to see only one column displaying total days outstanding per Created_Date not all separate days outstanding with different headed columns.
I have tried the following, and I get all the days separated on separate columns.
Example, this is what I meant:
______________________________________________________
Created_Date | Days Outstanding
2015-01-02 08:29:06 | 90-120
2015-01-02 08:35:44 | 90-120
2015-01-02 08:37:34 | 365
2015-01-02 09:07:01 | 120-180
2015-01-02 09:07:01 | 1 Year Plus
______________________________________________________
[Script Code:]
SELECT DISTINCT ge.Name,
ge.Entity_Type,
ge.Entity_Number,
bc.Super_Entity_ID,
ch.Check_Date, 'Date check was requested
ch.Created_Date, ' Created_Date was paid in full
ch.Check_Number,
ch.Amount,
vn.Vendor_Name,
DATEDIFF(day, [Created_Date], Getdate()) as " Number of Days ", ' Number
of Days Outstanding
'90_days' = CASE WHEN DATEDIFF(day, [Created_Date], Getdate()) Between 90
AND 120 Then [Amount] END,
'120_days' = CASE WHEN DATEDIFF(day, [Created_Date], Getdate()) Between 120
AND 180 Then [Amount] END,
'180_plus' = CASE WHEN DATEDIFF(day, [Created_Date], Getdate()) > 180 Then
[Amount] END
FROM
dbo.gl_entities AS ge ' All linked tables listed with "INNER JOIN"
INNER JOIN
dbo.super_entity AS se
ON ge.Super_Entity_ID = se.Super_Entity_ID
INNER JOIN
dbo.bank_codes AS bc
ON se.Super_Entity_ID = bc.Super_Entity_ID
INNER JOIN
dbo.checks AS ch
ON bc.Bank_Code_ID = ch.Bank_Code_ID
INNER JOIN
dbo.vendors AS vn
ON ch.Vendor_ID = vn.Vendor_ID
WHERE
DATEDIFF(Day, ch.Created_Date, GETDATE ()) > = 90 AND
ge.Active = 1 and vn.active = 1 and (ge.IS_Shadow = 1 OR se.IS_Tiered = 0)
AND CHECK_DATE > '20150101 00:00:00'
AND CHECK_DATE< '20190918 00:00:00'
ORDER BY ch.Check_Date, ch.Created_Date
Use a single CASE expression, like:
CASE
WHEN DATEDIFF(day, [Created_Date], Getdate()) Between 90 AND 119 Then '90 days'
WHEN DATEDIFF(day, [Created_Date], Getdate()) Between 120 AND 179 Then '90 days'
WHEN DATEDIFF(day, [Created_Date], Getdate()) >= 180 Then '180+ days'
END As [Days Outstanding]
I current have a query that grabs the number of parts made per hour between two dates:
DECLARE #StartDate datetime
DECLARE #EndDate datetime
SET #StartDate = '10/10/2018'
SET #EndDate = '11/11/2018'
SELECT
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) AS ForDate,
DATEPART(HOUR, presstimes) AS OnHour,
COUNT(*) AS Totals
FROM
partmasterlist
WHERE
((presstimes >= #StartDate AND presstimes < dateAdd(d, 1, #EndDate))
AND (((presstimes IS NOT NULL))))
GROUP BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111),
DATEPART(HOUR, presstimes)
ORDER BY
CONVERT(VARCHAR(10), CAST(presstimes AS DATE), 111) ASC;
Output:
Date Hour QTY
---------------------
2018/11/06 11 16
2018/11/06 12 20
2018/11/06 13 29
2018/11/06 14 26
Now I need to add another qty column to count where "trimmingtimes" is set.
I can't figure out how to full join the date and hour columns (e.g. presstimes might have 20qty for Hour 2, but trimmingtimes is NULL for Hour 2);
Input:
ID presstimes trimmingtimes
-----------------------------------------------------------------
1 2018-10-10 01:15:23.000 2018-10-10 01:15:23.000
2 2018-10-10 01:15:23.000 NULL
3 2018-10-10 02:15:23.000 NULL
4 NULL 2018-10-10 03:15:23.000
Output:
Date hour Press QTY T QTY
------------------------------------
10/10/18 1 2 1
10/10/18 2 1 0
10/10/18 3 0 1
I suspect you want something like this:
select convert(date, v.dt) as date,
datepart(hour, v.dt) as hour,
sum(ispress) as num_press,
sum(istrim) as num_trim
from partmasterlist pml cross apply
(values (pml.presstime, 1, 0), (pml.trimmingtime, 0, 1)
) v(dt, ispress, istrim)
group by convert(date, v.dt), datepart(hour, v.dt)
order by convert(date, v.dt), datepart(hour, v.dt);
You can add a where clause for a particular range.
I have two working shifts: 8:00:00 to 16:30:00 and 20:00:00 to 06:00:00.
I want to create a stored procedure that will retrieve data from an SQL table when I pass the date
this are my tables
Table1 Emp
ID DateTime EmpID
47 2014-12-05 08:00:00 1111
47 2014-12-05 08:25:00 1235
47 2014-12-05 23:55:00 4569
47 2014-12-06 00:00:00 4563
47 2014-12-06 02:00:00 7412
59 2014-12-06 04:00:00 8523
59 2014-12-05 10:30:00 5632
Table2 Product
ID DateTime ProductMade
47 2014-12-05 11:00:00 Milk
47 2014-12-05 08:00:00 Juice
47 2014-12-06 00:00:00 Bread
47 2014-12-06 06:00:00 Cakes
query for shift 2 18:00 to 06:00
SELECT *
FROM Table 1 as T1
INNER JOIN Table_Product as Prod ON t1.ID=Prod.ID
WHERE T1.DateTime BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-8), 0) + '18:00'
AND DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()-7), 0) + '06:00'
so this will get all the records that has the same ID matching
then i have to do another query for the first shift.
between 08:00 to 16:30
SELECT *
FROM Table 1 AS T1
INNER JOIN
Table_Product AS Prod ON t1.ID=Prod.ID
WHERE DATEDIFF(day, CONVERT(VARCHAR(10), GETDATE(),110), CONVERT(VARCHAR(10), T1.DateTime,110))=-1 AND DATEPART(HOUR,T1.DateTime) BETWEEN '07' AND '16'
How do i make this into one stored procdure and elminate having two queries.
Try this if you want it for a specific shift. Then you have to specify #Shift
Declare #Shift char(1),
#days int
Set #Shift = 'A' -- will only get information for SHIFT A. Change to B if you want the rest
Set #days = 1
Select *
from Table1 t
where t.DateTime between
case when #Shift = 'A' then DateAdd(hour, 8, Convert(date, GetDate() - #days))
else DateAdd(hour, 20, Convert(date, GetDate() - #days)) end
and
case when #Shift = 'A' then DateAdd(hour, 16, Convert(date, GetDate() - #days))
else DateAdd(hour, 30, Convert(date, GetDate() - #days)) end
Specify the Shift and a Date, and it should work.
You can always do something like this as well. This you only have to specify the number of days in the past, and it will retrieve the information and specify the Shift in the first Column
DECLARE #days int
SET #days = 1
Select case when DATEPART(hour, t.DateTime) between 8 and 16 then 'A' else 'B' end AS Shift, *
from Table1 t
where t.DateTime between DateAdd(hour, 8, Convert(date, GetDate() - #days))
and DateAdd(hour, 30, Convert(date, GetDate() - #days))
ORDER BY 1, t.DateTime
It seems that you have two shifts per day and the day shift begins before the night shift. So, let's enumerate the shifts and let you choose the one(s) you want that way:
select t.*
from (select t.*,
row_number() over (partition by cast(sp.datetime as date)
order by sp.datetime
) as shiftnumber
from table t
) t
where DATEDIFF(day, CAST(GETDATE() as DATE), CAST(SP.DateTime as DATE)) = -1 and
shiftnumber = 1;
Note that I also changed the date arithmetic. The conversion to dates uses the built-in DATE type. Converting a date to a string and back to a date is inelegant.
What's a good way to get the most recent 8 PM in SQL? For instance if it was 7/17 4 PM result would be 7/16 8PM, but if it was 7/17 9 PM it would be 7/17 8 PM.
Thanks!
I think so:
select (case when datepart(hour, getdate()) >= 20
then cast(cast(getdate() as date) as datetime) + 20.0/24
else cast(cast(getdate() - 1 as date) as datetime) + 20.0/24
end)