I am not well advanced in SQL. Maybe someone could help me with this little problem.
I need to summarize data by year end. I know I can use
SELECT Year(Mydate) as year,
Sum(Amount) as amount
FROM table1
GROUP BY Year(Mydate)
Mydate here is full date column (i.e. 15/6/2020). However in this case I get year as year number. I need to get year as year end date, i.e. 12/31/2021. How can I do that?
Try format your field using DATE_FORMAT: (DATE_FORMAT(Mydate, '%Y/12/31')
https://www.w3schools.com/sql/func_mysql_date_format.asp
Try:
SELECT
Year(Mydate) as year,
Convert(datetime, Str(Year(Mydate)) + ' Dec 31', 103) as eoyDec31a
Convert(char(10), Convert(datetime, Str(Year(Mydate)) + ' Dec 31', 103), 103) As eoyDec31b,
Max(Mydate) As eoyMax,
Sum(Amount) as amount
FROM table1
GROUP BY
Year(Mydate),
Convert(datetime, Str(Year(Mydate)) + ' Dec 31', 103),
Convert(char(10), Convert(datetime, Str(Year(Mydate)) + ' Dec 31', 103), 103)
Order By 1
If Dec 31 occurs in the table for every year, just the eoyMax column will do. If not the end of year has to be created from the parts: 2012 dec 31 etc.
eoyDec31a is the american format and eoyDec31b is the date string in british format.
The Convert function converts from string to datetime and back.
OK, this seems to solve my issue:
SELECT DATEFROMPARTS(Year(Mydate),12,31) AS year,
Sum(Amount) as amount
FROM table1
GROUP BY DATEFROMPARTS(Year(Mydate),12,31)
Related
I have a log table PRTL_UserAccessLog which has columns userID,datetime. I need to get the weekly distinct counts of logged users between two custom dates like the following
from date:01 Dec 2017
todate:31 dec 2017
My week start date should be from sunday.
I have created the following query to get the result
SET DATEFIRST 7
SELECT DISTINCT
'week '+ CAST(DATEPART(WEEK, Datetime)AS NVARCHAR(10)) AS weeknumber,
--I need to get the distinct count of users within this week as weekloggedcount
FROM
dbo.PRTL_UserAccessLog
WHERE
Datetime > '2017-12-01' AND Datetime < '2017-12-31'
AND usertypeid=1
ORDER BY
weeknumber
The result should be like this:
**Weeknumber** **weeklogcount**
Week48 10
Week49 50
You can try this.
SET DATEFIRST 7
SELECT DISTINCT
'week '+ CAST(DATEPART(WEEK, Datetime)AS NVARCHAR(10)) AS weeknumber,
COUNT(DISTINCT userID ) weeklogcount
FROM
dbo.PRTL_UserAccessLog
WHERE
Datetime > '2017-12-01' AND Datetime < '2017-12-31'
AND usertypeid=1
GROUP BY 'week '+ CAST(DATEPART(WEEK, Datetime)AS NVARCHAR(10))
ORDER BY weeknumber
Hi please let me know how to extract the last day of Financial year in sql server.my financial year start from 2016-04-01 to 2017-03-31
Closest you can use is End Of Month for that you need to provide one date to that month as below:
select eomonth('2017-03-01')
To get the last day of the financial year for any date, you need to find the last of march if before march, or the last of march next year if after march:
declare #yourdate datetime = getdate();
select case when month(#yourdate) < 4 then CONVERT(datetime,cast(YEAR(#yourdate) as char(4)) + '-03-31' ,120)
else CONVERT(datetime,cast(YEAR(#yourdate) + 1 as char(4)) + '-03-31' ,120)
end as financial_year_end
Edit:
If you want last date derived based on from_date, then use something like this
Rextester Demo
select
case when datepart(mm,from_date) <=3 then
cast(concat(year(from_date),'-03-31') as datetime)
else
dateadd(year,1,cast(concat(year(from_date),'-03-31') as datetime))
end as last_date_fin
from
(select '2017-04-30' as from_date union all
select '2017-01-13') t;
This way from_date between Jan - Mar will give same year's 31st march. Else it will give next year's 31st March.
Previous answer:
http://rextester.com/AXVM26769
If you want to get last day of march for same year as passed, then use
select cast(concat(given_year,'-03-31') as datetime)
from
(select '2017' as given_year) t
If you want to pass 2016 and then get 2017-03-31 then use. You can change the year in derived table and change the output based on that.
select dateadd(year,1,cast(concat(given_year,'-03-31') as datetime))
from
(select '2016' as given_year) t;
This Code will work to find the last date of Financial Year.
For Previous Year case matches and 'THEN' part will Execute and for current year 'ELSE'
part will execute.
select CASE WHEN (MONTH(GETDATE())) <= 3
THEN convert(varchar(4), YEAR(GETDATE())-1) + '-' + '03-31'
ELSE convert(varchar(4),YEAR(GETDATE()))+ '-' + '03-31'
end
> LastDayOfYearFY] =
> eomonth( dateadd(month, 5,
> dateadd(year, datepart(year, (dateadd(month, 6, [date])) ) -1900, 0)))
Idea extension taken from return-first-day-of-financial-year
You can select all the dates order them descendant and take the first one.
SELECT date
FROM table
ORDER BY date desc
LIMIT 1;
I have made a view on Microsoft sql server for sum by week this is the sql code
SELECT TOP (100) PERCENT CONVERT(varchar, DATEPART(ww, D_Date), 101) AS ReportingWeek, Proj_name, SUM(Expr1) AS total
FROM dbo.View_Test_Two
GROUP BY CONVERT(varchar, DATEPART(ww, D_Date), 101), Proj_name
ORDER BY ReportingWeek
every thing is just okay , but my problem is with the week names
27 data 330736
28 data 117868
29 data 471472
31 data 246468
now i need to make instead of 27,28,29,31 something like week 1 , week 2 , week 3...etc
..
thank you in advance .
Does this do what you want?
SELECT 'week ' + CONVERT(varchar(255), DATEPART(ww, D_Date) - 26) AS ReportingWeek,
Proj_name, SUM(Expr1) AS total
FROM dbo.View_Test_Two
GROUP BY DATEPART(ww, D_Date), Proj_name
ORDER BY min(D_Date);
I am trying to group the number of hours that employees worked for the last 4 weeks but I want to group them on a weekly basis. For example:
WEEK HOURS
Feb 24 to March 2 55
March 3 to March 9 40
March 10 to March 16 48
March 17 to March 23 37
This is what I have so far, please help. thanks
SET DATEFIRST 1
SELECT CAST(MIN( [DT]) AS VARCHAR(20))+' TO '+CAST (MAX([DT]) AS VARCHAR(20)) AS DATE,
SUM(HOURS) AS NUM_HRS
FROM MyTable
GROUP BY DATEPART(WEEK,[DT])
HAVING COUNT(DISTINCT[DT])=7
Create a Calendar auxilliary table, with Year, Month, Week, Date columns (you can also add holidays and other interesting stuff to it, it has many potential uses) and populate it for the period of interest.
After that, it's as easy as this:
SELECT sum(hours), cast(min(date) as varchar), cast(max(date) as varchar)
FROM Calendar c
LEFT OUTER JOIN MyTable h on h.Date = c.date
GROUP BY year, week
ORDER BY year, week
SET DATEFIRST 1
SELECT DATEPART(WEEK,DT) AS WEEK,
SUM(HOURS) AS NUM_HRS
FROM MyTable
WHERE DT >= DATEADD(WEEK, -4, GetDate()),
GROUP BY DATEPART(WEEK,[DT])
Try something like
SELECT
DATEADD(DD,
CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7,
'1/1/1900') [WeekBeginDate],
DATEADD(DD,
(CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7) + 6,
'1/1/1900') [WeekEndDate],
SUM(HOURS) AS NUM_HRS
FROM MyTable t
GROUP BY CONVERT(INT, DATEDIFF(DD, '1/1/1900', t.DT)/7)
Though this is the brute force trick, I think in your case it will work.
EDIT : Modified the query a little bit, the error was caused because of the order in which DATEDIFF calculates the difference.
Also here is a SQL FIDDLE with a working example.
EDIT 2 : Updated the Fiddle with the Date Format. To customize the date format, this article would help.
Here I want to check whether date from database lies between financial year or not.
I am using following query to check date but it is working properly for year only. if i want to check according to month and year then i got wrong result.
Here is my query:
SELECT *
FROM Payments INNER JOIN Subsciber ON Subsciber.SubId = Payments.SubId
WHERE DATEPART(YEAR, Payments.SaveOn) BETWEEN 2010 AND 2011
AND DATEPART(MONTH, Payments.SaveOn) BETWEEN 4 AND 3
payments.saveon >= CONVERT(DATETIME, '20100401', 112) AND
payments.saveon < CONVERT(DATETIME, '20110401', 112)