Query records but return grouped by odd date range - sql

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);

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.

SQL for in between dates for multiple years

I'm doing free courses in the Oracle devgym and I came across a question about dates that I am unfamiliar with. If I have a table of sales with dates and I want to display only dates between any two given months for every possible year within the sales table how would I go about doing that. This is what I have but I don't know how to do it for multiple years without is showing everything in between those years also. I'm trying for instance to show all sales for dates in between August and December for every year in the table. Is it possible to do without knowing all the years within the table up front?
Select *
From Sale
Where SaleDate BETWEEN '08/01/2013' and '12/31/2013'
Order by SaleDate
You can use EXTRACT(MONTH FROM date) to get the month of a date
Select *
From Sale
Where EXTRACT(MONTH FROM SaleDate) BETWEEN 8 and 12
Order by SaleDate
The best way to tackle this is to use the DAYOFYEAR() function in MYSQL which makes things very easy. It returns a value from 1 to 366 for the day of the year. This totally eliminates the year restriction ALSO maintaining the bound of a single year. If you need to search between years, you have to specify the year numbers.
Select * From Sale Where SaleDate BETWEEN DAYOFYEAR(08/01/2013) and DAYOFYEAR(12/31/2013) Order by SaleDate;

I want data for customers for last three months from there deactivation date So I joined two

I want data for customers for last three months from there deactivation date
So I joined two tables
One table have data like recharge, Report_Month
Other table have deactivation date
Both table have mobile number column
So I have condition in where clause
Disconnection date between 2019-05-07 and 2019-08-10
And
Report month between ?????
So I have trouble in Report month condition
Report month data like 1,2,3 upto 12
So I want data between disconnection date and before three months of disconnection date
Sorry, I'm having trouble understanding your question, I think you're asking how to select the last three months worth of data from a disconnection date. If so you'll need a condition like:
WHERE `Report_Month` >= DATEADD(MONTH, -3, `Disconnection_date`)
So this will return everything with a report month up to three months before disconnection date.
The second part confuses me more, I think you're asking to group the report month, up to 12 months, but, if you're only retrieving the last three months data, then how could you group by 12?
GROUP BY Year(`Report_Month`), Month(`Report_Month`)
ORDER BY Year(`Report_Month`), Month(`Report_Month`)
That will group your results by Month in order, if that's what you're after?

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);

Group Dates by month

I've been working on this query and I'm about 90% where I need to be however there is one piece of this that I'm unable to figure out. Basically I'm looking for the Sum of Net flows by month, starting 12/31/2014 through the current date. I'm able to extract data by day and the sum of net flows for that day, however now I really need to be able to group all the dates in to their respective months. Ex. If I have 01/01/2015, 01/02/2015 and 01/03/2015 I just want both of them to be grouped together and show up as 01/2015. Bellow is the query that I have written. Please help with this last step.
SELECT
DATE,
SUM(NET_FLOWS/1000000.00) AS YTD_NET_FLOWS
FROM
HISTORY_TBL
WHERE
DATE >= TO_DATE ('12312014','MMDDYYYY')
GROUP BY
DATE
ORDER BY
DATE
You can truncate a date to a given format (day, year, month, etc.), as shown here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084
SELECT TRUNC(DATE,'MM'), SUM(NET_FLOWS/1000000.00) AS YTD_NET_FLOWS
FROM HISTORY_TBL
WHERE DATE >= TO_DATE ('12312014','MMDDYYYY')
GROUP BY TRUNC(DATE,'MM')
ORDER BY DATE