如何拉多个月

时间:2016-04-27 20:31:14

标签: sql-server select

这是我到目前为止所做的。它仅显示4月,但我需要几个月从2015年11月15日到2016年3月31日

SELECT DATENAME(MM,GETDATE()) AS MONTH, COUNT(*) AS TOTAL
  FROM S_SRV_REQ WITH (NOLOCK)          
  WHERE (dbo.fn_dstoffset(CREATED) >= '11-15-2015')
    AND (dbo.fn_dstoffset(CREATED) <= DATEADD(D, 1, '3-31-2016'))
    AND (INS_PRODUCT IN ('PS'))
    AND [SR_AREA] IS NOT NULL
    AND (SR_AREA IN ('Clinical Question'))

这是我得到的

MONTH         TOTAL

April          28381

1 个答案:

答案 0 :(得分:0)

我想你想在每个月的“创造”中得到点数 首先,我不确定你的查询是在MySQL还是MSSQL?
1.在My SQL中,将DATAADD更改为DATE_ADD
2.在我的SQL中将MONTHNAME而不是DATENAME

在MSSQL中会是这样的:

SELECT DATENAME(month, dbo.fn_dstoffset(CREATED)) AS MONTH, COUNT(*) AS TOTAL 
FROM S_SRV_REQ WITH (NOLOCK)
    WHERE (dbo.fn_dstoffset(CREATED) >= '11-15-2015') 
    AND (dbo.fn_dstoffset(CREATED) <= DATEADD(D, 1, '3-31-2016')) 
    AND (INS_PRODUCT IN ('PS'))
    AND [SR_AREA] IS NOT NULL AND (SR_AREA IN ('Clinical Question'))
    GROUP BY DATENAME(month, dbo.fn_dstoffset(CREATED))

在MySQL中将是这样的:

    SELECT MONTHNAME(STR_TO_DATE(month(dbo.fn_dstoffset(CREATED)), '%m')) AS MONTH, 
COUNT(*) AS TOTAL 
    FROM S_SRV_REQ WITH (NOLOCK)
        WHERE (dbo.fn_dstoffset(CREATED) >= '11-15-2015') 
        AND (dbo.fn_dstoffset(CREATED) <= DATE_ADD('3-31-2016',INTERVAL 1 DAY)
        AND (INS_PRODUCT IN ('PS')) 
        AND [SR_AREA] IS NOT NULL AND (SR_AREA IN ('Clinical Question'))
        GROUP BY MONTHNAME(STR_TO_DATE(month(dbo.fn_dstoffset(CREATED)), '%m'))