How to count the rows - sql

How i can count the rows between the two dates.
SELECT Count(DATEDIFF(day, ReceiptDate,GETDATE()))
As TotalDays
From JobDetails
Where Receiptdate Between DATEADD(day, -30, GETDATE()) and DATEADD(day, -90, GETDATE())
AND RepairCompleted='N'
Group By ReceiptDate
for example i want to check 30 to 90 days not completed jobs count.

Try this:
SELECT Count(*) As TotalDays
FROM JobDetails
WHERE Receiptdate >= DATEADD(day, -90, GETDATE())
AND Receiptdate <= DATEADD(day, -30, GETDATE())
AND RepairCompleted='N'

Related

Get last 90 days records (SQL)

I have the following query to get the last 90 days records from my DB.
Sample Data below:
my query code:
SELECT
Email
,Country
,Date_of_Birth
,Date_Added
,Received_ProfileCompletionPromoCode
,First_Name
,Purchase_since_entry
,Exit_Date
FROM
Profile_Completion_Journey_Exit_Log
WHERE
Exit_Date >= DATEADD(d, -90, GETDATE())
But I am getting the result where Exit_Date is 10/11/2020. What would be my error here?
Your code gives you records whose date is not older than 90 days ago.
If you want records whose date is exactly 90 days ago, then:
WHERE Exit_Date = DATEADD(day, -90, CONVERT(DATE, GETDATE())
The conversion to date is an important step. GETDATE() returns the current date and time: we need to truncate the time part.
This assumes that Exit_Date is of date datatype. If it has a time component, then:
WHERE Exit_Date >= DATEADD(day, -90, CONVERT(DATE, GETDATE())
AND Exit_Date < DATEADD(day, -89, CONVERT(DATE, GETDATE())
If you want data from 90 days ago, then use:
WHERE Exit_Date >= DATEADD(day, -90, CONVERT(DATE, GETDATE())) AND
Exit_Date < DATEADD(day, -89, CONVERT(DATE, GETDATE()))
This gets results from exactly 90 days ago.
Note the conversion to DATE. Despite its name, GETDATE() has a time component.

Looking for a getdate/dateadd statement to do 1 year and +/- 15 days

-- This is my current code which will allow for me to see all our work orders that have been submitted within the past week, and lets me know if any of the same work orders have appear 6 months ago.
SELECT
A.tagnumber,
count(*) AS CountTotal
FROM
v_workorder A
WHERE
--Date range Within Today and 6 months ago
wo_requestDate BETWEEN DATEADD(month, -6, GETDATE()) AND GETDATE()
AND
EXISTS
( -- Date range Within Today and 7 days ago
select
tagnumber
FROM
v_workorder
WHERE
wo_requestDate BETWEEN DATEADD(DAY,-7,GETDATE()) AND GETDATE()
)
AND
A.wc_description = 'Corrective'
AND
A.itemtype_name = 'Building'
GROUP BY A.tagnumber
ORDER BY CountTotal DESC
--However, Now I would like for my first variable of the getdate/adddate. To check back 1 year ago, +/- 15 days. So essentially 1 year and 15 days back instead of 6 months.
For past 1 year +/- 15 Days
SELECT A.tagnumber, count(*) AS CountTotal
FROM v_workorder A
WHERE wo_requestDate BETWEEN DATEADD(day, -15, DATEADD(year, -1, GETDATE())) AND DATEADD(day, 15, DATEADD(year, -1, GETDATE()))
AND
EXISTS ( select tagnumber FROM v_workorder WHERE wo_requestDate BETWEEN DATEADD(DAY,-7,GETDATE()) AND GETDATE() )
AND
A.wc_description = 'Corrective' AND A.itemtype_name = 'Building'
GROUP BY A.tagnumber
ORDER BY CountTotal DESC
For 1 year
SELECT ...
FROM ...
WHERE wo_requestDate BETWEEN DATEADD(year, -1, GETDATE()) AND GETDATE()
AND...;
For 15 Days
SELECT ...
FROM ...
WHERE wo_requestDate BETWEEN DATEADD(day, -15, GETDATE()) AND GETDATE()
AND...;
Other possible options of DATEADD()
year
quarter
month
dayofyear
day
week
weekday
hour
minute
second
millisecond
See more here.
.
To eliminate issues with the time component of datetime:
CAST(GETDATE() AS DATE
Find the date from a year ago:
SELECT DATEADD(YEAR, -1, CAST(GETDATE() AS DATE));
From there, subtract 15 days and add 15 days in your end points.
...
WHERE
wo_requestDate >= DATEADD(DAY, -15, DATEADD(YEAR, -1, CAST(GETDATE() AS DATE)))
AND
wo_requestDate < DATEADD(DAY, 15, DATEADD(YEAR, -1, CAST(GETDATE() AS DATE)))
I prefer >= and < to BETWEEN, especially with dates, just to avoid any ambiguity with the time component, so you may want to add 16 days to the last parameter if you want the range to include the 15th day out.

Query to convert UTC to CST

I'm using SQL Server to count rows in a 24 hour period. I've accomplished that, however I can't figure out how to convert the UTC time to CST that the database stores.
Then...
How to breakdown the 24 hours into 24 x 1 hour blocks with a SUM or Count of each hours rows?
Convert UTC TIME to CST?
Query?
-- THEN
Select COUNT (*) AS Total
From readmodels.Database
Where
Timestamp >= '2018-01-18' AND
Timestamp <= '2018-01-19'
-- Then Breakdown the count into 24 - 1 hour blocks
If you only need to convert from UTC to CST. You can simply use DATEADD(hour, -6, Timestamp) in your query.
e.g.
Select COUNT(*) as count, DATEPART(year, DATEADD(hour, -6, Timestamp)) as year, DATEPART(month, DATEADD(hour, -6, Timestamp)) as month, DATEPART(day, DATEADD(hour, -6, Timestamp)) as day, DATEPART(hour, DATEADD(hour, -6, Timestamp)) as hour
From readmodels.Database
Where
DATEADD(hour, -6, Timestamp) >= '2018-01-18' AND
DATEADD(hour, -6, Timestamp) <= '2018-01-19'
Group by DATEPART(year, DATEADD(hour, -6, Timestamp)), DATEPART(month, DATEADD(hour, -6, Timestamp)), DATEPART(day, DATEADD(hour, -6, Timestamp)), DATEPART(hour, DATEADD(hour, -6, Timestamp))
--this is what I ended up using
SELECT dateadd(hour, datediff(hour, 0, TimeStamp), 0) as TimeStampHour_CST, Count(*) As Total_Per_Hour
FROM readmodels.database
WHERE Timestamp >= '2018-01-17' AND
Timestamp <= '2018-01-18'
GROUP BY dateadd(hour, datediff(hour, 0, TimeStamp), 0)
ORDER BY dateadd(hour, datediff(hour, 0, TimeStamp), 0);

How to get the 'yesterday' business day in sql

I have a query that gets the yesterday records, but I want that in monday I be able to get the records from friday, not sunday.
Here what I have so far:
select * from tb_interaction
where
(DateInteraction >= DATEADD(day, DATEDIFF(day, 1, GETDATE()), 0)) AND
(DateInteraction < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
Any suggestions?
Thanks!
If your settings are set for English, then you can do:
select i.*
from tb_interaction i cross join
(select (case when datename(getdate()) = 'Monday' then 3
when datename(getdate()) = 'Sunday' then 2
else 1
end) as diff
) x
where DateInteraction >= dateadd(day, - diff - 1, cast(getdate() as date)) and
DateInteraction < dateadd(day, - diff, cast(getdate() as date))

Get Sum of all Distinct in a week

I am wondering where exactly i am not clear with this query. I want to get the count of all distinct RepIDs that worked in a particular week. This is In SQL Server 2005. Thank you!!
This query gives me distinct RepID's for the whole week. I want to count RepID twice if he has records on 2 different days but count only once even if he has more than 1 record for any partiular day.. I hope i am clear. I am sorry that i was not clear before! Thank you!
Select count(distinct(RepID)) as SalesPeople from DailyInfo
where Date > DATEADD(dd, -(DATEPART(dw, #Date)-1), #Date)
and Date < DATEADD(dd, 7-(DATEPART(dw, #Date)), #Date)
You can make unique combinations of the RepID+Date to make it unique (SQLFiddle):
SELECT COUNT(distinct RIGHT(DateDiff(d,0,Date),10)
+RIGHT(RepID,10)) as SalesPeople
FROM DailyInfo
WHERE Date > DATEADD(dd, -(DATEPART(dw, #Date)-1), #Date)
AND Date < DATEADD(dd, 7-(DATEPART(dw, #Date)), #Date);
I have assumed DailyInfo.Date can contain time information. You can swap DateDiff(d,0,Date) above for just Date. Similarly, CAST(DateDiff(d,0,Date) as datetime) below can be just `Date.
Below is the query if you needed to see the breakdown for each day.
SELECT CAST(DateDiff(d,0,Date) as datetime) TheDay,
COUNT(distinct RepID) as SalesPeople
FROM DailyInfo
WHERE Date > DATEADD(dd, -(DATEPART(dw, #Date)-1), #Date)
AND Date < DATEADD(dd, 7-(DATEPART(dw, #Date)), #Date)
GROUP BY CAST(DateDiff(d,0,Date) as datetime) -- by day
ORDER BY TheDay
Let me answer this by suggesting how you should think about the problem. You are looking for the number of reps per day. So, your query should have a summary (subquery) at this level. Then, you can count the number of days per week.
Assuming that your date does not have any time component, you can use the following:
select count(*)
from (select RepId, date as thedate, count(*) as NumOnDay
from DailyInfo
group by RepId, date
where Date > DATEADD(dd, -(DATEPART(dw, #Date)-1), #Date)
and Date < DATEADD(dd, 7-(DATEPART(dw, #Date)), #Date)
) rd
Alternatively, you could count the number of days that a rep worked during a week and then add these up:
select sum(numdates)
from (select RepId, count(distinct date) as numdates
from DailyInfo
group by RepId
where Date > DATEADD(dd, -(DATEPART(dw, #Date)-1), #Date)
and Date < DATEADD(dd, 7-(DATEPART(dw, #Date)), #Date)
) rd
If your date field has a time component, then you need to remove the time component for this to work. Or use some trick such as day(date), since the day function will returns a different value for each date in a week. In later versions of SQL Server, you can just cast(date as date), if the original date is datetime.
Select count(1)
from DailyInfo
group by convert(varchar(10),[date], 120)
where [put your condition here]
You could use a CTE, but might be over kill. And have the group by the day in the CTE and you need to do is a sum of the totals.
WITH cte ([day], total) as
(
Select DATENAME(DW,[Date]), count(distinct(RepID)) as SalesPeople from DailyInfo
where [Date] > DATEADD(dd, -(DATEPART(dw, #Date)-1), #Date) and [Date] < DATEADD(dd, 7-(DATEPART(dw, #Date)), #Date)
GROUP BY DATENAME(DW,[Date])
)
select SUM(total) FROM cte;
To do what I think you want you need to group by day, and filter on the week, and then do a distinct on the result:
Select Distinct(RepID)
From (Select RepID
Group By DateDiff(day, 0, Date)
From DailyInfo
Where Date > DateAdd(dd, -(DATEPART(dw, #Date)-1), #Date)
And Date < DateAdd(dd, 7-(DATEPART(dw, #Date)), #Date)