postgres: apply aggregate function to column with spaces in name - postgresql

I am trying to run a query like this
SELECT group, sum("a column with spaces") FROM x GROUP BY group
Unfortunately, I get the error function sum(text) does not exist. It appears to be interpreting my quoted identifier "a column with spaces" as a text string instead of column name. How can I specify that this is an identifier?

I found a way to trick it into parsing it as an identifier -- prefix it with the table identifier or alias:
SELECT group, sum(t."a column with spaces") FROM table_name t GROUP BY group;

Double-quotes are actually the right way to identify columns, but in this case it won't work.
I managed to apply an aggregate function renaming the column and removing its white spaces.
CREATE TABLE example (
"column with space" INTEGER
);
INSERT INTO example VALUES (1), (2);
SELECT SUM(column_with_space)
FROM (
SELECT "column with space" AS column_with_space
FROM example
) AS example_query;
Got result:
sum
3

Related

Grafana: how to overwrite values in one table with value mapping retrieved from another table

I have two tables as below. 1st table contain descriptions of all statuses. How to overwrite column status_id on right table with name from left table based on id?
Option 1: fixed list of mappings
When the status are fixed and a low number, I would recommend to use value mapping.
Make sure not to apply it to the whole table, but instead to use an override for the field status_id in your table.
Option 2: variable list of mappings
In case the status change or new ones are added, I would suggest a workaround as follows (since I don't use PostgreSQL, I show SQL statements from Google BigQuery standard SQL, please adjust for your usecase):
Create a query variable: query the table with the mappings (the description of the status) and by using concatenation, create the WHEN ... THEN ... part of a case statement. Example:
SELECT CONCAT('WHEN "', id, '" THEN "', name, '" ')
FROM ID_TABLE
This will give you rows like this: WHEN "1" THEN "OK". Then you have to concat/aggregate those rows to a single string.
Then use the variable in the query for your final table like in this example:
SELECT
CASE status_id
${QUERY_VARIABLE}
ELSE "UNKNOWN STATUS"
END AS status_id,
...
FROM YOUR_DATA_TABLE

Select column name containing forward slash in PostgreSQL

I am new to PostgreSQL and trying the below two scenarios:
Select a column name which already has a forward slash(/) in database (Road/Highway)
Using case on same column along with index
Select Road/Highway,
case
when index(upcase(Road/Highway), 'xturn') > 0 then 2
else 0
end as preferred_road
from abc_data;
But I am getting syntax error near index and for slash it is only taking Road.
Generally / means "division", so your column name is non-standard, much like working with keyword column names, column names with special characters must be quoted with double quotes. Use "Road/Highway" when referring to the column.

Timescaledb - How to display chunks of a hypertable in a specific schema

I have a table named conditions on a schema named test. I created a hypertable and inserted hundreds of rows.
When I run select show_chunks(), it works and displays chunks but I cannot use the table name as parameter as suggested in the manual. This does not work:
SELECT show_chunks("test"."conditions");
How can I fix this?
Ps: I want to query the chunk itself by its name? How can I do this?
The show_chunks expects a regclass, which depending on your current search path means you need to schema qualify the table.
The following should work:
SELECT public.show_chunks('test.conditions');
The double quotes are only necessary if your table is a delimited identifier, for example if your tablename contains a space, you would need to add the double quotes for the identifier. You will still need to wrap it in single quotes though:
SELECT public.show_chunks('test."equipment conditions"');
SELECT public.show_chunks('"test schema"."equipment conditions"');
For more information about identifier quoting:
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Edit: Addressing the PS:
I want to query the chunk itself by its name? How can I do this?
feike=# SELECT public.show_chunks('test.conditions');
show_chunks
--------------------------------------------
_timescaledb_internal._hyper_28_1176_chunk
_timescaledb_internal._hyper_28_1177_chunk
[...]
SELECT * FROM _timescaledb_internal._hyper_28_1176_chunk;

where clause requiring double quotes on an nvarchar?

all columns in DataImport table are nvarchar(max). column d is requiring double quotes.
the statement:
select cast(i.a as varchar(50)) as address,cast(i.c as varchar(50)) as phone
from dataimport i where i.d="x"
produces two records.
the statement:
select cast(i.a as varchar(50)) as address,cast(i.c as varchar(50)) as phone
from dataimport i where i.d='x'
produces no records
column definition is d(nvarchar(max),null)
Is there a column called x?
Using double quotes you're specifying column names and not data inside a column.
If the statement is not returning any data using single quotes there seems to be no record containing a 'x' inside column d.

Taking the prefix of a column value in PostgreSQL

I want to select a column value and trim away a suffix matching a regular expression, all inside PostgreSQL, how can this be done?
-- somewhat like performing s/_bar$// on "foo_bar"
select regexp_replace('foo_bar', '_bar$', '')