SQL Query - Previous 12 months Sum - Dynamically [duplicate] - sql

This question already has answers here:
SQL - Running Total - Year To Date, Previous Year To Date, and Last Rolling 12 Months
(1 answer)
SQL Server - Cumulative Sum over Last 12 Months, but starting from the Last Month (SQL Server 18)
(2 answers)
Closed 2 years ago.
I have a below table with me in which I have 2 years sales data with me and with multiple heads. I want to do running total of previous 12 months for all the heads. So for example if I am in Apr - 2019, my running total should give me sum of sales from May-2018 to Apr-2019 (12 months). And it should change dynamically as my month gets changed. The output should look something like below.
It would be very helpful for me if anyone can guide me on this. Please feel free to ask any questions regarding the requirement.

Related

Number of Days in a month HIVE [duplicate]

This question already has answers here:
Find last day of a month in Hive
(6 answers)
Closed 1 year ago.
Can someone let me know a simple way to get # of days in a month in HIVE SQL based on current_date.
e.g. 2021-02-16 = 28 days, 2021-06-30 = 30 days etc
Use
day(last_day(current_date))
last_day(date) returns last day date, day(date) returns day part of the date, int
See manual here: Language Manual - Date Functions

SQL Server - Select all records whose dates are within 5 days of target date [duplicate]

This question already has an answer here:
SQL Server Management Studio make default date 5 days from now
(1 answer)
Closed 4 years ago.
I need to create a view everyday with all records reaching the "Prazo" date ('Prazo' stands for due date in Portuguese), so everyday I need to make a select showing all the records that are 5 days from reaching the due date. How can I do that?
You can do:
Select *
from Table as t
Where DATEDIFF(DD, GETDATE(), PrazoDate) = 5
It just says how many records from today are 5 days from reaching their due date.

need sql query to get min and max experience?

I have a table with 2 columns (exp_Years, exp_months).
Saving years and months in 2 different columns.
Now I need a SQL Query to get min and max experience
If minExp=2years and maxExp=4years
how do I get records with out getting 4.1 and 4.2 exp?
example data:--
table: Resume(name, email, phone, exp_years, exp_months)
I need list of records with exp_years>2 and exp_years<5... but how to check exp_months here..
if I use exp_years>2 and exp_years< 5 condition am getting records with 5years 1 month, 5year 2 months are also getting.
I need only more than 2 years and less than or equals 5 years 0 months.. but not 5years 1month and moreee....
actually I need hibernate HQL Query....
Thanks in advance.
This is a strange way of storing the data. So you are supposed to store two years and three months as years = 2 and months = 3.
But you could just as well store years = 1 and months = 15 or years = 0 and months = 27, which would be the same time span. This said, it would be better to have just one column, i.e months only.
Then to get records with a time span of over 2 years and until 5 years exactly you'd select
where months between 25 and 60
If you must live with the table design you are showing, then you can always calculate total months = years * 12 + months. That would be
where years * 12 + months between 25 and 60
you can make this by add months to years after divide months on 12 then you can use ROUND to round to int value:
select ROUND(max(exp_Years+exp_months/12)), ROUND(min(exp_Years+exp_months/12))
from your_table

how to find the weekend dates from given list of dates [duplicate]

This question already has answers here:
Get day of week in SQL Server 2005/2008
(11 answers)
Closed 8 years ago.
I have a list of dates and other columns in which have to find the weekend dates among them and the the weekend dates should be there in the list of given dates
Output should be some thing like this...
Any help is appricated thanks.
use datepart (dw,..
to filter data needed

SQL last month date of given date [duplicate]

This question already has answers here:
Get the last day of the month in SQL
(22 answers)
Closed 9 years ago.
i am using sql
How to get the last day of the month for a given date
01/03/2014 should return 01/31/2014
I only need the date part.. Thanks
For SQL-SERVER, try this:
SELECT convert(varchar,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,'01/03/2014 ')+1,0))),101)