Find the mismatch columns from two tables with same structure - db2

I have two tables with same structure. I have to find the mismatch columns in both the tables based on id and year combination. Below is the table structure:
id and year is primary key in both the tables.
============================================================
Create table and insert script for table1:
create table table1 (id int, year int, name varchar(50), stat varchar(50), PRIMARY KEY (id,year));
insert into table1 values (1,2021,'Aman','L');
insert into table1 values (2,2021,'Ankit','H');
insert into table1 values (3,2021,'Rahul','G');
insert into table1 values (4,2021,'Gagan','L');
============================================================
Create table and insert script for table2:
create table table2 (id int, year int, name varchar(50), stat varchar(50), PRIMARY KEY (id,year));
insert into table2 values (1,2020,'Aman','H');
insert into table2 values (2,2020,'Anuj','M');
insert into table2 values (3,2020,'Rahul','G')
insert into table2 values (4,2020,'Abhi','L')
============================================================
Expected Output:
for example, id = 1 and year = 2021 from 1st table when compared with id = 1 and year = 2020 (table1 year -1) from table2 should return that stat is different.
id = 2 and year = 2021 from table1 when compared with id = 2 and year = 2020 from table2 should return that name and stat is different.
I need to compare the year-1 from table2 with year column of table1.
Can anyone help me with sql or DB2 query or procedure, how can I do that.

Sounds like you need a simple join
select *
from table1 t1
join table2 t2
on t1.id = t2.id
and t1.year = t2.year + 1
where t1.stat <> t2.stat
or t1.name <> t2.name --if you want this?

Related

In postgresql, Copying data of a column from table1 to table2 which is of different datatype in table1 and table2

Text datatype of a column in table1 has to be converted to integer datatype in table2 while copying the data. That is 3 categories are there in a column of table1 which has to be converted to integer type like(1, 2,3) in table2
Frist create select sql command from table 1 with the casting column and then create insert statement like this
insert into table2 (categories) select categories ::integer from table1
if you have more then 1 comunm then
insert into table2 (categories,column1) select categories ::integer,column1 from table1

PostgreSQL count other values of ID that have the same value of other column

Let's say we have the following table that stores id of an observation and its address_id. You can create the table with the following code:
drop table if exists schema.pl_address_cnt;
create table schema.pl_address_cnt (
id serial,
address_id int);
insert into schema.pl_address_cnt(address_id) values
(100), (101), (100), (101), (100), (125), (128), (200), (200), (100);
My task is to count for each id how many other ids (thus -1) have the same address_id. I've come up with a solution that turns out to be quite expensive (explain) on the original dataset. I wonder whether my solution can be somehow optimised.
with tmp_table as (select address_id
, count(distinct id) as id_count
from schema.pl_address_cnt
group by address_id
)
select id
, id_count - 1
from schema.pl_address_cnt as pac
left join tmp_table as tt on tt.address_id=pac.address_id;
You can try to omit the CTE and do a self left join on common address but different ID and then aggregate this.
SELECT pac1.id,
count(pac2.id)
FROM pl_address_cnt pac1
LEFT JOIN pl_address_cnt pac2
ON pac1.address_id = pac2.address_id
AND pac1.id <> pac2.id
GROUP BY pac1.id
ORDER BY pac1.id;
For performance you can try indexes on (address_id, id) and (id).

SQL: Union with subqueries

My structure is like this
table1
key
value
date
table2
key
value
date
table3
key
value
date
I would like to have a sql query which returns distinct all keys
and the sum of all values for each key from all 3 tables.
My try has been
Select key=
(select table1.key from table1 where date = '2017-05-30'
union select table2.key from table2 where date = '2017-05-30' union select
table3.key from table3 where date = '2017-05-30'), (select sum(value)
from table1 where table1.key = key and date = '2017-05-30' ) + select
(sum(value) from table2 where table2.key = key and date =
'2017-05-30') + select (sum(value) from table3 where table3.key=key
and date = '2017-05-30') from table1, table2, table3
You could combine the 3 tables into a temporary table and then do what you need to do on the temporary table:
CREATE TEMP TABLE temp1 AS
select key, value, date from table1
union all
select key, value, date from table2
union all
select key, value, date from table3

Join two tables with count from first table

I know there is an obvious answer to this question, but I'm like a noob trying to remember how to write queries. I have the following table structure in Postgresql:
CREATE TABLE public.table1 (
accountid BIGINT NOT NULL,
rpt_start DATE NOT NULL,
rpt_end DATE NOT NULL,
CONSTRAINT table1_pkey PRIMARY KEY(accountid, rpt_start, rpt_end)
)
WITH (oids = false);
CREATE TABLE public.table2 (
customer_id BIGINT NOT NULL,
read VARCHAR(255),
CONSTRAINT table2 PRIMARY KEY(customer_id)
)
WITH (oids = false);
The objective of the query is to display a result set of accountid's, count of accountid's in table1 and read from table2. The join is on table1.accountid = table2.customer_id.
The result set should appear as follows:
accountid count read
1234 2 100
1235 9 110
1236 1 91
The count column reflect the number of rows in table1 for each accountid. The read column is a value from table2 associated with the same accountid.
select accountid, "count", read
from
(
select accountid, count(*) "count"
from table1
group by accountid
) t1
inner join
table2 t2 on t1.accountid = t2.customer_id
order by accountid
SELECT table2.customer_id, COUNT(*), table2.read
FROM table2
LEFT JOIN table1 ON (table2.customer_id = table1.accountid)
GROUP BY table2.customer_id, table2.read
SELECT t2.customer_id, t2.read, COUNT(*) AS the_count
FROM table2 t2
JOIN table1 t1 ON t1.accountid = t2.customer_id
GROUP BY t2.customer_id, t2.read
;

postgresql calling column with same name

I have two tables, where they have the same ID name (I cannot change the way the tables are designed) and I'm trying to query table2's ID, how would I do this when they are joined?
create table table1(
id integer, -- PG: serial
description MediumString not null,
primary key (id)
);
create table table2 (
id integer, -- PG: serial
tid references table1(id),
primary key (id)
);
So basically when they're joined, two columns will have the same name "id" if I do the following query
select * from table1
join table2 on table1.id = table2.tid;
Alias the columns if you want both "id"s
SELECT table1.id AS id1, table2.id AS id2
FROM table1...
If you want to query all * on both tables but still be able to reference a specific id you can do that too, you will end up with duplicate id columns that you probably won't use, but in some situations if you really need all the data, it's worth it.
select table1.*, table2.*, table1.id as 'table1.id', table2.id as 'table2.id'
from ...
You cannot select it using select *.
try this :
select table1.id, table1.description, table2.id, table2.tid
from table1
inner join table2
on table1.id = table2.tid