Earliest and Lastdate for each year in sql - sql

I have a column with 3 columns. I have multiple records for a year. As you see some of my records as follows
ID stardate enddate
1 1/1/2010 5/3/2010
2 2/4/2010 NULL -**EDIT**
3 1/2/2011 5/6/2011
4 3/4/2011 NULL -**EDIT**
I want to get a result for the earliest date in that year and the last date in that year. So output could be like
**EDITED:** 1/1/2010 12/31/2010 - For Year 2010
**EDITED:** 1/2/2011 12/31/2011 - For Year 2011
How can i get that in a query?If you need more info,please ask. Thanks
EDIT: If for the year if one of the columns read NULL then I have to consider the last day of the year as the enddate. i.e.12/31/YYYY. And I need to do that for each year again.

Assuming you use DATE (or related) columns in a MySQL table, something like this should serve your request:
SELECT MIN(startdate),
MAX(enddate),
YEAR(startdate)
FROM my_table
GROUP BY YEAR(startdate);
This groups all entries by year (of the startdate) and show you the minimum and maximum entries for each year as you want. See also the documentation for the DATE function in MySQL.
There are similar date functions and possibilities if you are using an other database system. Usually you can easily find them by googling the database system and something like "date functions".

select MIN(stardate),max(enddate)
from [Tablename]
where YEAR(enddate)=2013

Related

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

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.

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.

Determine date gaps

I have a SQL Server table that has a begin date and end date column that denote the beginning and ending range of a pricing schedule.
As the years go by, many versions of this same schedule will be created, with different beginning and ending dates.
What I would like to do is ensure that the user doesn't add, or, in some cases edit, a beginning or ending date in such a way that days would be excluded in the overall time frame.
So if the data looked like this:
Start | End
-----------+--------------
01/01/2015 | 06/30/2015
07/01/2015 | 09/30/2016
10/01/2016 | 12/31/2020
So, lets assume I attempted to revised the last row Start to 10/15/2016. That would create a gap of days between 10/01/2016 and 10/14/2016, but I have no idea who to write a script to do this for me. Ultimately, I would like a list of all missing dates, but even a count of days missing would be great.
Is this possible or am I approaching the issue incorrectly? Any ideas?
Using SQL Server 2012, if it matters.
I am guessing you don't want overlaps either. So, just use lag() and check that it is the date before:
select t.*
from (select t.*,
lag(end_date) over (order by start_date) as prev_end_date
from t
) t
where start_date <> dateadd(day, 1, prev_end_date)

Query records but return grouped by odd date range

SQL Server 2016 to integrate with PowerBI for a static dashboard.
I have a historical table with a couple hundred thousand records in it and I need to pull data out. The data needs to be extracted out by a category type and grouped to give me a count of that type which is easy to do but each of these records has a date associated with it and I want to show counts by group for reoccurring date ranges for a historical pull. The problem I am having is my date ranges need to be from 7/1/{year} to 6/30/{year} as that is our annual cycle. I know how to pull this out by a defined specific year but not when the years overlap like this. The results I need to see are:
Count Category Year
400 Event1 2017-2018
244 Event2 2018-2019
etc.
Suggestions?
Thanks.
Jayson
Subtract 6 months and extract the year to get the fiscal year:
select category,
concat(year(dateadd(month, -6, datecol), '-', year(dateadd(month, -6, datecol)+1),
count(*)
from t
group by category, year(dateadd(month, -6, datecol);

Access query to pull previous month's data

I have a table in Access 2013 (Table1) that contains the following columns:
ID (pk), ReportDate, Amount
The most current data is 30-50 days old. For example, today (6/22/16) the most recent data would be the 5/1/16 row, as the 6/1/16 data won't be entered until mid-July. (All dates in the ReportDate column are the 1st of the month, i.e.: 4/1/16, 5/1/16, etc.)
I need to write a query that will do a 6-month lookback, but exclude the most current month's data.
So, for example, if I ran the query today (6/22/16), I would only get the rows that correspond to the following months:
12/1/2015
1/1/2016
2/1/2016
3/1/2016
4/1/2016
The data for 5/1/16 should be excluded, as it's the most recent month.
I can pull the previous 6 months worth of data with setting the criteria (in QBE) for ReportDate to>=DateAdd("m",-6,Date()), but I can't seem to figure out how to exclude the most recent month.
This should give you the start date of the most recent month in your table:
SELECT Max(ReportDate) AS MaxOfReportDate
FROM Table1;
If that is the month you want to exclude, use that query as a subquery which you cross join back to the table. Then you can use a WHERE clause with a BETWEEN condition whose end points are determined by DateAdd() expressions based on MaxOfReportDate:
SELECT t.ID, t.ReportDate, t.Amount
FROM
Table1 AS t,
(
SELECT Max(ReportDate) AS MaxOfReportDate
FROM Table1
) AS sub
WHERE
t.ReportDate BETWEEN DateAdd('m', -6, sub.MaxOfReportDate)
AND DateAdd('m', -1, sub.MaxOfReportDate);