Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
select max(QTR) from A.Table;
---2020Q4
In A.Table QTR column is in 'YYYYQQ' format
An expected outcome to be: Addition of QTR
---2021Q1
(ie. 2021Q1 is the max QTR means: Expected Outcome: 2021Q2
2021Q2 is the max QTR means: Expected Outcome: 2021Q3
2021Q3 is the max QTR means: Expected Outcome: 2021Q4
2021Q4 is the max QTR means Expected Outcome: 2022Q1)
How to achieve it?
One approach is to convert the value to a date by adding the appropriate number of months to the first of the year. Then convert back to YYYYQ format:
select to_char(add_months(to_date(substr(yyyyq, 1, 4) || '-01-01', 'YYYY-MM-DD'), substr(yyyyq, -1) * 3), 'YYYY"Q"Q')
from (select '2021Q4' as yyyyq from dual)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Good Day,
I am trying to apply if or case statement in SQL.
Ex. The marketing plan extended to more stores after a certain date:
First phase starting from Aug 1: only 1 store
Second phase starting from Sept 1: 3 stores
Last phase starting from Oct 1: all stores
So I have everything set except for the WHERE clause where I need to include if or case statement with the following logic:
WHERE
1=1
AND
Pseudo code, something along this line:
IF DATE >=20220801 AND <=20220831, STORE IN (1)
ELIF DATE >=20220901 AND <=20220930, STORE IN (1, 2, 3)
ELIF DATE >= 20221001, all stores
If anyone can point me to the right direction I would much appreciate.
Simply combine your conditions with AND and OR:
... WHERE (DATE >= '01-AUG-22' AND DATE < '01-SEP-22' AND STORE IN (1))
OR (DATE >= '01-SEP-22' AND DATE < '01-OCT-22' AND STORE IN (1, 2, 3))
OR (DATE >= '01-OCT-22')
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to calculate the average of the date according to the number of days between Factor2 and Facort3 where the Factor1 Value is A
how can I do it, please ?
select avg(DATEDIFF(DAY,Factor2,Factor3)) from [TestingTable]
where Factor1='A'
I've taken the difference between Factor2 and Factor 3 where Factor1 is A, then Find the average of the column.
and the result must be "3"
it's in SQL :
1- DATEDIFF ( datepart , startdate , enddate )
2-AVG ( [ ALL | DISTINCT ] expression )
3- datepart : it could be Day Month, Year....etc
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
The question is to Produce a list of the latest movies by genres for the current month. How to find the current month from the Date???
You are looking for DATEPART()
Select
*
From YourTable
Where
datepart(month,ReleaseDate) = datepart(month,getdate())
and datepart(year,ReleaseDate) = datepart(year,getdate())
Order by Genre, ReleaseDate desc
You can try this:
SELECT SUBSTRING(TO_CHAR(now(),'YYYYMMDD'),5,2)
In which:
Format to date and you get only the month
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a field with sales_start_date( values like 2014-06-17 ,2015-07-23...)
I need to do calculation based on sales_start date..
I have to take the most recent month-end date(i have to force this value to 2016-07-31) and subtract the sale_start_date to get an integer count of the days elapsed. If number of days passed is less than 2 default the value 2
sales_start_date
2016-01-01
2016-07-30
Output
Calculated field
155
2
Can any one help me in writing case statement.
Your help is much appreciated.
Thank You,
Swathi.
You basically need to use DATEDIFF
SELECT DATEDIFF(day,<enter start date column here>,<most recent month-end date column here>) AS [Calculated field]
FROM <your table here>
SQL-server 2012 + you have the EOMONTH() to help you get to the end of the month more easily.
;WITH cteData AS (
SELECT CAST('2016-01-01' AS DATE) as Start_date
UNION ALL
SELECT '2016-07-30'
)
SELECT
EOMONTH(DATEADD(MONTH,-1,GETDATE())) as EndOfPreviousMonth
,DATEDIFF(DAY,Start_date,EOMONTH(DATEADD(MONTH,-1,GETDATE()))) + 1 as DaysDifferent
FROM
cteData
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I Need help to select a number of data per month, there are 4 retailers and I need to select a total of transactions per month and the total amount for that month showing each month separately in sql.
Extract the month from the date and group by it. Then you can use aggregate functions to sum and count
select month(date_column),
sum(amount) as total_amount,
count(*) as transaction_count
from your_table
group by month(date_column)