I have a (probably) very easy question for a SQL server data issue. I have some test data with missing Customer IDs in certain rows - but I know that when the Details column is the same, I will have the same ID.
Meaning, for row 6 Customer will be 3 since it has the same Details as row 5 and 4.
Customer | Details | Date | Amount |
---|---|---|---|
1 | 40495BS | 15/01/2022 | 300 |
1 | 40495BS | 10/02/2022 | 250 |
2 | 83825NO | 31/10/2021 | 100 |
3 | 90401HI | 01/06/2022 | 525 |
3 | 90401HI | 07/09/2022 | 130 |
90401HI | 09/05/2022 | -130 | |
4 | 17452RE | 14/07/2022 | 125 |
Any ideas for a fix to return all the missing Customer IDs based on this logic?
CodePudding user response:
Actually MAX()
used as an analytic function might work well here:
SELECT MAX(Customer) OVER (PARTITION BY Details) AS Customer,
Details, Date, Amount
FROM yourTable;