2016-04-27 34 views
0

這是我迄今爲止所做的。這隻能說明在四月份,但我需要幾個月開始從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 
+0

爲了獲得對StackOverflow的幫助,你需要更清楚你的問題是關於什麼的。目前它是一個隨機的SQL和一個結果集合,其他很少。如果人們無法弄清楚你要問什麼,這個問題很可能會被解決。 – AlBlue

回答

0

我想你想要得到的計數在每個月的日期「CREATED」 首先我不確定你的查詢是在MySQL還是MSSQL?
1.在我的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'))