Say i have this table
| Product | Version | Value |
|---|---|---|
| 1 | 1 | 1000 |
| 2 | 1 | 2000 |
| 3_a | 1 | 2500 |
| 3 | 1 | 3000 |
| 2_a | 1 | 1200 |
What i want to do is:
For each row where product does not end with '_a':
check if there is any row with same product number that ends with '_a':
check if the value in that row is 60% of the value from the first row.
If there is no row like that, create it and increment version by 1
In logical terms:
For each row {
If exists(concatenate(row.Product,'_a')) as row2 then {
If not(row2.Value=row.Value*0.6) then {
Create row, Product=row2.Product, Version=row2.version+1,Value=row.value*0.6
}
} Else {
Create row, Product=concatenate(row.Product,'_a'), Version=1,Value=row.value*0.6
}
}
I have tried this approach (with correct syntax) but don't know how to reference the original row to compare the values, or generate the Versions. The desiered result should be two new rows:
| Product | Version | Value |
|---|---|---|
| 1_a | 1 | 600 |
| 3_a | 2 | 1800 |
Using SQL Server 2005
Product can have duplicates, as long as version numbers are different.
Edit:
If the newest version of a Product ending with '_a' has an incorrect value, I would also like to add an even newer version with the correct value:
If I have: | Product | Version | Value | |---------|---------|-------| | 1 | 1 | 1000 | | 1_a | 1 | 600 | | 1_a | 2 | 1000 |
Then I would like to add: | Product | Version | Value | |---------|---------|-------| | 1_a | 3 | 600 |