我在下面的代码中运行良好,但我想将日期的形状更改为" 2017年3月1日"而不是" 2017-03-01"
SELECT
*
FROM
(
SELECT
[MessageType].[Name],
CASE WHEN ( Format([OccuredAtUtc], 'yyyy-MM-dd') LIKE '2017-03%' ) THEN Format([OccuredAtUtc], 'yyyy-MM-dd')
ELSE NULL
END AS [Time],
COUNT(*) AS [Count]
FROM @Table
INNER JOIN @Table ON
GROUP BY Format([OccuredAtUtc], 'yyyy-MM-dd'),
[MessageType].[Name]
) s
WHERE ( [Time] IS NOT NULL )
ORDER BY [Time] ASC
OUTUPUT:
NAME_______TIME______COUNT____
HTTP 2017-03-01 21
HTTP 2017-03-02 37
HTTP 2017-03-03 42
.
.
HTTP 2017-03-31 29
如果我尝试使用会发生什么 CONVERT(VARCHAR(11),[OccuredAtUtc],106) IN CODE:
//所有被改变的东西都是...... 2件事
SELECT
*
FROM
(
SELECT
[MessageType].[Name],
CASE WHEN ( Format([OccuredAtUtc], 'yyyy-MM-dd') LIKE '2017-03%' ) THEN CONVERT(VARCHAR(11), [OccuredAtUtc], 106) --Format([OccuredAtUtc], 'yyyy-MM-dd')
ELSE NULL
END AS [Time],
COUNT(*) AS [Count]
FROM @Table
INNER JOIN @Table ON
GROUP BY [OccuredAtUtc], --Format([OccuredAtUtc], 'yyyy-MM-dd')
[MessageType].[Name]
) s
WHERE ( [Time] IS NOT NULL )
ORDER BY [Time] ASC
我获得了这个非分组输出:
HTTP 01 Mar 2017 1
HTTP 01 Mar 2017 1
HTTP 01 Mar 2017 1
HTTP 01 Mar 2017 2
HTTP 01 Mar 2017 1
HTTP 01 Mar 2017 1
.
.
HTTP 02 Mar 2017 1
.
.
HTTP 31 Mar 2017 1
猜猜数据类型存在问题
或 GROUP BY [OccuredAtUtc] ...(最后5行) ...如果我使用整个" CONVERT(VARCHAR(11)将会跳错误),[OccuredAtUtc],106)"在GROUP BY中也是
这有什么简单的解决方案吗?我只需要格式化的日期输出" 2017年3月1日" ...
[OccuredAtUtc]中的原始日期:
2017-03-01 12:16:58.5080000
2017-03-01 18:11:53.3090000
2017-03-01 18:34:18.3090000
2017-03-01 20:42:28.8570000
2017-03-01 21:10:36.7070000
.
.
.
提前谢谢!!!
答案 0 :(得分:2)
您的查询似乎不必要地复杂化。我认为这样做你想要的:
SELECT [MessageType].[Name],
CONVERT(VARCHAR(11), [OccuredAtUtc], 106) AS [Time],
COUNT(*) AS [Count]
FROM @Table t INNER JOIN -- I assume these table names are not the real name
@Table t2
ON . . .
WHERE OccuredAtUtc >= '2017-03-01' AND
OccuredAtUtc < '2017-04-01'
GROUP BY (CONVERT(VARCHAR(11), [OccuredAtUtc], 106),
[MessageType].[Name]
注意:
FROM
条款过于简化;我假设表名不是@table
。where
子句在聚合之前过滤。这比在聚合后过滤更有效。答案 1 :(得分:0)
如果您使用SQL Server 2012+,则可以使用FORMAT
函数FORMAT(DateColumn,'dd MMM yyyy')