简体   繁体   English

按财政/财政年度划分的 SQL 组

[英]SQL group by fiscal/financial year

Quick question, using the code below I just want to create a field displaying the records fiscal year and then group the results by the year.快速提问,使用下面的代码我只想创建一个显示会计年度记录的字段,然后按年份对结果进行分组。

The code to retrieve the results works however it does not want to group them by the fiscal year.检索结果的代码有效,但它不想按财政年度对它们进行分组。 I just get errors.我只是收到错误。 I have tried multiple combinations using the forums but I can't get it to work.我使用论坛尝试了多种组合,但无法正常工作。 It is doing my head in!它正在做我的头!

This could be simple to resolve but I can't see it.这可能很容易解决,但我看不到。 Could anyone help?有人可以帮忙吗?

Thanks!谢谢!

Select m.jobno, m.premid, m.address,m.COMPDATE,

year(dateadd(month, -3, m.COMPDATE)) as FiscalYear


FROM miscvisit m

----- GROUP BY year(dateadd(month, -3, m.COMPDATE))

Try this:尝试这个:

Select m.jobno, m.premid, m.address, 

year(dateadd(month, -3, m.COMPDATE)) as FiscalYear, sum([SalesField]) as Sales


FROM miscvisit m

GROUP BY 
m.jobno, m.premid, m.address, 
year(dateadd(month, -3, m.COMPDATE))

Try the below query..试试下面的查询..

SELECT m.jobno
    ,m.premid
    ,m.address
    ,m.COMPDATE
    ,YEAR(DATEADD(MONTH, - 3, m.COMPDATE)) AS FiscalYear
FROM miscvisit m
GROUP BY m.jobno
    ,m.premid
    ,m.address
    ,m.COMPDATE
    ,YEAR(DATEADD(MONTH, - 3, m.COMPDATE))

Given that you seem to want to return the entire record, perhaps you really want an ORDER BY clause here:鉴于您似乎想要返回整个记录,也许您真的想要一个ORDER BY子句:

SELECT
    jobno,
    premid,
    address,
    COMPDATE,
    YEAR(DATEADD(month, -3, COMPDATE)) AS FiscalYear
FROM miscvisit
GROUP BY
    YEAR(DATEADD(month, -3, COMPDATE))
ORDER BY
    FiscalYear;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM