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;
Related
I have a dataset with the structure below.
I want to calculate a monthly average of the views.
I attempted to calculate the yearly frequency with the following code and I believe it is correct
SELECT
EXTRACT (YEAR FROM TO_DATE("date",'Month YYYY') ) AS "year",
AVG("views")
FROM talks
GROUP BY EXTRACT (YEAR FROM TO_DATE("date",'Month YYYY') )
ORDER BY "year" DESC
When it comes to the monthly analysis I have the problem that there several records for the same month in a year and there several years with the same months (in reality the dataset has information for many years - this a reduced version).
How can I go to implement this?
If you want the average per month then just group by your current date field.
If you want the average per month regardless of year then you would have to extract the month part of the current date field and group by that.
But your date field now appears to be having string data type; it would be better to use proper date data type. Then your analysis would be much easier, more flexible, better performing.
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);
I have tried several times to figure this out but no luck.
I have one table that I am trying to query.
InvNo (primary key),
CustID,
InvAmt,
DatePD
I want to pull all of the customers that have paid at least one invoice for 3 consecutive fiscal years. Fiscal year for this example is Aug 1 to Jul 31 of the following year. Thie InvNo is unique but the custID can appear multiple times depending on how many invoices they have paid. Can anyone help me with this?
For output I need one record per custID and how many consecutive years that CustID has paid an invoice.
CustID 333 ConsecutiveYears 7
You can get the year by either subtracting 8 months or adding four months and extracting the year. Then, lag() can solve the problem:
select distinct custid
from (select t.*, lag(yyyy, 2) over (partition by custid order by yyyy) as prev_yyyy_2
from (select distinct custid, year(dateadd(month, -8, datepd)) as yyyy
from t
) t
) t
where prev_yyyy_2 = yyyy - 2;
The innermost subquery just gets pairs of customers and year. The lag() looks two rows behind. Three years are present if "2 rows behind" is exactly 2 years ago.
I'm currently running the following sql statement in JasperReports Server to bring back my data using derived tables.
Select count(createddate) as ModulesCreatedDuringPastWeek,
count(updateddate) as ModulesUpdatedDuringPastWeek,
createddate,
updateddate
from merchendisingmodule
group by merchendisingmodule.createddate, merchendisingmodule.updateddate
However when grouping my data, I am only able to do it in Year, quarter, month and day. However for my report I'm needing the data to be group weeks, and so I was wondering what I will need to add to my code to do this.
DATEADD(D,-DATEPART(weekday,createddate)+1,createddate)
I use this method to prevent issues around the year transitions (week 53 in first days of januari and also in the last days of december, will group days together that are 360 days apart).
I use the first day of the week, instead of week numbers. I can use these dates to group by.
Also this will ensure that every week is 7 days long, instead of the last week of the year being only 3 or 4 days long.
Btw, in this example the first day of the week is sunday.
If your dates include time, use:
CAST(FLOOR(CAST(createddate AS FLOAT)) AS DATETIME)
instead of createddate in the above SYNTAX
I have an interesting query to do and am trying to find the best way to do it. Basically I have an absence table in our personnel database this records the staff id and then a start date and end date for the absence. End date being null if not yet entered (not returned). I cannot change the design.
They would like a report by month on number of absences (12 month trend). With staff being off over the month change it obviously may be difficult to calculate.
e.g. Staff off 25/11/08 to 05/12/08 (dd/MM/yy) I would want the days in November to go into the November count and the ones in December in the December count.
I am currently thinking in order to count the number of days I need to separate the start and end date into a record for each day of the absence, assigning it to the month it is in. then group the data for reporting. As for the ones without an end date I would assume null is the current date as they are presently still absent.
What would be the best way to do this?
Any better ways?
Edit: This is SQL 2000 server currently. Hoping for an upgrade soon.
I have had a similar issue where there has been a table of start/end dates designed for data storage but not for reporting.
I sought out the "fastest executing" solution and found that it was to create a 2nd table with the monthly values in there. I populated it with the months from Jan 2000 to Jan 2070. I'm expecting it will suffice or that I get a large pay cheque in 2070 to come and update it...
DECLARE TABLE months (start DATETIME)
-- Populate with all month start dates that may ever be needed
-- And I would recommend indexing / primary keying by start
SELECT
months.start,
data.id,
SUM(CASE WHEN data.start < months.start
THEN DATEDIFF(DAY, months.start, data.end)
ELSE DATEDIFF(DAY, data.start, DATEADD(month, 1, months.start))
END) AS days
FROM
data
INNER JOIN
months
ON data.start < DATEADD(month, 1, months.start)
AND data.end > months.start
GROUP BY
months.start,
data.id
That join can be quite slow for various reasons, I'll search out another answer to another question to show why and how to optimise the join.
EDIT:
Here is another answer relating to overlapping date ranges and how to speed up the joins...
Query max number of simultaneous events