문제

나는 상태 별 버그 수를 계산하는 버그 수를 계산하는 쿼리 (Bug Tracker.net에서 사용)를 가지고 있습니다. 그러나 쿼리는 주 번호를 반환합니다. 정말로 원하는 것은 주 첫 번째 날짜입니다

select datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date)))
       as [week], bg_status , st_name as [status], count(*) as [count] 
  from bugs inner join statuses on bg_status = st_id 
 group by datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))),
          bg_status, st_name
 order by [week], bg_status
.

주 번호를 얻는 부분은

입니다.
datepart(wk, DateAdd(day, 0, DateDiff(day, 0, bg_reported_date))) as [week]
.

이 출력을 반환합니다.

week        bg_status   status                                        count
----------- ----------- --------------------------------------------- ------
22          1           new                                           1
22          5           closed                                        32
.

그러나 매주 첫 번째 날짜, 예 : 01-01-2010, 08-01-2010 등, 등 08-01-2010 등을 말하는 것이 좋습니다.

질문은 SQL Server의 주 번호로"주 시작 날짜 "및"주 종료 날짜 "를 어떻게 얻을 수 있습니까? 일주일부터)

일주일로 날짜를 계산합니다 에서 날짜 계산 (C #

제공되는 날짜 첫 번째 날짜 < / a> (질문은 자바 스크립트를 요청합니다)

SQL Server에 대한 답변을 찾지 못했지만 SQL Server (중요한 경우 2010)

도움이 되었습니까?

해결책

올바른 방식으로 생각하는 경우 SO 1267126 문제에 적용 할 수 있습니다.

그룹에있는 버그가있는 버그가있는 각각은 그룹에있는 날짜가 같은 주에 맵핑됩니다. 따라서 정의로 각 버그 날짜는 동일한 시작의 시작으로 맵핑해야합니다. 따라서, 버그 보고서 날짜와 주 주수의 날짜 '계산에서 주어진 날짜의 시작'계산 및 둘 다 (겸손하게 끔찍한) 표현식으로 그룹화하고 탐색하는 답변으로 끝납니다.

SELECT DATEPART(wk, DATEADD(day, 0, DATEDIFF(d, 0, bg_reported_date))) [week],
       DATEADD(dd, -(DATEPART(dw, bg_reported_date)-1), bg_reported_date)
       AS [weekstart], bg_status, st_name AS [status], COUNT(*) AS [count] 
  FROM bugs INNER JOIN statuses ON bg_status = st_id 
 GROUP BY DATEPART(wk, DATEADD(day, 0, DATEDIFF(day, 0, bg_reported_date))),
       DATEADD(dd, -(DATEPART(dw, bg_reported_date)-1), bg_reported_date),
       bg_status, st_name
 ORDER BY [week], bg_status
.


bg_reported_date가 datetime (주석 참조)이기 때문에 주석을 찾기 전에 날짜로 캐스팅해야합니다 (일주일 번호 표현식은 캐스팅이 필요하지 않습니다). 요일 '요일 시작 시작 표현식은 캐스트가 필요하지 않습니다) :

SELECT DATEPART(wk, DATEADD(day, 0, DATEDIFF(d, 0, bg_reported_date))) [week],
       DATEADD(dd, -(DATEPART(dw, bg_reported_date)-1),
               CAST(bg_reported_date AS DATE)) AS [weekstart],
       bg_status, st_name AS [status], COUNT(*) AS [count] 
  FROM bugs INNER JOIN statuses ON bg_status = st_id 
 GROUP BY DATEPART(wk, DATEADD(day, 0, DATEDIFF(day, 0, bg_reported_date))),
       DATEADD(dd, -(DATEPART(dw, bg_reported_date)-1),
               CAST(bg_reported_date AS DATE),
       bg_status, st_name
 ORDER BY [week], bg_status
.

nb : 테스트되지 않은 코드!

다른 팁

이것은 매우 오래된 스레드라는 것을 알지 못합니다. 그러나 "주 번호가 주어진 일주일에 첫 번째 날짜를 얻으십시오. 정확히 내가하고 싶은 일이며 실제로 일할 수있는 실제 날짜가 없으므로 수락 된 대답은나를 위해 일하지 마라.나는 내가 후손을 위해 솔루션을 게시 할 것이라고 생각했다.다른 문화 설정이이를 해제 할 수 있으므로 사용하기 전에 테스트하십시오.

내 답변은 이어

에서 시작됩니다.

일주일과 1 년을 알고 그 해의 그 주에 대한 시작과 종료일을 원한다고 가정 해 봅시다.여기있는 것이 있습니다 :

--These 2 "declared" variables would be passed in somehow
declare @WeekNumber int = DATEPART(wk, GETDATE())
declare @ForYear int = YEAR(GETDATE())-1

--Since we don't have a raw date to work with, I figured I could just start with 
--Jan 1 of that year.  I'll store that date in a cte here, but if you are doing this
--in a stored proc or function, it would make much more sense to use another @variable
;with x as
(
    --this method works in SQL 2008:
    SELECT CONVERT(DateTime, ('1/1/' + CONVERT(varchar, @ForYear))) as Jan1ForSelectedYear
    --If you are using 2014 or higher, you can use this instead:
    --DATETIME2FROMPARTS(@ForYear, 1, 1, 0,0,0,0,0)
)
--Now that we have a date to work with, we'll just add the number of weeks to that date
--That will bring us to the right week number of the given year.
--Once we have THAT date, we can get the beginning and ending of that week
--Sorry to make you scroll, but I think this is easier to see what is going on this way
SELECT  CONVERT(varchar(50), DateAdd(wk, (@WeekNumber - 1), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, x.Jan1ForSelectedYear) - 6, x.Jan1ForSelectedYear))), 101) as FirstDayOfWeekXForSelectedYear,
        CONVERT(varchar(50), DateAdd(wk, (@WeekNumber - 1), (DATEADD(dd, @@DATEFIRST - DATEPART(dw, x.Jan1ForSelectedYear)    , x.Jan1ForSelectedYear))), 101) as LastDayOfWeekXForSelectedYear
FROM x
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top