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);
Related
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)
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
I have a database with for example (ID,Datetime) now i want to have the Count of IDs in one specific Calendar Week, but explicit for all single Days in this calendar week.
So for example if there are 1000 entries this week, it should be separated by:
Monday 300
Tuesday 200
Wednesday 150
....
Is that possible?
My query at the moment is:
SELECT COUNT(*) AS Count
FROM Abfragen
WHERE (DATEPART(week, ErstelltAm) = 21) AND (DATEPART(year, ErstelltAm) = 2014)
Try something like this:
SELECT COUNT(*) AS [Count],
DATENAME(DW, ErstelltAm) AS [Day]
FROM Abfragen
WHERE (DATEPART(week, ErstelltAm) = 21) AND (DATEPART(year, ErstelltAm) = 2014)
GROUP BY DATENAME(DW, ErstelltAm)
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.
I'm trying to write a Microsoft SQL Server 2005 query that counts a total value, grouped by date with a cut-off point of 10:00am ?
eg: Table Orders
DateReceived Total
01-01-2012 06:10:01 2
01-01-2012 08:10:01 2
01-01-2012 10:10:01 4
02-01-2012 08:00:07 4
02-01-2012 10:00:07 4
I'd like to count the daily total, using 10:00 am as the cut-off point, so any orders before 10:00am appear in the total for the day before, and after 10:00 am in the total for that day.
I'm hoping to see query results like:
DateReceived Total
31-12-2011 4
01-01-2012 8
02-01-2012 4
I know how to group by just the date in Microsoft SQL Server:
SELECT DISTINCT CONVERT(varchar, [DateReceived], 111) AS [dt_DateReceived],
SUM([Total]) AS perday
FROM [Orders]
GROUP BY CONVERT(varchar, [DateReceived], 111)
ORDER BY [DateReceived] DESC
However I am unsure how to add a cut off time of 10:00am using Microsoft SQL Server.
Using MySQL, I can achieve this by grouping on a subtracted interval, however am unsure how to translate this to SQL Server:
GROUP BY
DATE(DATE_SUB( DateReceived , INTERVAL 10 HOUR))
Could anyone advise?
Thank you,
Jack
See the translation:
SELECT
CONVERT(varchar, DATEADD(hour, -10, [DateReceived]), 111) AS [dt_DateReceived],
SUM([Total]) AS perday
FROM [Orders]
GROUP BY CONVERT(varchar, DATEADD(hour, -10, [DateReceived]), 111)
ORDER BY 1 DESC
Test script
Note that my local datetime format is yyyy-mm-dd
;WITH Orders AS (
SELECT * FROM (VALUES
(CAST('2012-01-01 06:10:01' AS DATETIME), 2)
, ('2012-01-01 08:10:01', 2)
, ('2012-01-01 10:10:01', 4)
, ('2012-01-02 08:00:07', 4)
, ('2012-01-02 10:00:07', 4)
) AS Orders (DateReceived, Total)
)
SELECT CONVERT(varchar, DATEADD(hour, -10, [DateReceived]), 111) AS [dt_DateReceived]
, SUM([Total]) AS perday
FROM [Orders]
GROUP BY
CONVERT(varchar, DATEADD(hour, -10, [DateReceived]), 111)
ORDER BY
1
The testscript can be executed here
PS: Distinction is not needed