[英]Counting rows in SQL Server by substrings
I am using SQL Server 2008 R2. 我正在使用SQL Server 2008 R2。 Currently, I have a table that looks like the following:
目前,我有一个如下所示的表:
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
as well as the following SQL query: 以及以下SQL查询:
SELECT RIGHT(Text, LEN(Text) - CHARINDEX(':', Text) - 1) AS Sub, DateLogged, SomeID, SomeOtherID
FROM Example
WHERE SomeOtherID = 190
I would like to get the counts of rows with the same substring (Sub) for a report that I am building with SSRS. 我想获得使用SSRS构建的报告具有相同子字符串(Sub)的行数。 However, I can't seem to find a way to group the query results by substring (FYI, the DateLogged and SomeID fields are used for grouping in the report).
但是,我似乎找不到通过子字符串对查询结果进行分组的方法(FYI,DateLogged和SomeID字段用于在报告中进行分组)。 The report would look like the following:
该报告如下所示:
Month SomeID Sub Count
---------------------------------
April
1
Data 3
Text 1
Any solution, whether it is on the query level or the report level, would be greatly appreciated! 任何解决方案,无论是在查询级别还是报表级别,都将非常感谢!
This counts the substrings: 这会计算子字符串:
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);
If you want the date and other id: 如果你想要日期和其他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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.