Wildcard select of columns - postgresql

Is it possible to select the columns that match a pattern?
For example, I would like to write something similar to select customer_* from tbl to select only the columns customer_name, customer_age, and customer_height from tbl

Related

Liquibase insert select multiple rows postgres

I want to insert into table1 multiple rows from table2. The problem is that I have some fields in table1 that I want to compute, and some rows that I want to select from table2. For example something like this:
insert into table1 (id, selectField1, selectField2, constant)
values ((gen_random_uuid()), (select superField1 from table2), (select superField2 from table2), 'test');
So the logic is to select superField1 and superField2 from all the rows in the table2 and insert them into table1 with constant value test and generated uids. superField1 and superField2 should be from the same row in table2 when inserting in table1. How can I achieve something like this using liquibase?
P.S: I'm using <sql> tag since it's easier to implement using SQL than using XML changeset, but if you know how to do it in XML that would be appreciated too, but just in SQL will be enough too. DBMS is postgres.
Don't use the VALUES clause if the source is a SELECT statement:
insert into table1 (id, selectField1, selectField2, constant)
select gen_random_uuid(), superField1, superField2, 'test'
from table2;

how to get distinct records from all columns from table in postgresql

I have a table with 100 columns and i need to get distinct records from all the columns from the table.
I used below query to get distinct records from table
select distinct col1, col2, col3,........ from test_table
but is there any good query to fetch distinct records from all the columns
from table without mentioning column names in the query.
Since you want DISTINCT on all columns, and you want to select all columns, it couldn't be simpler:
SELECT DISTINCT * FROM table
I am not sure if there is a simpler way,
You can use information_schema to get your columns and then use it.
SELECT string_agg(column_name::character varying, ',') as columns
FROM information_schema.columns
WHERE table_schema = 'schema_name'
AND table_name = 'table_name'
This will return you the list of columns in your table.
SELECT string_agg(column_name::character varying, ',') as columns
FROM information_schema.columns
WHERE table_schema = 'schema_name'
AND table_name = 'table_name' \gset
You can refer to gset here,
For example, if your table has two columns 'a' and 'b', gset will store, 'a,b'.
echo might be used to check what gset has stored,
\echo :columns
The following query might help you,
select distinct :columns from table_name;

select all except for a specific column

I have a table with more than 20 columns, I want to get all columns except for one which I'll use in a conditional expression.
SELECT s.* (BUT NOT column1),
CASE WHEN column1 is null THEN 1 ELSE 2 END AS column1
from tb_sample s;
Can I achieve it in postgresql given the logic above?
It may not be ideal, but you can use information_schema to get the columns and use the column to exclude in the where clause.
That gives you a list of all the column names you DO want, which you can copy/paste into your select query:
select textcat(column_name, ',')
from information_schema.columns
where table_name ='table_name' and column_name !='column_to_exclude';

PostgreSQL select uniques from three different columns

I have one large table 100m+ rows and two smaller ones 2m rows ea. All three tables have a column of company names that need to be sent out to an API for matching. I want to select the strings from each column and then combine into a single column of unique strings.
I'm using a version of this response, but unsurprisingly the performance is very slow. Combined 2 columns into one column SQL
SELECT DISTINCT
unnest(string_to_array(upper(t.buyer) || '#' || upper(a.aw_supplier_name) || '#' || upper(b.supplier_source_string), '#'))
FROM
tenders t,
awards a,
banking b
;
Any ideas on a more performant way to achieve this?
Update: the banking table is the largest table with 100m rows.
Assuming PostgreSQL 9.6 and borrowing the select from rd_nielsen's answer, the following should give you a comma delimited string of the distinct names.
WITH cte
AS (
SELECT UPPER(T.buyer) NAMES
FROM tenders T
UNION
SELECT UPPER(A.aw_supplier_name) NAMES
FROM awards A
UNION
SELECT UPPER(b.supplier_source_string) NAMES
FROM banking b
)
SELECT array_to_string(ARRAY_AGG(cte.names), ',')
FROM cte
To get just a list of the combined names from all three tables, you could instead union together the selections from each table, like so:
select
upper(t.buyer)
from
tenders t
union
select
upper(a.aw_supplier_name)
from
awards a
union
select
upper(b.supplier_source_string)
from
banking b
;

Create a new table from Union two tables with union in postgres

I would like to create a new table as the result of the union of two tables without duplicates. I searched in stackoverflow and I found a question with exactly what I want but using mysql Create a new table from merging two tables with union.
Solution in mysql
CREATE TABLE new_table
SELECT * FROM table1
UNION
SELECT * FROM table2;
I tried to do something similar but I got:
SQL error.
I would like to achieve this if is possible with an statement similar to mysql.
I know that if you create a new table first with the fields that I want. I can do a select into this table over the union of this tables. If there aren't other option well I have to do something like this.
But in summary If possible to do something similar to the question with mysql in postgres. I would like to use syntactic sugar to do that
Thanks in advance
Update
In order to clarify I have two table with equal structure
TABLE1(id,field1,field2,field3)
TABLE2(id,field1,field2,field3)
and The table that I want
TABLE3(id,field1,field2,field3)
Notice that I tried
CREATE TABLE new_table as
SELECT * FROM table1
UNION
SELECT * FROM table2;
and it works but didn't put the fields in the correct place for example put field3 of table 1 in field 1 of table_result
You are missing the AS keyword:
CREATE TABLE new_table
AS
SELECT * FROM table1
UNION
SELECT * FROM table2;
If you need the columns in a specific order, then specify them in the select:
CREATE TABLE new_table
AS
SELECT id, column1, column2, column3
FROM table1
UNION
SELECT id, column1, column2, column3
FROM table2;
More details in the manual:
https://www.postgresql.org/docs/current/static/sql-createtableas.html