I'm having issues using (T-)SQL converting data that is given in this format...
Group Column Value ValidFrom ValidTo
1 LowerBorder 150000 25.09.2018 31.12.2999
1 LowerBorder 0 13.09.2018 25.09.2018
1 LowerBorder 150000 08.09.2018 13.09.2018
1 LowerBorder 0 07.09.2018 08.09.2018
1 UpperBorder 450000 08.09.2018 31.12.2999
1 UpperBorder 0 07.09.2018 08.09.2018
2 LowerBorder 5 08.09.2018 31.12.2999
2 UpperBorder 7 08.09.2018 31.12.2999
to this format:
Group LowerBorder UpperBorder ValidFrom ValidTo
2 5 7 08.09.2018 31.12.2999
1 150000 450000 25.09.2018 31.12.2018
1 0 450000 13.09.2018 25.09.2018
1 150000 450000 08.09.2018 13.09.2018
1 0 0 07.09.2018 08.09.2018
To explain, let's have a look at the two lines in Group 1 that both have a "ValidFrom" date of 07.09.2018 in the first table. Putting these together, we get the line at the bottom of the second table. This is the easy case because they have the same "ValidTo" date.
However, if you look at the two lines in Group 1 that have a date of 08.09.2018 -> one of them has a ValidTo date of 13.09.2018 and the other has a ValidTo date of 31.12.2999. So the value of the second line (which relates to "UpperBorder") is valid "until the end" but there will be more lines for the "LowerBorder" before "the end". I'm having a hard time creating the lines in table 2 because of that.
I'm pretty sure I have to use the PIVOT-function as the second step but I'm missing the first step... I have a hunch that I have to somehow create new lines for combinations of "Column" and "ValidFrom" first and then copy the values to the new lines... but I might be mistaken.
Ok, I am not sure if this is going to work...
select *
from Table1 a
pivot
(
min([Value])
FOR [Column] in (LowerBorder, UpperBorder)
) as b
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.