updating a table in informatica by giving precedence to a column - updates

I am updating a table with an ID based on some condition (
ex:id is null ) through target update overide concept.
My requirement is, there is an table_num coulmn in the same table which has 3 values say 1,2,3.
Basically in dw terms I have to update the dimensions first and then the facts.Here 1 is a dimension where as 2 and 3 are facts.
So if i have 3 records with table_num 1,2,3 which has id as null.I should only update record with table_num=1.
Second scenario: if id is not null for table_num=1 and 2 and 3 are having nulls.Then i have to update 2 and 3.
Third SCenario: if 2 or 3 then update 2 or 3.
So basically table_num=1 should get the first precedence.
I know how to handle it in a pl/sql procedure and call a stored procedure tranformation.But i want to handle it in informatica.Could you please help me

You could use a Lookupon your target table in your process:
Join condition: whatever column(s) you use to update your target
Output: table_num
For this to work you must configure the Lookup policy on multiple match (in the Properties tab of the Lookup) to Use First Value and add a Lookup Source Filter: ID IS NULL.
Then you can use the table_num from the Lookup as an additional condition to update your target. If the table_num = 1 has already been updated (i.e. ID IS NOT NULL), but the table_num = 2 has not, the table_num = 2 will be update.
I hope I got this right, let us know.

Related

How to duplicate all rows in SQL Database table and change data in one of the columns?

So I have an SQL DATABASE and I want to duplicate all rows from a table named "searches", that have the word "google" in the column named "search_url". The records in this table are more than 300,000 so I don't want to duplicate them all, just the ones that have "google" in the column named "search_url".
The new rows' ID should automatically increment, and all the rest of the columns must be the same, except for the column named "search_url" where "google" has to become "search.yahoo"
My SQL is not that good and I will appreciate all the help I can get! Thank you :)
EDIT:
Sample data:
ID SEARCH_URL SEARCHABLE_ID SEARCHABLE_TYPE
1 https://www.google.com/search?q=camping 1 App\News
2 https://www.google.com/search?q=hiking 2 App\News
So I want this to become after this is duplicate:
ID SEARCH_URL SEARCHABLE_ID SEARCHABLE_TYPE
1 https://www.google.com/search?q=camping 1 App\News
2 https://www.google.com/search?q=hiking 2 App\News
3 https://search.yahoo.com/?q=camping 1 App\News
4 https://search.yahoo.com/?q=hiking 2 App\News
Here's a step by step. Follow this process so you don't end up inserting a load of bad data.
1) Select the rows you want, except the autoincrement primary key id - mysql will manage that itself:
SELECT SEARCH_URL, SEARCHABLE_ID, SEARCHABLE_TYPE FROM TABLE --where .. ?
Use a where clause if you don't want all rows
2) You modify the data according to your wish. Here we use the string replace function to change google into yahoo:
SELECT REPLACE(SEARCH_URL, 'google', 'search.yahoo'), SEARCHABLE_ID, SEARCHABLE_TYPE FROM TABLE
Check it looks good.. (You said you wanted to change google -> search.yahoo but strictly speaking your example data demands www.google -> search.yahoo) - CHECK YOUR DATA BEFORE YOU INSERT IT
3) Then add in an insert above it, to insert into your table
INSERT INTO TABLE(SEARCH_URL, SEARCHABLE_ID, SEARCHABLE_TYPE)
SELECT REPLACE(SEARCH_URL, 'google', 'search.yahoo'), SEARCHABLE_ID, SEARCHABLE_TYPE FROM TABLE
You need to specify the full set of columns you're inserting, and you have to specify the same count of columns for insert as you have selected.
If you have an autoincrementing primary key, you can't do an insert into table select * from table because that will select the ID column and then try to insert what will technically be a bunch of already-existing values
Please use below query to get the desired output:
INSERT INTO searches(search_url, searchable_id,searchable_type)
SELECT REPLACE(search_url, 'google', 'search.yahoo'), searchable_id,searchable_type FROM searches;
Hope it works for you.

Update the value of a row which is present in one of two tables

I have two tables
**Table A**
ID Name
1 abc
2 bcd
4 lmh
**Table B**
ID Name
3 abc
5 bcd
I don't know in which table ID=4 resides so I want to update the value with ID=4
how can i update the row with this?
I use UNION for selecting the value from multiple colmns but union is not working on update query.
I am using mysql.
Please help.
I don't think it's the good solution but I just run the update query two times. if it'll find some data with the requested id in either of tables it'll update the data

Is it possible to reference a mysql table entry value from a second table entry dynamically?

I can't find anything about dynamically referencing one MySQL table entry to another. It may not be possible.
Essentially, I'd like to know if in MySQL you can do the equivalent to referencing the value of a certain Excel cell to another. For example, if in Excel I set Sheet 1 Cell A1 to some value like "MyVal". Then if I set Sheet 2 Cell A1 to "=Sheet1!A1" and Sheet 3 Cell A1 to "=Sheet2!A1" the value of Sheet 3 Cell A1 is "MyVal". If I go back to Sheet 1 Cell A1 and change the value to "MyNewVal" then the value is automatically updated on Sheet 2 Cell A1 and Sheet 3 Cell A1 to "MyNewVal".
My question is... in MySQL can I set the value of a certain entry in the first table to be dynamically linked to the value of a different entry in a second table such that when I query the first table (using existing PHP code) I get the value that's in the second table? I imagine that if it's possible then perhaps the value of the entry in the first table would look like a query that queries the second table for the correct value.
I understand how to write an UPDATE query in PHP to explicitly make the values the same but I don't want to change the existing php code. I want to link them in a relative/dynamic way. The short reason is that I don't want to change the PHP code since the same code is used on several of the sites I maintain and I want to keep the existing php code the same for cleaner maintenance/upgrading.
However, since the databases on the various sites are already different, it would be very clean to somehow dynamically link the appropriate entries in the different tables in the database itself.
Any help would be very appreciated. If this is possible, if you could just point me in the right direction, I'd be happy to do the research.
There are 2.5 ways to do this (basically two, but it feels like there's three):
From easiest to hardest...
Option 1:
If you need tableA to reflect tableB's value, don't store the value in tableA at all, just use tableB's value. Use either a join:
select a.*, b.col1
from tableA a
join tableB b on <some join condition>
or a subselect
select *, (select col1 from tableB where <some condition>) col1
from tableA
Option 2:
If you're happy with option 1, convert it to a view, which behaves like a table (except are restrictions on updating views that are joins):
create view myview as
select ... (one of the above selects)
Option 3:
Create a database trigger that fires when tableB's value is changed and copies the value over to the appropriate row/column in tableA
create trigger tableB_update
after update on tableB
for each row
update tableA set
tablea_col = new.col1
where id = new.tableA_id;
Note that new and old are special names given to the new and old rows so you can reference the values in the table being updated.
Choose the option that best suits your needs.
Databases don't really provide this type of facility, it's a completely different paradigm.
You can achieve the same results using joins, groupings or functions.
ALternatively if you wish to save the representation, store the query into a view which makes it more re-usable from various interfaces. More information on views can be found here; http://www.mysqltutorial.org/mysql-views-tutorial.aspx
Anything more complex and you will need to look at some business analysis tools.
Perhaps you have oversimplified the question, but you should not need to use a trigger. Just join the tables and any time 'MyVal' is changed it will automatically be available through query.
CREATE TABLE Sheet1
(
`ID` int auto_increment primary key
, `A` varchar(5)
)
;
INSERT INTO Sheet1
(`A`)
VALUES
('MyVal')
;
CREATE TABLE Sheet2
(
`ID` int auto_increment primary key
, `Sheet1FK` int)
;
INSERT INTO Sheet2
(`Sheet1FK`)
VALUES
(1)
;
CREATE TABLE Sheet3
(
`ID` int auto_increment primary key
, `Sheet2FK` int)
;
INSERT INTO Sheet3
(`Sheet2FK`)
VALUES
(1)
;
Query 1:
select
sheet3.id id3
, sheet2.id id2
, sheet1.id id1
, sheet1.a
from sheet1
inner join sheet2 on sheet1.id = sheet2.sheet1fk
inner join sheet3 on sheet2.id = sheet3.sheet2fk
Results:
| ID3 | ID2 | ID1 | A |
|-----|-----|-----|-------|
| 1 | 1 | 1 | MyVal |

How Do I delete these orphan records from my Table, Iteratively?

I have 2 tables like this
words(word_id, value);
word_map(sno(auto_inc), wm_id, service_id, word_id, base_id, root_id);
in which sno is auto incremented just for indexing.
wm_id is the actual id which are unique for each service like
(serviceid, wm_id together form a unique key).
base_id and root_id are referenced to wm_id i.e., I store the values of respective wm_id of new word being inserted.
My Requirement now is I want to delete the records from this table where, a words's base_id or root_id does not exists in the table
For example,
A new word with tr_id = 4, its base_id = 2 and root_id = 1 then There must two other records with tr_id s 2 and 1 if not we can call it as an orphan and that record with wm_id = 4 must be deleted, then records with other wm_ids having this 4 as base_id or root_id must also be deleted as they r also now orphans if 4 gets deleted and so on.
Can anybody suggest me the solution for the problem.
What I tried:
I tried write a procedure using while in which it has a query like,
delete from words_map where base_id not in (select wm_id from words_map) or root_id not in (select wm_id from words_map)
But deleting/ or updating on same table using this kind of nested queries is not possible, So I am searching for an alternate way.
What I doubt is :
I thought of reading these wm_ids into an array then reading one by one deleting based on that, but I dont think we have arrays in stored
procedures.
Is Cursor an alternative for this sitution.
or any other best solution for this problem.
EDIT 1: Please go through this http://sqlfiddle.com/#!2/a4b6f/15 for clear experimental data
Any and early help would be appreciated

Is that possible making sure same ID wont appear twice in 2 tables (Database)

Is that possible making sure same ID wont appear twice in 2 tables?
For example i've got this colum: call id
and I want to make sure it'll count it in both tables
for ex: calls_to_x and calls_to_y
I want to enter both tables data and to make sure the call id colum will increase from both table for ex:
calls_to_x:
Call id: 1 parameters: .... time: 01:00:00
Call id: 3 parameters: .... time: 01:30:00
calls_to_y:
Call id: 2 parameters: .... time: 01:10:00
Call id: 4 parameters: .... time: 01:34:00
The answer depends on what RDBMS you're using.
For MySQL:
If you only need unique values (and not sequential), you can use something like an UUID to uniquely identify each row.
If you do need the values to be sequential, either merge the tables to one single table by having a field indicating the type of content or use a third table to keep track of used ids with an id field and auto_increment for that column. A second column can keep track of which table used the id if needed. You can INSERT INTO that table in an SQL function and return the value of LAST_INSERT_ID() to get the next id for your other tables.
For most other RDBMSes
Use a sequence (CREATE SEQUENCE).
Another option would be to have a table that is AllCalls that has a callid and at recipient then include this id as a foreign key in either of the two tables. Only the fields that are different could be in the calls_to_x and calls_to_y table things that are generally applicable to any call could all be in the AllCalls table.