DISTINCT ON (col) col_alias – alias not working - postgresql

I am trying to run this command:
SELECT DISTINCT ON (_id) test FROM creator_map.infos;
which is giving me the error:
ERROR: column "test" does not exist
but according to the following tutorial, the test should be an alias
SELECT
DISTINCT ON
(column_1) column_1_alias,
column_2
FROM
tbl_name
ORDER BY
column_1,
column_2;
Source.
How do I get the distinct to become an alias?

You can't give on (...) an alias because it's not expression that "returns" a name. It's like trying to give an IN condition an alias where a in (1,2,3) as foo
If in doubt, read the manual. It does not mention an alias there.
You can only define an alias for an expression in the actual select list, and the (...) is not part of the select list, but part of the distinct on expression.
I think it's just a matter of bad wording on that tutorial page, and should read:
select distinct on (column1)
column1 as column_1_alias,
column2
from ....

Related

Select field names assigning an alias in firebird

It is possible to assign a alias to each fields when doing a select of field names, like that:
select rdb$field_name as field
from rdb$relation_fields
where rdb$relation_name='TableName'
What, for instance, would return something like that:
FieldName1
FieldName2
FieldName3
I'm looking for a way of set an alias for each field, instead returning FieldName1 return MyName, so on.
You may run command comment on column tablename.tablecolumn is 'my comment text'
See chapter 5.16 in SQL manual
But then you would have to do a special query to fetch comments along with field names.
select coalesce(rdb$description, rdb$field_name) as field
from rdb$relation_fields
where rdb$relation_name='TableName'

error while trying to name column in select query [duplicate]

I encountered a strange issue this morning. I was creating a view simplifying a list of applications in a Postgres table.
This failed.
CREATE OR REPLACE VIEW application_view AS
SELECT COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) name
, id
FROM application
ORDER BY COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name)
whereas
CREATE OR REPLACE VIEW application_view AS
SELECT COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) application
, id
FROM application
ORDER BY COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name)
worked.
I often use name as a column name in tables so any ideas as to why the first sql statement failed?
It's a keyword. When you want to use a keyword as an alias in the select list you have to use the word as:
select 1 name;
ERROR: syntax error at or near "name"
LINE 1: select 1 name;
select 1 as name;
name
------
1
(1 row)
From the documentation about aliases in the select list:
To specify the name to use for an output column, write AS output_name after the column's expression. (You can omit AS, but only if the desired output name does not match any PostgreSQL keyword (see Appendix C). For protection against possible future keyword additions, it is recommended that you always either write AS or double-quote the output name.)

Postgres query for sequence containing camel case

I want to set a sequence in my table. So I listed all sequences with
SELECT c.relname
FROM pg_class c
WHERE c.relkind = 'S';
which gives me e.g.: tableName_id_seq
when I try to set the sequence using
SELECT setval("tableName_id_seq", (SELECT MAX(id) from "tableName"));
I get:
ERROR: relation "tableName_id_seq" does not exist
LINE 1: SELECT setval('tableName_id_seq', (SELECT MAX(id) from "t...
I already tried single and double quotes. Anyone any idea what to do here?
Avoid creating identifiers in camel case. It's really boring to work with it:
CREATE SEQUENCE "CamelCaseSeq";
SELECT nextval(format('%I', 'CamelCaseSeq'));
SELECT setval(format('%I', 'CamelCaseSeq'), 6666);
SELECT currval(format('%I', 'CamelCaseSeq'));
SELECT * FROM "CamelCaseSeq";
A quote from the docs:
Quoting an identifier also makes it case-sensitive, whereas unquoted
names are always folded to lower case. For example, the identifiers
FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo"
and "FOO" are different from these three and each other.
More info here and here.

Can I use a Postgres keyword as an alias in select list?

I encountered a strange issue this morning. I was creating a view simplifying a list of applications in a Postgres table.
This failed.
CREATE OR REPLACE VIEW application_view AS
SELECT COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) name
, id
FROM application
ORDER BY COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name)
whereas
CREATE OR REPLACE VIEW application_view AS
SELECT COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name) application
, id
FROM application
ORDER BY COALESCE( nullif(full_name,''), nullif(additional_info,''), app_name)
worked.
I often use name as a column name in tables so any ideas as to why the first sql statement failed?
It's a keyword. When you want to use a keyword as an alias in the select list you have to use the word as:
select 1 name;
ERROR: syntax error at or near "name"
LINE 1: select 1 name;
select 1 as name;
name
------
1
(1 row)
From the documentation about aliases in the select list:
To specify the name to use for an output column, write AS output_name after the column's expression. (You can omit AS, but only if the desired output name does not match any PostgreSQL keyword (see Appendix C). For protection against possible future keyword additions, it is recommended that you always either write AS or double-quote the output name.)

How can I reduce the complexity of this expression?

I have an IIF in my query that says:
IIF(ApplicationDate BETWEEN #START and #END
AND convert(varchar,LEFT(AAA.LastName,2)+LEFT(BBB.CITY,2)+BBB.ZIPCode+RIGHT(CCC.SSN,4)) in <list>,THIS,THAT)
Where 'list' is 13,000 varchar strings that look like 'MCSM349954987'
When I run this query I get an error message that says "Internal error: An expression services limit has been reached. Please look for potentially complex expressions in your query, and try to simplify them."
Is there a way I can simplify this so my query will run?
It looks like you want to figure out how to compare that list... you can do that with a sub query.
First, use OPENROWSET to bring in your values from excel into a TEMP TABLE
IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL DROP TABLE #tempTable
SELECT * INTO #tempTable
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;HDR=YES;Database=C:\yourFolder\yourFile.xlsx',
[ProductList$]);
Now you can use this for your IN clause, via a sub-select.
SELECT
CASE
WHEN ApplicationDate BETWEEN #START and #END
AND (convert(varchar,LEFT(AAA.LastName,2)+LEFT(BBB.CITY,2)+BBB.ZIPCode+RIGHT(CCC.SSN,4)) in (select * from #tempTable) THEN 'True'
ELSE 'False'
END as SomeNewColumn
FROM
SomeTable