Postgresql: Concatenate 2 columns data into 1 column - postgresql

I am migrating the data from one DB to another (both postgresql). I want to concatenate the value of firstname and lastname columns from the source DB to name column of the destination DB. When I try to run the query, I am getting an error: psycopg2.ProgrammingError: more than one row returned by a subquery used as an expression Checking other threads here in SO, I found this concatenate. I added LIMIT 1 to the code but it gives me the first and last name of the existing name in the table.
My code:
cur_t.execute("""
SELECT firstname, lastname
FROM authors;
""")
for row in cur_t:
cur_p.execute("""
INSERT INTO lib_author (
created, modified, last_name,
first_name, country,
school_id, name)
VALUES (current_timestamp, current_timestamp, %s, %s, %s,
(SELECT id FROM ed_school WHERE name='My Test School'),
(SELECT CONCAT(first_name, ',', last_name) AS name FROM lib_author LIMIT 1)
)
""", (row['lastname'], row['firstname'], ''))
How can I call the firstname and lastname column from the source DB?

Related

increase field count when conflict on insert

I have the following table
"phone_number_info"
id uuid
country character
phone_number text
page_views bigint
I have the following query which is supposed to insert a row into the table, and if it already exists, increase the page_views count
INSERT INTO phone_number_info (country, phone_number) VALUES('us', '1234567890')
ON CONFLICT (country, phone_number)
DO UPDATE SET phone_number_info.page_views = (phone_number_info.page_views + 1);
When executing the query above, I get the following message
Error in query: ERROR: column "phone_number_info" of relation "phone_number_info" does not exist
LINE 3: DO UPDATE SET phone_number_info.page_views = (phone_number_i...
What is wrong with the query?
The column identifiers in the SET clauses cannot be not qualified with the table name - they always refer to the updated table. So use
INSERT INTO phone_number_info (country, phone_number) VALUES('us', '1234567890')
ON CONFLICT (country, phone_number)
DO UPDATE SET page_views = (phone_number_info.page_views + 1);
^^^^^^^^^^^^^^^^

Error on execute crosstab return n+ tuples

I try to get all results using these conditions to list all company have tickets with status opened, and list but I got message.
Using crosstab I want use other table in future.
ERROR: invalid return type
DETAIL: Query-specified return tuple has 3 columns but crosstab returns 49150.
My query:
select *
from crosstab ('select id as id_ticket,company_id, created_at, description
from tickets
where close != false
group by tickets.id, company_id, created_at
order by 1',
'select distinct(created_at) from tickets order by 1')
as tickets(id int, company_id int, description text);
i want get this output data
ID COMPANY_ID DESCRIPTION UPDATED_AT CREATED_AT
521521 1 TEST DEBUG .2019-01-10 23;59 .2019-01-10 23;5

Finding distinct values of non Primary Key column in CQL Cassandra

I use the following code for creating table:
CREATE KEYSPACE mykeyspace
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
USE mykeyspace;
CREATE TABLE users (
user_id int PRIMARY KEY,
fname text,
lname text
);
INSERT INTO users (user_id, fname, lname)
VALUES (1745, 'john', 'smith');
INSERT INTO users (user_id, fname, lname)
VALUES (1744, 'john', 'doe');
INSERT INTO users (user_id, fname, lname)
VALUES (1746, 'john', 'smith');
I would like to find the distinct value of lname column (that is not a PRIMARY KEY). I would like to get the following result:
lname
-------
smith
By using SELECT DISTINCT lname FROM users;
However since lname is not a PRIMARY KEY I get the following error:
InvalidRequest: code=2200 [Invalid query] message="SELECT DISTINCT queries must
only request partition key columns and/or static columns (not lname)"
cqlsh:mykeyspace> SELECT DISTINCT lname FROM users;
How can I get the distinct values from lname?
User - Undefined_variable - makes two good points:
In Cassandra, you need to build your data model to match your query patterns. This sometimes means duplicating your data into additional tables, to attain the desired level of query flexibility.
DISTINCT only works on partition keys.
So, one way to get this to work, would be to build a specific table to support that query:
CREATE TABLE users_by_lname (
lname text,
fname text,
user_id int,
PRIMARY KEY (lname, fname, user_id)
);
Now after I run your INSERTs to this new query table, this works:
aploetz#cqlsh:stackoverflow> SELECT DISTINCT lname FROm users_by_lname ;
lname
-------
smith
doe
(2 rows)
Notes: In this table, all rows with the same partition key (lname) will be sorted by fname, as fname is a clustering key. I added user_id as an additional clustering key, just to ensure uniqueness.
There is no such functionality in cassandra. DISTINCT is possible on partition key only.
You should Design Your data model based on your requirements.
You have to process the data in application logic (spark may be useful)

Pass String in Postgres Query

I am using Postgresql 9.3 and what I am trying to do is Passing column names as string into my query. For newtable my column number can be dynamic sometimes it might be 3 or more for which I am trying to select column from another table and pssing relut of my query as string in the existing query
Please help how can i do this
select * from crosstab (
'select "TIMESTAMP_S","VARIABLE","VALUE" from archieve_export_db_a3 group by 1,2,3 order by 1,2',
'select distinct "VARIABLE" From archieve_export_db_variables order by 1'
) AS newtable (TIMESTAMP_S int,_col1 integer,_col2 integer);

updating a varchar column with multiple select stmts

I have to update a VARCHAR column of a table by concatenating values from SELECT queries from another table. I have build a query like
UPDATE url SET VALUE = (SELECT id FROM ids WHERE identifier='site')':'(SELECT id FROM cids WHERE identifier='cid')
WHERE name='SToken:CToken'
AND tokenvalue LIKE (SELECT id FROM ids WHERE identifier='site');
Here value is VARCHAR.
How should I do this?