Archive old Information in table - sql

I want to archive old information in my table.
If the value in T1C2 is a number (or less than 5 characters) then I want to replace it with the word archive and the value in T1C1.
T1C1 T1C2
1 London
2 New York
3 2342
4 Cardiff
5 2394
6 Sydney
7 2342
8 2343
9 7345
Result
T1C1 T1C2
1 London
2 New York
3 Archive3
4 Cardiff
5 Archive5
6 Sydney
7 Archive7
8 Archive8
9 Archive9

You can do this with an update:
update mytable
set t1c2 = 'Archive' + cast(t1c1 as varchar(255))
where t1c2 not like '%[^0-9]%';
You could also use isnumeric() for the where, but I you need to check for other conditions:
where isnumeric(t1c2) = 1 and t1c2 not like '%[.e]%'

Try this one :
UPDATE yourtable
SET T1C2 = 'Archive' + Cast(T1C1 AS VARCHAR(100))
WHERE isnumeric(T1C2) = 1
OR Len(T1C2) < 5

Related

Cant merge two queries with different columns

I'm studying SQL and somehow I'm stuck with a question. I have 2 tables ('users' and 'follows').
Follows Table
user_id
follows
date
1
2
1993-09-01
2
1
1989-01-01
3
1
1993-07-01
2
3
1994-10-10
3
2
1995-03-01
4
2
1988-08-08
4
1
1988-08-08
1
4
1994-04-02
1
5
2000-01-01
5
1
2000-01-02
5
6
1986-01-10
7
1
1990-02-02
1
7
1996-10-01
1
8
1993-09-03
8
1
1995-09-01
8
9
1995-09-01
9
8
1996-01-10
7
8
1993-09-01
3
9
1996-05-30
4
9
1996-05-30
Users Table
user_id
first_name
last_name
school
1
Harry
Potter
Gryffindor
2
Ron
Wesley
Gryffindor
3
Hermonie
Granger
Gryffindor
4
Ginny
Weasley
Gryffindor
5
Draco
Malfoy
Slytherin
6
Tom
Riddle
Slytherin
7
Luna
Lovegood
Ravenclaw
8
Cho
Chang
Ravenclaw
9
Cedric
Diggory
Hufflepuff
I need to list all rows from follows where someone from one house follows someone from a different house. I tried to make 2 queries, one to get all houses related to follows.user_id and another one with all houses related to follows.follows and "merge" then:
select a.nome_id, a.user_id_house, b.follows_id, b.follows_house
from ( select follows.user_id as nome_id
, users.house as user_id_house
from follows inner join users
ON users.user_id = follows.user_id
) as a,
( select follows.follows as follows_id
, users.house as follows_house
from follows inner join users
ON follows.user_id = users.user_id
) as b
where a.user_id_house <> b.follows_house;
The problem is that the result is like 400 rows, its not right. I have no idea how I could solve this.
Try this
SELECT follows.user_id, users.school, followers.user_id, followers.school FROM follows
JOIN users ON follows.user_id=users.user_id
JOIN users as followers ON follows.follows=followers.user_id
WHERE users.school <> followers.school
Note: Pay attention to naming in my answer
Thanks for correcting to Thorsten Kettner

Is there a Teradata equivalent to R’s rbind?

I’m trying to combine table1 (with columns “id”, “state”, “cost”) and table2 (with same columns “id”, “state”, “cost”) such that all rows are combined into a single table3. There may be duplicates for “id” and/or “state” but all duplicates should be retained.
Table1:
Id
State
Cost
1
IL
50
3
CA
10
2
WY
70
Table2:
Id
State
Cost
4
NY
100
3
PA
15
6
FL
5
Goal table3:
Id
State
Cost
1
IL
50
3
CA
10
2
WY
70
4
NY
100
3
PA
15
6
FL
5
This would be a simple rbind using R but I’m probably overthinking it in Teradata.
Thanks!

t-sql merging two tables and replace null values

I have these two tables, and that what I want is to compare them to know if there is any null value in table 2, if there is, replace the existing value in table 1 by the null value in table 2 (by the code column that is the primary key).
Table 1
Code Name Points
1 Juan Perez 10
2 Marco Salgado 5
3 Carlos Soto 9
4 Alberto Ruiz 12
5 Alejandro Castro 5
10 Jonatan Polanco 0
11 JD NULL
Table 2
Code Name Points
1 Juan Perez 10
2 Marco Salgado 5
3 Carlos Soto 9
4 Alberto Ruiz 12
5 Alejandro Castro 5
10 Null 0
11 JD 9
The resulting table should look like this
Table 2
Code Name Points
1 Juan Perez 10
2 Marco Salgado 5
3 Carlos Soto 9
4 Alberto Ruiz 12
5 Alejandro Castro 5
10 Jonatan Polanco 0
11 JD 9
If you are trying to update the rows that have null values in Points column, You just need to join the two tables and add a where clause to limit the rows to the ones you want to update. Something like this
UPDATE t2
SET Points = t.Points
FROM table_1 t
JOIN table_2 t2
ON t.code = t2.code
WHERE t2.Points IS NULL

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3
table 1
ID name: status : age
1 john F 28
2 peter G 20
3 Roger K 67
Table 2:
ID name: status : age
1 john Y 28
2 peter J 20
3 Roger K 67
4 trump U 120
5 Donald F 450
Table 3 should contain the updated values
1 john Y 28
2 peter J 20
3 Roger K 67
I need to get the updated status of IDs present in table 1 in table 3 how can I do that.
Note: I am using exacttarget SQL activity and update and many more functionalities does not work so I need some work around> I have tried this but this does not work.
UPDATE
1C-C1-MatchStatus_72hoursSubscribers
SET
1C-C1-MatchStatus_72hoursSubscribers.current_status = B.current_status
FROM
1C-C1-MatchStatus_72hoursSubscribers A
INNER JOIN
a_query B
ON
A.current_status = B.current_status

How to select faulty records?

I'm investigating an error in one of our tables of a geographical database. Given the table below, the DistrictName and DisId should always have the same combination (i.e. Bronx = 11, Manhatten = 14), but some records have a different DisId (while still sharing the the same DistrictName).
Id DistrictName DisId Section
------------------------------------------------
1 Bronx 11 1
2 Bronx 11 2
3 Brooklyn 12 1
4 Brooklyn 13 2 //wrong
5 Manhatten 14 1
6 Manhatten 14 2
7 Queens 15 1
8 Queens 16 2 //wrong
9 Queens 17 3 //wrong
How can I select all faulty records in a query?
There is always a Section 1, so records with a section > 1 containing the same DistrictName but having a deviating DisId are the ones I'm looking for.
I've tried using a group by (districtname) but I'm having difficulties comparing with the section1 record. I'm kind of lost when it comes to putting the logic in the having or where clause. Any help appreciated!
select * from your_table
where section > 1
and districtname in
(
select districtname
from your_table
group by districtname
having count(distinct disid) > 1
)