I'm having a hard time trying to puzzle together a query against an older MySQL 5.0 DB. The data model isn't optimal to begin with.
Data
ReceiptID | LineNbr | ProdNbr | Description | PrincipleDescription |
---|---|---|---|---|
54092 | 1 | 1023 | abc | abc |
54092 | 2 | 731 | def | def |
54092 | 3 | 617 | ghi | ghi |
54092 | 4 | 618 | rst | ghi |
54092 | 5 | 692 | xyz | ghi |
54092 | 6 | 331 | jkl | jkl |
54092 | 7 | 608 | mno | mno |
54092 | 8 | 618 | rst | mno |
54092 | 9 | 1040 | ahd | ahd |
The Description values rst, xyz are prefixed with two space characters thus stored in the DB as e.g. ' rst'. These are added comments/notes to the preceding line item that ain't prefix with spaces.
Expected Output:
ReceiptID | LineNbr | ProdNbr | DESCRIPTION | PrincipleDescription |
---|---|---|---|---|
54092 | 1 | 1023 | abc | abc |
54092 | 2 | 731 | def | def |
54092 | 3 | 617 | ghi | ghi (rst, xyz) |
54092 | 6 | 331 | jkl | jkl |
54092 | 7 | 608 | mno | mno (rst) |
54092 | 9 | 1040 | ahd | ahd |
I was thinking LEAD() or alike could help out here but that only got introduced since MySQL 8 thus that ain't available for my use case. Any other ways I could achieve such result instead?