我正在使用SQL Server 2008 R2。目前,我有一个如下所示的表:
Text DateLogged SomeID SomeOtherID
-----------------------------------------------
1234: Data 04/04/2014 1 190
3212: Text 04/04/2014 1 190
4332: Data 04/04/2014 1 190
1256: Data 04/04/2014 1 190
以及以下SQL查询:
SELECT RIGHT(Text, LEN(Text) - CHARINDEX(':', Text) - 1) AS Sub, DateLogged, SomeID, SomeOtherID
FROM Example
WHERE SomeOtherID = 190
我想获得使用SSRS构建的报告具有相同子字符串(Sub)的行数。但是,我似乎无法找到方法通过substring对查询结果进行分组(FYI,DateLogged和SomeID字段用于在报告中进行分组)。该报告如下所示:
Month SomeID Sub Count
---------------------------------
April
1
Data 3
Text 1
任何解决方案,无论是在查询级别还是报告级别,都将非常感谢!
答案 0 :(得分:2)
这会计算子字符串:
SELECT RIGHT(Text, LEN(Text) - CHARINDEX(':', Text) - 1) AS Sub, count(*)
FROM Example
WHERE SomeOtherID = 190
GROUP BY RIGHT(Text, LEN(Text) - CHARINDEX(':', Text) - 1);
如果您想要日期和其他ID:
SELECT datepart(month, datelogged) as mon,
RIGHT(Text, LEN(Text) - CHARINDEX(':', Text) - 1) AS Sub,
someotherid, count(*)
FROM Example
WHERE SomeOtherID = 190
GROUP BY datepart(month, datelogged) as mon,
RIGHT(Text, LEN(Text) - CHARINDEX(':', Text) - 1),
SomeOtherId;