I have two tables . I have to check why each row from table1 is missing from other table2.
table1
ID col1 col2 col3 date
------------------- --------- ---------- ----------- ------------
1000 AAA B 212 20220712
1001 ALC B 40 20210528
1002 BBBB B 13 20210528
1003 UUU B 299 20210528
1004 EEE S 36 20210707
1005 CCC S 695 20220420
table2
ID col1 col2 col3 date
------------------- --------- ---------- ----------- ------------
1000 AAA B 212 20220712
1001 AAA B 40 20210528
1002 BBBB B 13 20210428
1003 UUU B 299 20210528
1006 EEE S 36 20210707
1005 CCC B 695 20220520
Result table:
ID col1 col2 col3 date Reason
------------------- --------- ---------- ----------- ------------ -------------------
1001 ALC B 40 20210528 Col1 Change
1002 BBBB B 13 20210528 Date Change
1005 CCC S 695 20220420 Col2 and Date Change
1004 EEE S 36 20210707 ID change/whole row missing
For example,
1.ID 1000 matches between both the tables . This has to be ignored.
2.ID 1001 changed only in col1 and this has to be reported with column "Col1 changed"
3.ID 1002 changed only in date and this has to be reported with column "date changed"
4.ID 1005 changed in col2 and date and this has to be reported with column "date and col2 changed"
5. If anything is in table2 and not in table1 , this can be ignored. All the non matching rows between table1 and table2 has to be reported.
Related
I'm trying to get a table where the number of rows will always have to be 12xN. Where N is the number of different products that exist in an event table.
Basically I try to obtain the evolution of the value from one month to another.
What SQL logic should I use?
table
idfecha
update_id
value
--------
---------
-----
20220105
123
3
20220221
345
2
20220730
567
4
20221011
678
2
20221102
789
5
dimtime
idfecha
label_month
--------
---------
20220101
enero
20220102
enero
20220103
enero
20220104
enero
20220105
enero
20220106
enero
20220107
enero
20220108
enero
…
…
20221229
diciembre
20221230
diciembre
20221231
diciembre
result
product_id
value
----------
------
aaa
3
aaa
3
aaa
3
aaa
3
aaa
3
aaa
3
aaa
4
aaa
4
aaa
4
aaa
2
aaa
2
aaa
2
ccc
0
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
2
ccc
5
ccc
5
I made some attempts with:
GROUP BY dimtime.month_label, table.product_id.
and combining it with a left outer join, where left is the dimtime.
However, I do not get what I want and I am very clear about why.
Even so, I do not arrive at the adequate logic to achieve it.
Do I have to use an over partition?
thank you!
I am looking for the best solution to find duplicated rows that have in in specific column NULL value and some INTGER value as shown bellow.
Result
This is what I expect to get from query
TREATY_NUMBER
SECTION_NUMBER
DT_PERIOD_START
INVOLVEMENT
1
001
20190101
NULL
1
001
20190101
58
1
001
20200101
NULL
1
001
20200101
58
2
001
20200101
NULL
2
001
20200101
77
2
001
20200101
NULL
2
001
20210101
77
I was trying to do something like this to find all TREATY_NUMBERs that have INTEGER value and than to join them to same table to get all data.
select distinct v.*
from STREATY v
join
(select TREATY_NUMBER, SECTION_NUMBER, DT_PERIOD_START, max(INVOLVEMENT) INV
from TREATY group by TREATY_NUMBER, SECTION_NUMBER, DT_PERIOD_START
having count(*) >1) a
on a.TREATY_NUMBER=v.TREATY_NUMBER and a.DT_PERIOD_START=v.DT_PERIOD_START
where a.INV is not null
But in this case I got also a lines that have only INTEGER value but do not have any NULL value
This is what I get now from query
TREATY_NUMBER
SECTION_NUMBER
DT_PERIOD_START
INVOLVEMENT
1
001
20190101
NULL
1
001
20190101
58
1
001
20200101
NULL
1
001
20200101
58
2
001
20200101
NULL
2
001
20200101
77
2
001
20200101
NULL
2
001
20210101
77
6038
001
20200101
6
6038
001
20200101
7
6038
001
20200101
8
My query return below results, whenever i see value "YES" for COLUMN4 values i need to Set the "YES" value for all the records under the COLUMN1 group.
for Example DAVID has 2 records with NO and YES- but my target state should be "YES" for all rows because he has Value "YES" for atleast one of the records.
QUery Results
Column1 Column2 Column3 Column4
=================================
Mary AA AAA YES
Mary BB BBB YES
David AA AAA YES
David BB BBB NO
Clara AA AAA NO
Clara BB BBB NO
Requested Target State
Column1 Column2 Column3 Column4
================================================
Mary AA AAA YES
Mary BB BBB YES
David AA AAA YES
David BB BBB **YES**
Clara AA AAA NO
Clara BB BBB NO
UPDATE my_table m set m.column4='YES'
WHERE m.column4='NO'
AND exists(
select 1 from my_table mm where mm.column1= m.column1 and mm.column4 = 'YES')
Here is the Select statement you want,
SELECT mt.Column1,mt.Column2,mt.Column3,
(CASE WHEN mt2.cc IS NULL THEN 'NO' ELSE 'YES' END) AS Column4
FROM mytable mt LEFT JOIN
(SELECT Column1,COUNT(*) AS cc FROM mytable WHERE Column4 = 'YES' GROUP BY Column1)
AS mt2 ON mt2.Column1 = mt.Column1
Result:
+-------------+-------------+-------------+-------------+
| Column1 | Column2 | Column3 | Column4 |
+-------------+-------------+-------------+-------------+
| Mary | AA | AAA | YES |
+-------------+-------------+-------------+-------------+
| Mary | BB | BBB | YES |
+-------------+-------------+-------------+-------------+
| David | AA | AAA | YES |
+-------------+-------------+-------------+-------------+
| David | BB | BBB | YES |
+-------------+-------------+-------------+-------------+
| Clara | AA | AAA | NO |
+-------------+-------------+-------------+-------------+
| Clara | BB | BBB | NO |
+-------------+-------------+-------------+-------------+
Due to comments:
UPDATE my_table m1 set m.column4='YES'
WHERE m1.column4='NO'
AND m1.column1 IN (
select m2.column1 from my_table m2 where mm.column4 = 'YES')
I have 3 columns ID, serial_no, priority_no in my table.
It may look like below
--------------------
ID | Ser_No | pri_NO
--------------------
1 | 123 | 215
1 | 123 | 280
2 | 215 | 215
3 | 123 | 360
4 | 111 | 111
-------------------
Look at the table
We are having same ser_no for ID 1 and 3, and we are having same pri_no for ID 1 and 2
For ID = 1 , we are having Pri_No as 215 and the same 215 for Ser_No in ID = 2
and vice versa one record's ser_no will be in another record's Pri_No
The above conditions, we called it as Family
Now the output I need is (Not the family records)
--------
ID
--------
4
--------
select id from table a
where not exists
(select * from table where (a.Ser_No = Ser_No or a.pri_NO = pri_NO) and id!=a.id)
SQL Fiddle check
Would like to count records in the result of aggregate query. So if my original data looks like this:
========================
Field 1 Field 2 Field 3
------------------------
aaa ccc 10
aaa ccc 10
aaa ddd 10
aaa ddd 10
bbb ddd 10
bbb ddd 10
bbb eee 10
I group by Field 1, Field 2 and I do Sum of Field 3
So result looks:
========================
Field 1 Field 2 Field 3
------------------------
aaa ccc 20
aaa ddd 20
bbb ddd 20
ddd eee 10
I would like to count, how many times each value appears in Field 1 and Field 2.
So that result would look like this:
======================================================
Field 1 Field 2 Field 3 Count Field 1 Count Field 2
------------------------------------------------------
aaa ccc 20 2 1
aaa ddd 20 2 2
bbb ddd 20 2 2
ddd eee 10 2 1
I tried to use Cound function, but it counts original table, so shows result of original table, I get result:
======================================================
Field 1 Field 2 Field 3 Count Field 1 Count Field 2
------------------------------------------------------
aaa ccc 20 4 2
aaa ddd 20 4 4
bbb ddd 20 3 4
ddd eee 10 3 1
Most databases support window/analytic functions. These can be combined in aggregations to do what you want:
select field1, field2, sum(field3) as sumfield3,
count(*) over (partition by field1) as numField1,
count(*) over (partition by field2) as numField2
from table t
group by tield1, field2;