Refering to a column name that is a number - sql

my problem is: I've got a table with several columns which names are numbers (e.g. i've got a column named 1. How can I refer to these columns?

put square brackets round them like this [1], [2]
you can also do this if you have columns named after SQL keywords

You can put square braces or double quotation marks around the column names.
select [1], "2" from YourTable

Related

Bracket requirement on MDX

Why are brackets around the COUNT required in the below?
SELECT
Measures.[Count] on COLUMNS,
(
Conference.Conference.members,
{Division.Division.&North, Division.Division.&South}
)
on ROWS
FROM Avails
The query works removing the brackets on every word except for the .[Count] in the above. I thought brackets were only required if there's a space in the name?
They are needed cause count is a function. Try renaming it to count123 and use it without []

How to convert column names to all uppercase in Google Bigquery? (SQL) (Images included)

I want to clarify that I am not looking to covert the data within a column to upper case, rather I would like the column names to be in upper case. I know I can do this manually by selecting every column and setting it as uppercase, but I have a lot of columns and want to scale it across my entire table. See image below. I want those columns to be in upper case characters
You can build that line with this:
SELECT
string_agg(concat(column_name,' as ', upper(column_name)), ',') as column_string
FROM
`bigquery-public-data`.census_bureau_usa.INFORMATION_SCHEMA.COLUMNS
WHERE
table_name="population_by_zip_2010"
this returns
geo_id as GEO_ID,zipcode as ZIPCODE,population as POPULATION,minimum_age as MINIMUM_AGE,maximum_age as MAXIMUM_AGE,gender as GENDER

Is it possible to use column names with spaces in NamedAgg expressions?

Is it possible to create names with spaces and special characters when naming an aggregation column using pandas.NamedAgg aggregation function? The typical syntax would be:
pvt = (df.groupby(by=[....])
.agg(value=pd.NamedAgg(column='col', aggfunc='count'))
)
But is there a way to create a column name that is not a valid python variable name (as value in this example), but something like 'my new column name'?
The only solution that I can think of now is to rename value afterwards:
.rename(columns={'value': 'my new column name'})
If your column name does not make a valid Python variable name, you can use dictionary unpacking:
df.groupby(...).agg(**{
'My New Column Name': ('col_name', 'mean'),
'Column 42##$': ('col_name', 'max')
})

Combine several columns in one with Teradata

I have 10 columns and their values could be either null, or a name of a fruit.
I would like to add another column with all the fruits that every row has. I have used Concat(column1 , column2,..., column10) as name.
Issue : There are no commas coming on the result and if I add the comma before concatenating, we are having them together, the last word is also a comma.
Any ideas?
Thanks!
You can use the standard concatenation (||) in conjunciton with COALESCE function, which returns the value of the first non-null argument.
Example:
select coalesce(column1||',', '')||coalesce(column2||',', '')|| ... ||coalesce(column10||, '');

Accessing a column named Cast in Bigquery

I have a table and one of the columns is named CAST. How can I access this column. I've tried
Select [Cast] AS cast_s FROM tablename without success, Can I use this name or must I reimport all my data into bigquery?
I know that cast is a function. This is the error message:
Error:
Encountered " "CAST" "Cast "" at line 10, column 63. Was expecting: < E O F > (EOF has no spaces, markdown makes it disappear)
Thanks.
The lexical rules for BQ use backticks for this purpose:
select `cast` as cast_s
from tablename;
The documentation is here.
For BigQuery Legacy SQL you CAN use square brackets
SELECT [cast] as cast_s
FROM tablename
From documentation
You can use square brackets to escape reserved words so that you can
use them as field name and aliases. For example, if you have a column
named "prefix", which is a reserved word in BigQuery syntax, the
queries referencing that field will fail with obscure error messages
unless you escape it with square brackets:
SELECT [prefix] FROM ...