我试图根据trackid显示记录表的计数。
我的ToTransaction专栏中的一些条目非常相似:Toshi-A,Toshi-B,Toshi-C,Tosan,Toki,Toto
我在查询中要做的是在一行中显示所有Toshi,同时仍然给Tosan,Toki和Toto他们自己的行。
Route ToTransaction Count
F43 Toshi 100
F43 Tosan 200
F43 Toki 75
F43 Toto 125
而不是
Route ToTransaction Count
F43 Toshi-A 35
F43 Toshi-B 25
F43 Toshi-C 22
F43 Toshi-D 18
F43 Tosan 200
F43 Toki 75
F43 Toto 125
SELECT Route, ToTransaction, count(TrackID) as 'Count' from TestDB
group by Route, ToTransaction
答案 0 :(得分:1)
试试这个:
SELECT IF(SUBSTR(ToTransaction,1,5)="Toshi","Toshi",ToTransaction) as "Trans",
COUNT(TracID) as "Count" from TestDB
GROUP BY Trans;
答案 1 :(得分:0)
如果你总是使用“ - ”作为分隔符并且“ - ”字符之前的字符数是动态的,你可以使用substring_index:
对于MySQL:
SELECT substring_index(ToTransaction,'-',1) as "Trans",
COUNT(TracID) as "Count" from TestDB
GROUP BY 1;
对于MSSQL(我无法尝试,但它应该工作):
SELECT CASE WHEN CHARINDEX('-',ToTransaction) > 1
THEN LEFT(ToTransaction,CHARINDEX('-',ToTransaction)-1)
ELSE ToTransaction
END as "Trans",
COUNT(TracID) as "Count" from TestDB GROUP BY Trans;