Update rows from another table [duplicate] - tsql

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 8 years ago.
I have a table with 2 columns, Country and Qty. the field country has distinct acronyms of all the countries. Now my job is to replace those acronyms with the actual country name. Now there is another table with acronyms and corresponding country names. I have to take the values from this second table and update the first one where Acronyms match. Please help..

UPDATE q
SET country = a.country
FROM quantity q
JOIN acronym a
ON a.acronym = q.country

Related

Postgresql returns rows in a wrong order [duplicate]

This question already has answers here:
Postgres default sort by id - worldship
(4 answers)
Postgresql: row number changes on update
(1 answer)
Closed 3 months ago.
I found a really strange thing related to postgresql. Here is my query:
select *
from "bids"
inner join "users" on "users"."id" = "bids"."userId"
where "auctionId" = 151
And here is a result:
So, as you see, rows are sorted in descending order by id. If I add ordering in query:
select *
from "bids"
inner join "users" on "users"."id" = "bids"."userId"
where "auctionId" = 151
order by "bids"."id" asc
I get a result:
It looks like one of database settings was set incorrectly, but I couldn't find it in google. Please, put your thoughts here, any help is welcome.
Thanks.

How to use LIKE in these cases? [duplicate]

This question already has answers here:
Regular expression in PostgreSQL LIKE clause
(2 answers)
Closed 10 months ago.
There is a list of countries.
I want to get the list of the countries that:
consist of the letter U and A -or- S
consist of the letter U and any character except S.
These structures are correct for the MS SQL but don't work for Postgres
For MS SQL Server:
SELECT
country
FROM world.country
WHERE country LIKE 'U[AS]%'
SELECT
country
FROM world.country
WHERE country LIKE 'U[S]%'
For Postgres?
You can use SIMILAR TO Documentation
select * from (values ('UAA'),('MUA'),('UUA'),('USA')) as test (country)
where country SIMILAR TO 'U(A|S)%'
returns 'UAA' and 'USA'
You can use also POSIX regular expression with the same result.
select * from (values ('UAA'),('MUA'),('UUA'),('USA')) as test (country)
where country ~ '^U(A|S)'

postgres `insert on conflict update` multiple columns [duplicate]

This question already has answers here:
How to update all columns with INSERT ... ON CONFLICT ...?
(2 answers)
Closed 4 years ago.
In this Postgres query,
INSERT INTO TB_PO
SELECT * FROM temporary_table
ON CONFLICT (id) DO UPDATE
SET id = excluded.id;
Since both the tables tb_po and temporary_table are identical with 26+ columns, is there a way I can specify after the SET, that it will set all columns of the affected row? So that I don't have to manually input each column with SET.
thanks
You could avoid some typing by generating your statement based on the results of
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'TB_PO';

Subquery not working with NULL data [duplicate]

This question already has answers here:
How does 'in' clause works in oracle
(5 answers)
NOT IN (subquery) producing zero rows
(2 answers)
SQL "select where not in subquery" returns no results
(12 answers)
Why is nothing selected in this select query?
(5 answers)
Correct way to use "NOT IN" Postgres
(4 answers)
Closed 5 years ago.
Today I have seen one weird issue in PostgreSQL query. In which I have 2 tables Products and Parameters.
Purpose:
It's very simple query, I just want to list all products which are not
there in parameter table.
Query:
select
id
from product_proudct
where active=True and
id not in
(
select distinct product_id from parameter_view
)
Issue:
It won't list anything if there is null value in
parameter table in product_id column. I have verified by just removing null record from the parameter table by adding where clause.
select distinct product_id from parameter_view where product_id is not null
And then it's working fine but with null it won't work, it's really
strange for me.
I would like to know the reason why subquery not working well with null ?

sql server Junction table values saves four times in one upload

I uploaded a csv file to a dummy table(csv_upload) in sql server 2008. I have to distribute the data to the tables below using a trigger;
Questions Answers Test QAT
--------- --------- ------ -----
Questid Ansid Testid Questid
Question Answers Testname Ansid
Testid
id
StdUsername
The trigger code;
INSERT INTO tbl_answers ( Answer)
select DISTINCT Answer
from tbl_csv_upload
INSERT INTO tbl_questions ( Question )
select DISTINCT Question
from tbl_csv_upload
INSERT INTO tbl_taqa (StdUsername,questid, ansid , testid )
SELECT StdUsername ,q.quest_id,a.ans_id,t.test_id
FROM csv_upload c, questions q, answers a, test t
WHERE c.Question = q.Question
AND c.Answer = a.Answer
AND t.test_id = IDENT_CURRENT('test')
This trigger worked well for the first upload from an asp.net application but on second upload it saves the data 4 time in the QAT table instead of ones but other tables are ok.
Pls i need urgent help.
Probably, you have duplicates of questions and answers in your data. DISTINCT gets rid of duplicates currently present in the upload table, but the next time your are calling your proc they get inserted again since it does not take into account values already present in the table.
Use this to insert the answers:
INSERT
INTO answers (answer)
SELECT answer
FROM tbl_csv_upload
EXCEPT
SELECT answer
FROM answers
and a similar query for questions.