I have 3 tables:
manager with columns:
id serial PK
money int
facilities_work_data with columns:
id serial PK
income integer
manager_facilities with colunns:
id serial PK
manager_id references manager(id)
facilities_work_data_id references facilities_work_data(id)
The aim is to update all manager table rows by adding to manager.money column the value from facilities_work_data.income which in turn has to have id column selected as:
SELECT facilities_work_data_id from manager_facilities WHERE manager.id = manager_facilities.manager_id
At least I found the following request:
UPDATE A AS a SET money = a.money + b.income FROM B AS b WHERE b.a_id = a.id
But its not my case, seems like I need one more join.
Could you help with such a request?
you can get data from two tables and add the conditions in the where clause like below
update manager M
set money = money + FWD.income
FROM manager_facilities MF, facilities_work_data FWD
WHERE M.id = MF.manager_id
AND MF.facilities_work_data_id = FWD.id
Related
Just to level set: i'm working within a Vertica database using SQL.
Let's say i have two tables: Table A and Table B. Let's also say that Table A is my final/master table used for data vis within Tableau (or something akin), and that Table B feeds certain columns into Table A based on matches within a tertiary table, Table C (which is not relevant to this conversation).
As is, Table A has columns:
ProgramName [varchar(50)]
CustomerName [varchar(50)]
Total_Cost [numeric(18,4)]
As is, Table B has columns:
CustomerCode [varchar(10)]
Total_Cost [numeric(18,4)]
What I would like to do is update Table A's CustomerName column to equal CustomerCode in Table B where the columns of total_cost_dollars equal each other across tables.
I've run this left join query to ensure that, when I do update Table A's CustomerName to equal CustomerCode, the total cost columns are exact/true matches for my entire data set.
SELECT
A.ProgramName,
A.CustomerName,
A.total_cost_dollars,
B.CustomerCode,
B.total_cost_dollars
FROM
TableA A
LEFT JOIN
TableB B
ON
B.total_cost_dollars = A.total_cost_dollars
WHERE
A.CustomerName IS NULL;
Any idea on how to solve this problem?
Since Vertica supports merge query, you can use merge statement:
merge into TableA A
using TableB B
ON (B.total_cost_dollars = A.total_cost_dollars)
when matched then
update
set
A.CustomerName = B.CustomerCode
where
A.CustomerName IS NULL;
I have two tables.Table Bill has following fields:
Field_Name Field_Type
===============================
Bill_Sts_Sk decimal(18) PK
epn_id bigint child key
epn_seq_id bigint child key
ref_id integer child key
Table CLM_Crg has following fields:
Field_Name Field_Type
===============================
Bill_Sts_Sk decimal(18)
epn_id bigint PK
epn_seq_id bigint PK
ref_id integer PK
I need to look up Bill_Sts_Sk against parent key(Bill_Sts_Sk) against BILL table.Following is the condition for look up:
Lookup on BILL matching on epn_id,
epn_seq_id and ref_id
if not found,
try again only using the first 2 fields.
If not found use default value, -1.
If more than 1 key is found, use the maximum value
How can we achieve it by writing a sql query ? I have written following query for first part:
select Bill_Sts_Sk
from Bill bl
left join CLM_CRG crg
ON bl.epn_id = crg.epn_id
and bl.epn_seq_id = crg.epn_seq_id
and bl.ref_id = crg.ref_id
Can any any please help me to write sql query in Teradata(14.10.06.05) for the above conditions
You can join on the first two columns and then look for the best match using a ROW_NUMBER:
SELECT bl.*, COALESCE(crg.Bill_Sts_Sk, -1)
FROM Bill bl
LEFT JOIN CLM_CRG crg
ON bl.epn_id = crg.epn_id
AND bl.epn_seq_id = crg.epn_seq_id
QUALIFY
ROW_NUMBER()
OVER (PARTITION BY bl.epn_id, bl.epn_seq_id
ORDER BY CASE WHEN bl.ref_id = crg.ref_id THEN 1 ELSE 2 END -- best: matching ref_ids
,crg.Bill_Sts_Sk DESC) = 1 -- 2nd best: highest Bill_Sts_St
The best PI for this would be on (epn_id, epn_seq_id) for both tables.
It is a little bit tricky for me so I need your help :)
I want to update the column Relevant to 0 WHERE Contract_Status_Code is 10 OR the Date_Contract_start YEAR is the same AND the Ranking_Value is lower than the other one ON all records that have the same VIN.
So I want to compare all records which have the same VIN.
Few examples to illustrate it:
I have there two records with the VIN = 123456. One of them (ID = 6847) has a higher Ranking_Value (7) than the other one (3). The YEAR is the same as well so I want to update the column relevant to 0 where the ID is 8105.
Two records with the VIN = 654321. Both of them have the same Ranking_Value but the record with the id = 11012 has the value 10 for the column Contract_Status_Code so I want to update the relevant column to 0 where the ID = 11012.
The last two records... They have the VIN = 171819. The first one (ID = 11578) has the higher Ranking_Value. But they have a different year where the contract has started. So I don't want to update both.
It is also possible that there are three or four records with the same VIN.
I hope you understand my problem. I'm from Germany so sorry for my English :)
By considering your ID column as unique or Identity column, I can suggest you the below query for your solution:
With cte
As
(Select a.Id, a.VIN From Table a
Join (Select max(Ranking_Value) ranks,VIN From Table Group By VIN, Year(Date_Contract_start)) b
on a.VIN=b.VIN And a.Ranking_Value = b.ranks)
update table
set Relevant = 0
where (Contract_Status_Code = 10) Or
ID Not In (Select id from cte)
update table1
set Relevant = 0
where Contract_Status_Code = 10
or (VIN,Year,Ranking_value) not in(
select VIN,Year,max(Ranking_Value)
from table1
group by VIN,Year
)
Database: Oracle
I want to insert data from table 1 to table 2 but the catch is, primary key of table 2 is the combination of first 4 letters and last 4 numbers of the primary key of table 1.
For example:
Table 1 - primary key : abcd12349887/abcd22339887/abcder019987
In this case even if the primary key of table 1 is different, but when I extract the 1st 4 and last 4 chars, the output will be same abcd9887
So, when I use select to insert data, I get error of duplicate PK in table 2.
What I want is if the data of the PK is already present then don't add that record.
Here's my complete stored procedure:
INSERT INTO CPIPRODUCTFAMILIE
(productfamilieid, rapport, mesh, mesh_uitbreiding, productlabelid)
(SELECT DISTINCT (CONCAT(SUBSTR(p.productnummer,1,4),SUBSTR(p.productnummer,8,4)))
productnummer,
ps.rapport, ps.mesh, ps.mesh_uitbreiding, ps.productlabelid
FROM productspecificatie ps, productgroep pg,
product p left join cpiproductfamilie cpf
on (CONCAT(SUBSTR(p.productnummer,1,4),SUBSTR(p.productnummer,8,4))) = cpf.productfamilieid
WHERE p.productnummer = ps.productnummer
AND p.productgroepid = pg.productgroepid
AND cpf.productfamilieid IS NULL
AND pg.productietype = 'P'
**AND p.ROWID IN (SELECT MAX(ROWID) FROM product
GROUP BY (CONCAT(SUBSTR(productnummer,1,4),SUBSTR(productnummer,8,4))))**
AND (CONCAT(SUBSTR(p.productnummer,1,2),SUBSTR(p.productnummer,8,4))) not in
(select productfamilieid from cpiproductfamilie));
The highlighted section seems to be wrong, and because of this the data is not picking up.
Please help
Try using this.
p.productnummer IN (SELECT MAX(productnummer) FROM product
GROUP BY (CONCAT(SUBSTR(productnummer,1,4),SUBSTR(productnummer,8,4))))
I have a question using below 3 tables:
1. Company (table)
CONAME
COPOINTS
2. Group_Member (table)
CONAME
NAME
3. Member (table)
NAME
MPOINTS
I would like to have a correct query with the following condition:
Update Member
Set MPOINTS=MPOINTS+5
Where Company.CONAME=Group_Member.CONAME
And
Group_Member.NAME=Member.NAME
Can you please correct above query?
In T-SQL (MS-SQL Server dialect) this would be
Update Member
set MPOINTS = MPOINTS + 5
from Group_Member
join Company on Company.CONAME = Group_Member.CONAME
where Group_Member.NAME = Member.NAME
I would like to remind you, that names are not a good choice for neither primary nor foreign key relations. You will never be able to change a name without violating primary key constraints. Use a (numeric, autoincrement) ID column instead.
try this:
Update M
Set MPOINTS=MPOINTS+5
from Member M
join Group_Member GM
on GM.NAME=M.NAME
join Company C
on C.CONAME=GM.CONAME
SQL Server 2005+
UPDATE x
SET x.MPOINTS += 5
FROM (SELECT m.MPOINTS FROM Company c JOIN Group_Member g ON c.CONAME = g.CONAME
JOIN Member m ON g.NAME = m.NAME) x