格式化SQL语句的标头

时间:2016-11-05 20:23:38

标签: c# sql csv datatable

我在CSV文件中有一个棒球统计表,我查询为

SELECT 
    birthState, COUNT(playerId) 
FROM 
    [Master.csv] 
GROUP BY 
    birthState 
ORDER BY 
    COUNT(birthState) DESC

结果显示为,

birthState  Expr1001
--------------------
   CA       2160
   PA       1417
   NY       1207
   IL       1054

我有一些(不完美的)逻辑来迭代结果DataTable中的列名(我使用C#)来填充上面的标题。

有没有办法将第二列的标题表示为COUNT(playerId)而不是Expr1001

感谢。

1 个答案:

答案 0 :(得分:4)

如果使用“as”(或别名)命令,则会返回带有指定标题的计算列:

SELECT birthState, COUNT(playerId) as NumberOfPlayers
FROM [Master.csv] GROUP BY birthState ORDER BY COUNT(birthState) DESC

我不确定你是否可以用这种方式标题标题“Count(playerid)”,但在名称周围使用方括号:

SELECT birthState, COUNT(playerId) as [Count(playerid)]
FROM [Master.csv] GROUP BY birthState ORDER BY COUNT(birthState) DESC

应该将文本视为文本而不是命令。