I have a table that looks like this
Tep | ID number | Date | Value | type |
---|---|---|---|---|
ABC | 1 | 22-09-2021 | 1.2 | X |
XYZ | 2 | 22-10-2021 | 3.2 | X |
ABC | 3 | 22-10-2021 | 3.2 | Y |
WSH | 4 | 22-10-2021 | 3.2 | X |
I want the output like this
Tep | ID number | Date | Value | type | ID number -1 | Value -1 | type -1 | total |
---|---|---|---|---|---|---|---|---|
ABC | 1 | 22-09-2021 | 1.2 | X | 3 | 3.2 | Y | 4.4 |
Basically I want the records with the same value in TEP in the same row.
Please can someone suggest how to do it?
2
Answers
If there are only ever two rows with the same TEP value, you could use something like:
It joins the table to itself, matches on the TEP value and makes sure that the ID values are different.
Quite straightforward using window functions, a bit verbose but I hope easy to read.
the_table
CTE is a mimic of the actual data table. No restriction on the number of rows with the sametep
value.