I need a count of serial numbers (last 6 months) that are under warranty for a failure rate report - sql

My table has the following columns:
SerialNo
ProductNo
WarrantyBeginDt
WarrantyEndDT
I would like to get a monthly in warranty count looking back about 6 months. I know how to get a month by specifying in the where clause. Would like to have a query that generates the last 6 months with out having to specify the month in the where clause.
SELECT count(*)
FROM Supplemental_Warranty
WHERE WarrantyBeginDt <= '6-15-2022' AND WarrantyEndDt >= '6-15-2022'
How could I create a query that looks back 6 months from the current date?

UPDATED to add group by month for 6 months to get count by month.
This should give you 6 months worth of data using system date (subtract 6 months from today) and end date/time is now so you dont have to specify specific dates, just using date add functionality to subtract 6 months from stating date.
SELECT count(*), MONTH(WarrantyBeginDt) AS CountPerMonth
FROM Supplemental_Warranty
WHERE WarrantyBeginDt BETWEEN DATEADD(MONTH, -6, GETDATE()) AND GETDATE()
--if you have any flags or other logic to identify if it is underwarranty or not
AND IsUnderWarranty = 1
GROUP BY MONTH(WarrantyBeginDt)
NOTE: Not tested but this should do it depending on your SQL technology.

Related

Return the number of months between now and datetime value SQL

I apologize, I am new at SQL. I am using BigQuery. I have a field called "last_engaged_date", this field is a datetime value (2021-12-12 00:00:00 UTC). I am trying to perform a count on the number of records that were "engaged" 12 months ago, 18 months ago, and 24 months ago based on this field. At first, to make it simple for myself, I was just trying to get a count of the number of records per year, something like:
Select count(id), year(last_engaged_date) as last_engaged_year
from xyz
group by last_engaged_year
order by last_engaged_year asc
I know that there are a lot of things wrong with this query but primarily, BQ says that "Year" is not a valid function? Either way, What I really need is something like:
Date() - last_engaged_date = int(# of months)
count if <= 12 months as "12_months_count" (# of records where now - last engaged date is less than or equal to 12 months)
count if <= 18 months as "18_months_count"
count if <= 24 months as "24_months_count"
So that I have a count of how many records for each last_engaged_date period there are.
I hope this makes sense. Thank you so much for any ideas
[How to] Return the number of months between now and datetime value [in BigQuery] SQL
The simples way is just to use DATE_DIFF function as in below example
date_diff(current_date(), date(last_engaged_date), month)

Oracle SQL: Count Weekdays of a Calendar Week

So I want to make a query to show me if a certain calendar week has all 7 Day.
It would be okay if it just returns the numbers 1-7.
The table that I have contains articles of the 3 month of 2020 but even so the first week just contains Wednesday to Sunday it still counts it as a calendar week.
With that select I would make pl/sql Script to check it and if yes something happens.
This is an example of the Table:
Date Articel_Id
14.10.2020 78
15.10.2020 80
16.10.2020 96
17.10.2020 100
18.10.2020 99
Can I Use to_char() to check if Calendar Week has all 7 Days ?
If yes, how ?
The challenging is actually defining the weeks. If you want to define them using the ISO standard, then aggregate:
select to_char(date, 'IYYYY-IW') as yyyyww,
count(distinct trunc(date)) as num_days
from t
group by to_char(date, 'IYYYY-IW')
order by yyyyww;
This counts the number of days per week. I'm not sure if you want to filter, have a flag, or what the result set should look like. For filtering, using a having clause, such as having count(distinct trunc(date)) = 7.

Data of last 6 quarter including current quarter

How to get data of last 6 quarter in Oracle including current quarter. I mean if I run the query today so data between 01-JAN-2018 to 30-JUN-2019 should come in the query.
You could do something like this:
SELECT
*
FROM
DUAL
WHERE
DATE_FIELD >= (SYSDATE - (30*(3*6)))
What this query is doing is taking the current date (SYSDATE), and grabbing all values from the previous 6 quarters. The rationale is:
30 = days in a month | 3 = months in a quarter | 6 = quarters specified by OP
You can use add_months and trunc functions for date value with Q(quarter) argument
select t.*
from tab t
where insert_date between
trunc(add_months(sysdate,-3*5),'Q')
and trunc(add_months(sysdate,3),'Q')-1;
Demo for verification
for the starting date, -3*(6-1) = -3*5 considered, starting from 5 quarter back to be able to count 6 quarter including the current quarter. 3 is
obvious as being the number of months in each quarter.

Trying to Look back 4 whole months in teradata with ADD_MONTHS function in sql statement

I'm trying to go back and retrieve counts for the last 4 full months. This is an example of what I have so far:
SELECT datecolumn, Count(datacolumnA) AS CountOfdatacolumnA, datacolumnB
FROM tableA
WHERE datacolumnB='AA' AND datecolumn >= ADD_MONTHS(CURRENT_DATE, -4)
My results show the last four months plus the current month, October in this case. The problem is that June isn't showing the correct count for the entire month. I'm only getting a partial count for the month.
You need to adjust to the start of the month. You can do this by subtracting the day of the month to get the '0th' of the month and then adding 1 to get the first. (I think dates in teradata are decimals with the int part being number of days since an epoch)
Select
datecolumn,
Count(datacolumnA) As CountOfdatacolumnA,
datacolumnB
From
tableA
Where
datacolumnB='AA' And
datecolumn >=
add_months(current_date, -4)
- extract(day from add_months(current_date, -4)) + 1

Compare date and month in sql server for alert system

I have an a table with two columns birthday and anniversary. I want to get alerts about birthdays and anniversaries between a 7 day period of time but, that should not include year (obviously if I include year, it would always be less than the current date). I want to get the alerts 7 days in advance.
That is, the query should compare the birthday and anniversary with the current date and return a list if their birthday or anniversary falls between 7 days of the same month so that it alerts me in advance about the upcoming birthdays and anniversaries.
Subtract the year difference from now to the requested date and then use datediff to calculate the date difference of the result with the requested date.
SELECT *
FROM Table
WHERE DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Birthday,GETDATE()),GETDATE()),Birthday) BETWEEN 0 AND 7
OR DATEDIFF(dd,DATEADD(yyyy,-DATEDIFF(yyyy,Anniversary,GETDATE()),GETDATE()),Anniversary) BETWEEN 0 AND 7
Try This
SELECT Name,max(Table .birthdate)
FROM Table group by Table .Name having (datediff(day,max(birthdate),getutcdate())>7 and datediff(day,max(birthdate),getutcdate())<8)