Transaction
which has following Columns:
Transaction_no | Register|Adult|child
The Input Data is as follows:
INPUT
+----------------+----------+-------+-------+
| transaction_no | register | adult | child |
+----------------+----------+-------+-------+
| 1234 | A | 0 | 1 |
| 1234 | A | 1 | 2 |
| 1234 | A | 1 | 1 |
| 3456 | B | 1 | 0 |
| 5678 | B | 1 | 0 |
| 2468 | C | 1 | 0 |
| 2468 | C | 0 | 1 |
+----------------+----------+-------+-------+
My Requirement is to add another column namely rn using mySQL which will use Rank and dense rank like logic to generate the following intermediate output
INTERMEDIATE
+----------------+----------+-------+-------+----+
| transaction_no | register | adult | child | rn |
+----------------+----------+-------+-------+----+
| 1234 | A | 0 | 1 | 1 |
| 1234 | A | 1 | 2 | 2 |
| 1234 | A | 1 | 1 | 3 |
| 3456 | B | 1 | 0 | 1 |
| 5678 | B | 1 | 0 | 1 |
| 2468 | C | 1 | 0 | 1 |
| 2468 | C | 0 | 1 | 2 |
+----------------+----------+-------+-------+----+
Here the partition is done on transaction number.
The Final Query Output should contain all the rows whose rn=1 and the rn value should not displayed.
OUTPUT
+----------------+----------+-------+-------+
| transaction_no | register | adult | child |
+----------------+----------+-------+-------+
| 1234 | A | 0 | 1 |
| 3456 | B | 1 | 0 |
| 5678 | B | 1 | 0 |
| 2468 | C | 1 | 0 |
+----------------+----------+-------+-------+
Oracle Documentation for Reference : OracleDocument
I have also added SQL fiddle for Reference.SqlFiddlePlease help me on this.
MySQL Solution:
Note: Desired ordering can only be achieved on explicit request, hence
ORDER BY
is used in the query.