I have a request to remove some rows from a source.
This source contains 3 columns : Id, Type, Value, and containes some data like :
Id Type Value
1 Master This is the first value
1 Second This is a new value
1 Third This is not a mandatory value
2 Master Another one
2 Third And again
3 Second A new hope
3 Third A third
4 Second A single value
...
The rule to keep row is :
If single row for one Id, get the existing value
Else :
If multiple rows for same Id and 'Master' exists, get the 'Master' value
If multiple rows for same Id and 'Master' not exists and 'Second' exists, get the 'Second' value
If multiple rows for same Id and 'Master' not exist and 'Second' not exists and 'Third' exists, get the 'Third' value.
In my sample so, I would like to extract only :
Id Type Value
1 Master This is the first value
2 Master Another one
3 Second A new hope
4 Second A single value
I try split into 3 different sources and join or lookup, but not found any parameter to discard the duplicate row.
How I can do that ?
Thanks in advance,
BR
Xavier
Try put them through a sorter to order by ID then Type and finally through an aggregater grouping by ID asc, desc depending on your requirements (lucky master comes before second which comes before third alphabetically)
Please find the query which can be converted in to informatica map.
create table test1
( id integer,
type varchar2(100),
value varchar2(1000)
);
insert into test1 values (1,'Master','This is the first value');
insert into test1 values (1,'Second','This is a new value');
insert into test1 values (1,'Third','This is not a mandatory value');
insert into test1 values (2,'Master','This is the first value');
insert into test1 values (2,'Third','This is not a mandatory value');
insert into test1 values (3,'Second','This is the first value');
insert into test1 values (3,'Third','This is not a mandatory value');
insert into test1 values (4,'Second','mandatory value');
commit;
select * from test1;
From the below query "agg" part can be done in aggregator and decode function within aggregator transformation.
Then use joiner to join agg part and actual table
use filter to filter the required rows
with agg as
(select max(case when type='Master' then 10
when type='Second' then 9
when type='Third' then 8
else 7 end) ms,
id
from test1
group by id
)
select a.ms,b.* from agg a, test1 b
where a.id=b.id
and case when a.ms=10 then 'Master'
when a.ms=9 then 'Second'
when a.ms=8 then 'Third'
else 'noval2'
end =b.type;
Related
There are 3 tables (First, Second and Third). I need to check condition in table First and then make an action in Second or Third depend on the result. As it is a huge dataset I prefer to have it in one query instead scripting with PHP or so row by row.
The pseudocode could look like follows:
SELECT status FROM First WHERE userid=.... ;
IF 'status' = 2 THEN INSERT INTO Second (...)
ELSE INSERT INTO Third (...);
It can be converted to the form:
SELECT Count(*) AS cnt FROM First WHERE status=2 AND userid=.... ;
IF #cnt>0 INSERT INTO Second (...)
ELSE INSERT INTO Third (...);
The problem is that MySQL seems to not see the value of #cnt nor there is IF...ELSE... statement in MySQL for queries as far as I know (I may be wrong).
You may use 2 queries:
INSERT INTO Second ( {columns to be inserted into} )
SELECT {values to be inserted}
WHERE EXISTS (
SELECT status
FROM First
WHERE userid=....
AND status = 2
);
INSERT INTO Third ( {columns to be inserted into} )
SELECT {values to be inserted}
WHERE NOT EXISTS (
SELECT status
FROM First
WHERE userid=....
AND status = 2
);
Or use stored procedure.
I want to achieve the following in single SQL Query.
I am inserting values into table "A" for the columns "A1,A2".
I am passing the value for "A1" column directly.
For the column "A2", I need to select the "B2" column from "B" table and pass the value to "A2".
Incase if the "B" table does not contains the value for the condition which i am checking in my query, then i need to insert new row in "B" table and pass the B2 value to "A2".
My above query works well if the B table has value and condition matches.
I am not able to make it when the B table is not having/matching the value. Need help to insert new row and pass the newly inserted row to table A.
INSERT INTO A(A1,A2) VALUES('A1 Value',(select B2 from B where action = 'test'))
You can't do this in one query, since you can only insert into one table at a time. So there's no way to insert a new row into B in the same query if the row is missing.
Assuming action has a unique index, you can do it in 2 queries like this:
INSERT IGNORE INTO B (action, B2) VALUES ('test', 'default value');
INSERT INTO A (A1, A2)
SELECT 'A1 Value', B2
FROM B
WHERE action = 'test';
INSERT IGNORE will do nothing if action = 'test' already exists.
I want to insert values from different sources. For example
insert into a (('id','name','add'),'college')
select from b where id = 1,'abc'
Here there is no timestamp field in table b
I would rewrite your query as follows:
insert into a (id, name, add, college)
select id, name, add, 'abc' from b where id = 1
With this query, the 4th column in a will be assigned a constant value of 'abc'.
My database is in MySQL
I have a table, let's say of 4 columns.
I would like to know if it's possible, and how to implement the following: fill the 4th column according to the value of the column 2 and column 3
In Excel I have a formula, let's give an example: if column2 value is set to "grey" and column3 value is set to "car", then column 4 value should be set to "super"
I just say this as an example.
My real formula in Excel looks like this: =IF(K4=4;"Maximal";IF(K4>4;"Maximal";IF(K4=3;"Important";IF(K4>3;"Important";IF(K4=2;"Limited";IF(K4>2;"Limited";IF(K4=1;"Forgettable";IF(K4>1;"Forgettable";"error"))))))))
However I want to do it in SQL.
I was thinking of creating my table until the column 3, set column 4 to NULL or empty, then open a GUI written in Java and maybe there do a piece of code to automatically fill the column 4 according to what is in column 2 and column 3 (these values will be choosable via Choicelist).
But if there is a way to do it directly in SQL, I am interested
Thx a lot in advance for your help.
regards
Yes. you can easily update your NULL-values according to some requirements for the other values in other columns of a particular row with the Update statement
UPDATE <tablename>
SET <column> = 'value'
WHERE <condition>
The only drawback here might be that you have to create an update statement for each of the combinations of your values in column2 and column3. (however, it's not much work for your amount of conditions).
I created an example (demo):
Creating a table in SQL according to your example could look like this,I used a temporary one for the sake of an example:
CREATE GLOBAL TEMPORARY TABLE demoTable (
"Col1" VARCHAR2(50 BYTE) NOT NULL,
"Col2" VARCHAR2(50 BYTE) NOT NULL,
"Col3" VARCHAR2(50 BYTE) NOT NULL,
"Col4" VARCHAR2(50 BYTE) DEFAULT NULL
)
ON COMMIT PRESERVE ROWS
I also inserted some dummy data:
INSERT INTO demoTable VALUES ('Charles', 'grey', 'car', NULL);
INSERT INTO demoTable VALUES ('Alice', 'grey', 'bike', NULL);
INSERT INTO demoTable VALUES ('Bob', 'red', 'car', NULL);
The result:
Now, create the update statements like this, for example:
UPDATE demoTable dt
SET dt."Col4" = 'super'
WHERE dt."Col2" = 'grey' AND dt."Col3" = 'car';
The result
You can try like this;
select * from mytable
COL1 COL2
---- --------------------
0 -
1 -
2 -
3 -
4 -
4 record(s) selected.
update mytable Set Col2 =
Case
When Col1<1 Then 'error'
When Col1=1 Then 'Forget'
When Col1=2 Then 'Limited'
When Col1=3 Then 'Important'
When Col1=4 Then 'Maximal'
End"
select * from mytable"
COL1 COL2
---- --------------------
0 Error
1 Forget
2 Limited
3 Important
4 Maximal
4 record(s) selected.
You can create a sql function, lets say udfGetColumn4Value taking in the column2, column3 as parameters to it and return a value.
Now you can run a select column2, column3, udfGetColumn4Value(column2, column3) from table or a query as desired. Hope this helps.
You were not very precise regarding which DBMS you're using. And also about the exact logic behind using your two columns.
Still here comes a probable SQL-Server solution, where I have taken one statement using CASE WHEN with your example and concatenated your two columns col2 and col3 (you can apply your further logic of here) otherwise:
UPDATE TableName
SET Col4 = CASE WHEN col2 = 'red' AND col3 = 'car' THEN 'super' ELSE col2 + col3 END;
You should replace col2 + col3 with your further logic.
Seems that a simple UPDATE-Query could address your problem:
update things set result = "super" where thing = "car" and color = "grey";
The where-clause does what you desire to do by saying
fill the column 4 according to what is in column 2 and column 3
I created a test table here on turorialspoint, there you can check if it fits your needs.
i have to insert values in one of my columns in table based on restrictions on another table . these are the conditions. i have two tables A and B . A has a column a , and values in column a is dependent on values in table B. lets say there are b,c,d columns in B. now conditions are these if value of d='Y' , value in a is 1 , if value in b&c are same value of a is 2 and else value is 3 . so i wrote this code . A and B have same primary key values thus same number of rows
select b ,c,d from B case when d='Y' then insert into A(a) values(1);
else case when b=c then insert into A(a) values(2); else insert into
A(a) values(3); end case end case ;
now there seems to be a syntax error , but i feel my fundamentals are weak in this topic. i think this can easily be done using cursor or loops , but data sets are huge so i don't think this would be feasible
Try this way:
insert into A(a)
select case
when d = 'Y' then 1
when b = c then 2
else 3
end
from B