Oracle: select max date per month per year from table - database

I have a table with a set of business dates I need to select the max date per month and year tried using the last_day function but that returns the last day not the max date of that month.please help me out.

MAX is an aggregate function, so you need to figure out how to group all of the days of the month together. The easiest way to do that is apply a function that will return the same value for every day in that month. LAST_DAY would work, but I prefer TRUNC (with 'MM' specified).
SELECT MAX(your_column) FROM your_table GROUP BY TRUNC(your_column, 'MM')

Related

How to extract day of week from timestamp

I am trying to extract the day of the week from a timestamp in SQL Server.
I am specifically looking for the SQL Server equivalent syntax to EXTRACT.
I want to count how many fields are in each day of the week.
This is how I would do it on BigQuery:
SELECT
EXTRACT(DAYOFWEEK FROM order_date ) as day,
count(*) count_trips
FROM `sales.orders`
group by EXTRACT (DAYOFWEEK FROM order_date)
Try this:
SELECT DATENAME(WEEKDAY, DATE(timestamp))
example:
SELECT DATENAME(WEEKDAY, '2022/05/08 18:50:30');
Output: Sunday
P.S.
I am helping you the day part only considering you know the rest of your code. Feel free to reply this for exact query.

I want to calculate Week-Ending date for particular dates given in a separate column

I am currently working on SQL Server 2019. There is need to calculate week-ending date (every Sunday) for a given effective date in a column. I am trying to make it work using the below logic.
dateadd(dd,((datediff(dd,'17530107',GETDATE()+13)/7)*7)+7,'17530107') as Weekend_Date
Desired Output,
Effective Date ( this date is give): Week-Ending Date (this needs to be calculated)(desired output)
10/08/2019 10/13/2019
11/01/2019 11/03/2019
11/07/2019 11/10/2019
If Getdate() is the field from your database and Sunday is day of week 1 then this should work.
Select dateadd (dd,(7- datepart(dw,getdate()) + 1),cast(getdate() as date))

Date search range previous month to current month

I had a working SQL date search...
(DATEADD (dd,-30, getdate()) <= pymnt_pstd_dt)
it gives me data from the past 30 days, but what I actually need is a comparison of the previous month MTD with the current month MTD
I am currently struggling with a good way to write that for SQL
Any help would be appreciated.
Here are WHERE statements that will get month to date and previous month to date for your field, pymnt_pstd_dt:
WHERE pymnt_pstd_dt BETWEEN DATEADD(month,DATEDIFF(MONTH,0,GETDATE()),0) AND GETDATE() --Month to date
WHERE pymnt_pstd_dt BETWEEN DATEADD(month,DATEDIFF(MONTH,0,GETDATE())-1,0) AND DATEADD(MONTH,-1,GETDATE()) --Previous month to date

TSQL Determine every other Friday from a "seed" date

Greetings StackOverflow Wizards.
SQL datetime calculations always give me trouble. I am trying to determine if an employee's hiredate fell between the last payday of that month and the first of the next month. (I.e. did they get a paycheck in their hire month.
Knowns:
I know our paydays are every other Friday.
I know 01/02/1970 was a Payday, and that date precedes the longest
active employee we have.
I know the hire date of each active employee (pulled from table).
I know (can calculate) the first of the month following the hire
date.
What I cannot seem to wrap my head around is how to use that seed date (01/02/1970) with datediff, dateadd, datepart, etc. to determine if there is a pay date between the hire date in question and the first of the following month.
In pseudo-code, here is what I'm trying to do:
declare #seedDate datetime = '01/02/1970' -- Friday - Payday seed date from which to calculate
declare #hireDate datetime = '09/26/2008' -- this date will actually be pulled from ServiceTotal table
declare #firstOfMonth datetime = DATEADD(MONTH, DATEDIFF(MONTH, 0, #hireDate) + 1, 0) -- the first of the month following #hireDate
declare #priorPayDate datetime -- calculate the friday payday immediately prior to #firstOfMonth
if #priorPayDate BETWEEN #hireDate AND #firstOfMonth
begin
-- do this
end
else
begin
-- do that
end
Using the hard-coded #hireDate above, and the #seedDate to determine every-other-Friday paydays, I know that there was a payday on 9/19/2008 and not another one until 10/03/2008, so the boolean above would be FALSE, and I will "do that" rather than "do this." How do I determine the value of #priorPayDate?
In all my databases where there is a lot going on with dates I create a table with colums for date,day, weekday,month,weeknr,dayof month, etc etc. I then use a procedural programming language or a bunch of handwritten sql to populate this table with every day for a large range of years say 1970 to 2200.
I pack this table 100% and index it heavily. You can then simply join any date to this table and do complex date stuff with simple where clause. So basically you pre calculate a helper table. maybe in you case you add a column to the date helper table with friday since seed column.
hope that makes sense.
Taking a DATEDIFF for days between your #seedDate and #firstOfMonth will give you a total number of days, which you can modulus by number of days between pay periods (14) to get number of days from the last pay period to the #firstOfMonth. You'll run into problems when the 1st is a payday (e.g. next month), which makes a CASE statement necessary:
DECLARE #priorPayDate DATETIME
SET #priorPayDate = CASE
WHEN DATEDIFF(dd, #seedDate, #firstOfMonth) % 14 = 0
THEN DATEADD(dd, -14, #firstOfMonth)
ELSE DATEADD(dd, -(DATEDIFF(dd, #seedDate, #firstOfMonth) % 14), #firstOfMonth)
END

Filtering columns based on a date in a field for current week, past weeks, and future weeks

Example Table:
(Column Heading) Total Interviews of everyone --- (Row Heading) Interview Requested
(Column Heading) Number of interviews this week -- (Row Value) Count all the dates scheduled this week that have the status: "Interview Requested" e.g 4
(Column Heading) Number of interviews next week -- (Row Value) Count all the dates scheduled next week that have the status: "Interview Requested" e.g 6
First of all i'll like to aploigise about my makeshift table, it seems that i cant use pictures since im a new member.
I am also new to using reportbuilder and sql and have been trying to figure out how i can count the amount of dates that fall between certain date ranges in a report.
Like ive stated in the makeshift field called "Number of interviews this week" I wish to count all the dates scheduled for the current week, the week after, the week before and so on based on a date field. I then plan to break this table down to show all the interviews a single person has schedueled.
The issue I am having is that there will be many dates spanning different months/days. So Im not sure how i can represent this with DateDiff or other date features, because the table is meant to be a live representation of the current interviews.
Is what i'm trying to do even possible using report builder?? If so any tips would be great, if not then thanks for taking the time to look at my question.
As Requested Dataset Fields:
[UserName] (User table), [InterviewStatus] (Interview table), [DateUpdated] (Interview Table)
Those are the main ones that will be used in the report
If you just want to count the number of columns of each field, you can use the sql statement COUNT() see COUNT().
You can do the common query like this:
SELECT COUNT(fieldName1), COUNT(fieldName2), ... , COUNT(fieldNamen) FROM <tableName>
Also try to show your codes so that your question will be more clarified.
You can use the generic select statement from your interview table filtering in the where clause for dates between the beginning of the current week and the end of the next.
Then the two fields would be a sum of 1 if the date falls within the current week or next.
EDIT:
Example below will give the number of dates within this week. Taken from another question:
Select InterviewStatus,
Sum(Case when DateUpdated >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
AND DateUpdated < CAST(CONVERT(VARCHAR(10), DATEADD(dd, (8 - DATEPART(dw, GETDATE())), GETDATE()), 120) AS DATETIME)
Then 1
Else 0 end)
as NumberInCurrentWeek
From InterviewTable
Where DateUpdated >= DATEADD(dd, -(DATEPART(dw, GETDATE()) -1), GETDATE())
AND DateUpdated < CAST(CONVERT(VARCHAR(10), DATEADD(dd, (8 - DATEPART(dw, GETDATE())), GETDATE()), 120) AS DATETIME)
Group by InterviewStatus
This just needs to be tweaked to your exact circumstance. For example alter the 8 by 7 will capture the current fortnight.
Then produce a simple table based on the query.

Resources