到目前为止我所拥有的内容..当我运行查询时,它将所有内容组合在一起..我希望它能够删除重复项但计算它们。
Electronic Payment RM Transfer to CCV2 2016-03-01 16:04:42.000 1
Electronic Payment RM Transfer to CCV2 2016-03-01 18:45:55.000 1
Electronic Payment RM Transfer to CCV2 2016-03-01 18:53:11.000 1
Electronic Payment RM Transfer to CCV2 2016-03-01 20:21:41.000 1
Electronic Payment RM Transfer to CCV2 2016-03-01 21:44:54.000 1
Electronic Payment RM Transfer to CCV2 2016-03-02 18:21:10.000 1
Electronic Payment RM Transfer to CCV2 2016-03-02 22:19:31.000 1
Electronic Payment RM Transfer to CCV2 2016-03-03 16:56:01.000 1
Electronic Payment RM Transfer to CCV2 2016-03-03 18:02:31.000 1
Electronic Payment RM Transfer to CCV2 2016-03-03 18:39:43.000 1
Electronic Payment RM Transfer to CCV2 2016-03-03 19:10:41.000
这是我到目前为止写的代码
SELECT SR_AREA, INS_PRODUCT, RESOLUTION_CD, CREATED AS DATEADD, COUNT(*) AS DAY
FROM S_SRV_REQ WITH (NOLOCK)
WHERE (dbo.fn_dstoffset(CREATED) >= '3-1-2016') AND (dbo.fn_dstoffset(CREATED) <= DATEADD(D, 1, '3-31-2016')) AND (INS_PRODUCT='RM')
GROUP BY INS_PRODUCT, RESOLUTION_CD, SR_AREA, CREATED
ORDER BY SR_AREA,INS_PRODUCT,RESOLUTION_CD DESC
答案 0 :(得分:0)
如果您想按日期分组,只需使用Created
上的DATE()函数即可
和DATE(Created)这样分组
SELECT SR_AREA, INS_PRODUCT, RESOLUTION_CD, DATE(CREATED) AS DATEADD, COUNT(*) AS DAY
FROM S_SRV_REQ WITH (NOLOCK)
WHERE (dbo.fn_dstoffset(CREATED) >= '3-1-2016') AND (dbo.fn_dstoffset(CREATED) <= DATEADD(D, 1, '3-31-2016')) AND (INS_PRODUCT='RM')
GROUP BY INS_PRODUCT, RESOLUTION_CD, SR_AREA, DATE(CREATED)
ORDER BY SR_AREA,INS_PRODUCT,RESOLUTION_CD DESC
这将返回(将,DATEADD ASC
添加到订单末尾)
Electronic Payment RM Transfer to CCV2 March, 01 2016 00:00:00 5
Electronic Payment RM Transfer to CCV2 March, 02 2016 00:00:00 2
Electronic Payment RM Transfer to CCV2 March, 03 2016 00:00:00 4
很奇怪,DATE()不适合你...也许尝试DATE_FORMAT
SELECT SR_AREA, INS_PRODUCT, RESOLUTION_CD, DATE_FORMAT(CREATED,'%Y-%m-%d') AS DATEADD, COUNT(*) AS DAY
FROM S_SRV_REQ WITH (NOLOCK)
WHERE (dbo.fn_dstoffset(CREATED) >= '3-1-2016') AND (dbo.fn_dstoffset(CREATED) <= DATEADD(D, 1, '3-31-2016')) AND (INS_PRODUCT='RM')
GROUP BY INS_PRODUCT, RESOLUTION_CD, SR_AREA, DATE_FORMAT(CREATED,'%Y-%m-%d')
ORDER BY SR_AREA,INS_PRODUCT,RESOLUTION_CD DESC, DATEADD ASC
更新 OP提到SQL Server而不是MySQL。 所以请改用
SELECT SR_AREA, INS_PRODUCT, RESOLUTION_CD, CONVERT(VARCHAR(19),CREATED,111) AS DATEADD, COUNT(*) AS DAY
FROM S_SRV_REQ WITH (NOLOCK)
WHERE (dbo.fn_dstoffset(CREATED) >= '3-1-2016') AND (dbo.fn_dstoffset(CREATED) <= DATEADD(D, 1, '3-31-2016')) AND (INS_PRODUCT='RM')
GROUP BY INS_PRODUCT, RESOLUTION_CD, SR_AREA, CONVERT(VARCHAR(19),CREATED,111)
ORDER BY SR_AREA,INS_PRODUCT,RESOLUTION_CD DESC, DATEADD ASC