选择datepart年,然后按datepart分组

时间:2018-11-04 20:03:15

标签: sql-server

我做到了这一点并且工作正常,但是现在我想要如何显示月份和年份,例如01 2018,

ALTER PROCEDURE [dbo].[SaleChart]  
    (@FromYear NVARCHAR(50),
     @ToYear NVARCHAR(50))
AS
BEGIN
    SELECT 
        SUM ([TotalQty]) AS 'Total',
        DATEPART (yyyy, [Date]) AS 'Year'
    FROM
        [dbo].[SalesMainTable]
    GROUP BY 
        DATEPART (yyyy, [Date])
    HAVING 
        DATEPART (yyyy, [Date]) >= @FromYear 
        AND DATEPART (yyyy, [Date]) <= @ToYear
END

4 个答案:

答案 0 :(得分:1)

这是您要寻找的吗?

请注意having,仅在对汇总值进行过滤时才需要。否则请使用where

ALTER PROCEDURE [dbo].[SaleChart]
(
  @FromYear NVARCHAR(50)
  , @ToYear NVARCHAR(50)
)
AS
BEGIN
  SELECT 
    DATEPART(year, [Date]) AS 'Year'
    , DATEPART(month, [Date]) AS 'Month'
    , SUM ([TotalQty]) AS 'Total'
  FROM [dbo].[SalesMainTable]
  WHERE DATEPART(year, [Date]) >= @FromYear AND DATEPART(year, [Date]) <= @ToYear
  GROUP BY DATEPART(year, [Date]), DATEPART(month, [Date])
END

编辑:如果您也希望按月份进行过滤,我会按如下所示将其作为日期传递:

ALTER PROCEDURE [dbo].[SaleChart]
(
  @FromDate date
  , @ToDate date
)
AS
BEGIN
  SELECT 
    DATEPART(year, [Date]) AS 'Year'
    , DATEPART(month, [Date]) AS 'Month'
    , SUM ([TotalQty]) AS 'Total'
  FROM [dbo].[SalesMainTable]
  WHERE [Date] >= @FromDate and [Date] <= @ToDate
  GROUP BY DATEPART(year, [Date]), DATEPART(month, [Date])
END

答案 1 :(得分:0)

添加选择并按月和日分组

library(Hmisc)
Hmisc::cut2(x, breaks, formatfun = scales::percent)
# [1] [0%,25%)   [25%,50%)  [50%,75%)  [50%,75%)  [75%,100%]
# Levels: [0%,25%) [25%,50%) [50%,75%) [75%,100%]

答案 2 :(得分:0)

这是正确的

更改程序[dbo]。[SaleChartnew] (@FromYear NVARCHAR(50),@ToYear NVARCHAR(50)) 始于

DECLARE 
    @YearFrom   INT = RIGHT(@FromYear, 4)
,   @YearTo     INT = RIGHT(@ToYear, 4)
,   @MonthFrom  INT = LEFT(@FromYear, CHARINDEX('/',@FromYear) - 1)
,   @MonthTo    INT = LEFT(@ToYear, CHARINDEX('/',@ToYear) - 1)
,   @StartDate  DATE
,   @EndDate    DATE 


SET @StartDate = DATEFROMPARTS(@YearFrom, @MonthFrom, 1)
SET @EndDate   = DATEFROMPARTS(@YearTo, @MonthTo + 1, 1)
SET @EndDate   = DATEADD(DAY, -1, @EndDate)


SELECT 
    SUM([TotalQty]) AS 'Total'
 ,  YEAR([Date])    AS 'Year'
 ,  MONTH([Date])   AS 'Month'
FROM 
    [dbo].[SalesMainTable]

    where
 [Date] >= @StartDate
AND [Date] <= @EndDate

GROUP BY 
   Datepart (YEAR,[Date]),DATEPART (MONTH,[Date])
,   MONTH([Date])   

END

答案 3 :(得分:-1)

我认为您正在寻找这样的东西:

ALTER PROCEDURE [dbo].[SaleChart]
( @FromYear NVARCHAR(50), @ToYear NVARCHAR(50) ) 
AS BEGIN

    DECLARE 
        @YearFrom   INT = RIGHT(@FromYear, 4)
    ,   @YearTo     INT = RIGHT(@ToYear, 4)
    ,   @MonthFrom  INT = LEFT(@FromYear, CHARINDEX('/',@FromYear) - 1)
    ,   @MonthTo    INT = LEFT(@ToYear, CHARINDEX('/',@ToYear) - 1)
    ,   @StartDate  DATE
    ,   @EndDate    DATE 


    SET @StartDate = DATEFROMPARTS(@YearFrom, @MonthFrom, 1)
    SET @EndDate   = DATEFROMPARTS(@YearTo, @MonthTo + 1, 1)
    SET @EndDate   = DATEADD(DAY, -1, @EndDate)


    SELECT 
        SUM([TotalQty]) AS 'Total'
     ,  YEAR([Date])    AS 'Year'
     ,  MONTH([Date])   AS 'Month'
    FROM 
        [dbo].[SalesMainTable]
    GROUP BY 
        YEAR([Date])
    ,   MONTH([Date])   
    HAVING 
        [Date] >= @StartDate
    AND [Date] <= @EndDate

END

两个参数当前使用的日期格式输入均为mm/yyyy。如果将来要更改日期格式,则需要分别调整它们的LEFT和RIGHT,并可能使用SUBSTRING。不知道为什么要使用这种日期输入方式,因为它将使事情变得复杂。我建议您使用SQL Server可以理解的标准日期格式之一,例如yyyy-mm-dd作为输入,并且在查询中,可以根据需要使用YEAR,MONTH,DAY或DATEPART函数对结果进行分组。