我需要按组合的年周字符串对 SQL Server 2003 上保存的记录进行计数和分组

发布于 2024-10-17 14:32:11 字数 661 浏览 2 评论 0原文

我被要求绘制 SQL Server 中保存的数据的图表。我可以使用我现有的工具来做到这一点。

但是,我被要求计算数据库中的记录数,根据年份的最后两位数字和周数的组合对它们进行分组,

例如 2011 年的第一周是 1101
例如,2010 年的第十五周将是 1015

(注意需要周为 2 个字符),因此按字母顺序索引 who

我已经尝试了各种组合,例如,

select CASE WHEN DATENAME(ww,j.requestedat) <= 9
            THEN (CAST('0' AS VARCHAR(1)) + CAST(DATENAME(ww,j.requestedat) AS VARCHAR(1)))
            ELSE CAST(DATENAME(ww,j.requestedat) AS VARCHAR(2))
         END AS WeekNumber
         , right(DateName(yy, j.requestedat),2) + WeekNumber as YYWW
from
    facts_reactive.dbo.jobs j 
order by j.requestedat

但都失败了。

任何帮助/指导都感激地接受。

I have been asked to graph data held in SQL Server. This I can do with the tools I have available.

However I have been asked to count the number of records in the database, grouping them based on a combination of last two digits of the Year, and the week number

For example the first week of 2011 would be 1101
example the fifteenth week of 2010 would be 1015

(note need week as 2 char) so index who alphabetically

I have tried various combinations such as

select CASE WHEN DATENAME(ww,j.requestedat) <= 9
            THEN (CAST('0' AS VARCHAR(1)) + CAST(DATENAME(ww,j.requestedat) AS VARCHAR(1)))
            ELSE CAST(DATENAME(ww,j.requestedat) AS VARCHAR(2))
         END AS WeekNumber
         , right(DateName(yy, j.requestedat),2) + WeekNumber as YYWW
from
    facts_reactive.dbo.jobs j 
order by j.requestedat

but all have failed.

Any help/guidance gratefully accepted.

Rob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过去的过去 2024-10-24 14:32:11

试试这个:

select
    right(DateName(yy, wn.requestedat),2) + wn.WeekNumber as YYWW
from
    (
        select CASE WHEN DATENAME(ww,j.requestedat) <= 9
                THEN (CAST('0' AS VARCHAR(1)) + CAST(DATENAME(ww,j.requestedat) AS VARCHAR(1)))
                ELSE CAST(DATENAME(ww,j.requestedat) AS VARCHAR(2))
             END as WeekNumber, j.requestedat
        from
            facts_reactive.dbo.jobs j      
    ) AS wn
order by wn.requestedat

try this one :

select
    right(DateName(yy, wn.requestedat),2) + wn.WeekNumber as YYWW
from
    (
        select CASE WHEN DATENAME(ww,j.requestedat) <= 9
                THEN (CAST('0' AS VARCHAR(1)) + CAST(DATENAME(ww,j.requestedat) AS VARCHAR(1)))
                ELSE CAST(DATENAME(ww,j.requestedat) AS VARCHAR(2))
             END as WeekNumber, j.requestedat
        from
            facts_reactive.dbo.jobs j      
    ) AS wn
order by wn.requestedat
红衣飘飘貌似仙 2024-10-24 14:32:11

试试这个:

SELECT RIGHT(YEAR(j.requestedat),2) + RIGHT('00'+CAST(DATEPART(ww,j.requestedat) AS VARCHAR(2)),2) YYWW
FROM facts_reactive.dbo.jobs j  
ORDER BY j.requestedat 

Try this:

SELECT RIGHT(YEAR(j.requestedat),2) + RIGHT('00'+CAST(DATEPART(ww,j.requestedat) AS VARCHAR(2)),2) YYWW
FROM facts_reactive.dbo.jobs j  
ORDER BY j.requestedat 
聊慰 2024-10-24 14:32:11
SELECT
  YYWW = RIGHT(YEAR(j.requestedat) * 100 + DATEPART(ww, j.requestedat), 4)
FROM facts_reactive.dbo.jobs j
ORDER BY j.requestedat
SELECT
  YYWW = RIGHT(YEAR(j.requestedat) * 100 + DATEPART(ww, j.requestedat), 4)
FROM facts_reactive.dbo.jobs j
ORDER BY j.requestedat
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文