Delete oldest data with Talend - talend

I need to delete the oldest address of each user I have in a database with Talend. I assume that all the users in the database have more than one address. The user must be in the “student” user group.
The address is in the user_address table (example row included):
id user_id address_1 address_2 city country created_date
1 1 1 Rosebery Pl London UK 2017-03-12
The user group is in the user_group table (example row included):
user_id usergroup
1 Student
What components do I need? This is what I have so far but not sure it is correct.
tFileList ->Iterate -> tFileProperties -> Main -> tUnite -> Main -> tSortRow -> Main -> tSampleRow -> Main -> tFileDelet

First fetch the data in ASC order by using tDBInput component to give the oldest data on top:
select id, user_id, address_1, address_2, city, country
from student
order by created_date ASC
Get the First Match record using tMap
The image represents the component mapping to delete the oldest record in address table using tDBOutput

Related

How to delete certain amount of last rows in the table in PostgreSQL?

Without referencing the SERIAL id.
Something like:
delete from users LAST 3
which would delete last 3 rows from the table.
This will work :
DELETE
FROM users
WHERE id in (
SELECT id
FROM users
ORDER BY id desc
LIMIT 3
)

How to generate multiple formatted files in Talend open studio based on MySQL tables?

I need to pull data from MYSQL DB and create formatted files depending on the data. How can I do that by Talend open studio??
MySQL DB has one table ( user_id, order_id, purchase_date) and I need to generate csv files for each user contains his orders. files names should have the user_id ( output files could be like user_id.csv)
Thanks
You can try below -
tMysqlInput--->tFlowToIterate---(iterate)-->tMysqlInput--->tFileOutputDelimited
More details given below -
tmysqlInput(select user_id from table group by user_id) --- row Main ---> tFlowToIterate (uncheck use the default key option, create a new key called user_id and set value to user_id in dropdown) ----- Iterate -----> tmysqlInput(sql = "select user_id, order_id,purchase_date from table where user_id=((String)globalMap.get("user_id))") ----- row main ----> tFileOutputDelimited(set filename = (String)globalMap.get("user_id))+".csv").
to summarize - you first get list of all distinct user_id then you iterate through each of them and again fetch orders for each user_id by applying filter and use this user_id value from global variable into filename..

Postgresql select duplicate column by another column

I have a data set like
ID STATUS SOURCE
1 new data1
1 old data2
2 old data1
2 new data2
and I want to be able to select those duplicates ID by the STATUS column and keep the SOURCE, the final list will be:
ID STATUS SOURCE
1 new data1
2 new data2
I can make the list of duplicate ID with something like:
select id, status, source
from data
where id in (
select id
from data
group by id
having (count(* ) > 1)
then I can not find a way to filter by the status to remove the duplicates.
Thanks.
You can make use of Postgresql's DISTINCT ON feature in conjunction with an ORDER BY clause.
SELECT DISTINCT ON (id)
id,
status,
source
FROM data
ORDER BY id, status
Here is an aequivalent query that does not use any Postgres specific features:
SELECT id,
status,
source
FROM (SELECT id,
status,
source,
row_number() OVER (PARTITION BY id ORDER BY status) AS n
FROM data) AS sub
WHERE sub.n = 1
The ORDER BY clause feels a bit clumsy with this data set (in both query variants) because it uses alphabetical ordering in order to express the semantic ordering of "new is newer than old". The ordering would feel more natural if we used a timestamp column created_at (or similar) instead of the status column.

Complex Joins in Postgresql

It's possible I'm stupid, but I've been querying and checking for hours and I can't seem to find the answer to this, so I apologize in advance if the post is redundant... but I can't seem to find its doppelganger.
OK: I have a PostGreSQL db with the following tables:
Key(containing two fields in which I'm interested, ID and Name)
and a second table, Key.
Data contains well... data, sorted by ID. ID is unique, but each Name has multiple ID's. E.G. if Bill enters the building this is ID 1 for Bill. Mary enters the building, ID 2 for Mary, Bill re-enters the building, ID 3 for Bill.
The ID field is in both the Key table, and the DATA table.
What I want to do is... find
The MAX (e.g. last) ID, unique to EACH NAME, and the Data associated with it.
E.g. Bill - Last Login: ID 10. Time: 123UTC Door: West and so on.
So... I'm trying the following query:
SELECT
*
FROM
Data, Key
WHERE
Key.ID = (
SELECT
MAX (ID)
FROM
Key
GROUP BY ID
)
Here's the kicker, there's about... something like 800M items in these tables, so errors are... time consuming. Can anyone help to see if this query is gonna do what I expect?
Thanks so much.
To get the maximum key for each name . . .
select Name, max(ID) as max_id
from data
group by Name;
Join that to your other table.
select *
from key t1
inner join (select Name, max(ID) as max_id
from data
group by Name) t2
on t1.id = t2.max_id

Microsoft Access 2010 - Foreign Key as Dropdown

I have two tables that I display in a form:
tblUsers -> user_id, firstname, lastname, group_id
tblGroups -> group_id, groupName, groupDesc
I can get the data that I want with:
SELECT tblUsers.firstname, tblUsers.lastname, tblGroups.groupName
FROM tblGroups INNER JOIN tblUsers ON tblGroups.[group_id] = tblUsers.[group_id];
But what I need is a form that shows the user information and a dropdown list for the group but showing the name of the group not the ID and so the group can be changed for a specific user e.g. Manager, Editor etc...
Thanks,
Gareth
Right-click on the ComboBox (assuming it's a ComboBox...) and open the Properties window.
Set the Row Source to:
Select Group_ID, GroupName from tblGroups
Set the Column Count to 2, because you want the combo to store both the ID and the Group name
Set the Column Widths to "0; 2" (without the quotes). This will essentially hide the ID because the column width of the ID field is 0.
Set the Bound Column to 1, because you want to bind to the ID column and not the Group Name column, because the Group ID is easier to query with.
Then you reference the combo with Me!MyComboboxName.Value to get the Group ID.