insomniac insomniac - 27 days ago 7
SQL Question

Formatting headers for SQL statements

I have a table of baseball stats in a CSV file which I query as,

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


The result is displayed as,

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


I have some (imperfect) logic to iterate over the column names in the resulting
DataTable
(I'm using C#) to fill in the headers above.

Is there a way to represent the header of the second column as
COUNT(playerId)
instead of
Expr1001
?

Thanks.

Answer

If you use the "as" (or alias) command this will return the computed column with a specified header:

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

I'm not sure if you can label the header "Count(playerid)" this way, but using square brackets around the name:

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

should treat the text as just text rather than a command.

Comments