I have a wide table that looks like this:
Table Windspeed
Timestamp A-004 A-005a A-006a ... (column 161) E-009
09-Apr-19 00:00:00 13.09 13.95 10.16 11.71
09-Apr-19 00:05:00 12.17 11.98 12.54 10.66
I need to generate a new table in the same format, but replace the column values from a lookup table that looks like this:
Table Powercurve
WindSpeedBin Power_WdSpd Turb_Num
...
9.5 1216.9 1
10 1362.9 1
10.5 1476.3 1
11 1597 1
11.5 1701.5 1
12 1798.3 1
12.5 1828.5 1
13 1840 1
13.5 1846.1 1
...
9.5 1211.9 2
10 1342.9 2
10.5 1436.3 2
11 1517 2
11.5 1691.5 2
12 1713.3 2
12.5 1801.5 2
13 1833.1 2
13.5 1846.1 2
...
And finally, a third table that looks like this:
Table NameLookup
TName Tag TNum
A-004 A04 1
A-005a A05 2
A-006a A06 3
A-008a A08 4
A-009 A09 5
...
E-009 E09 162
I need to generate a table that takes the values from table 1 (Windspeed), looks up the values in table 2 (Powercurve) (round to the nearest .5 to match WindSpeedBin), using the column names to lookup the Turb_Num value. Any ideas how to go about this? the end result would look something like this:
Table Expected_Production
Timestamp A-004 A-005a A-006a ... (column 161) E-009
09-Apr-19 00:00:00 1840 1846.1 1528.5 1780.6
09-Apr-19 00:05:00 1828.5 1713.3 1843.9 1614.5
...
This is for Microsoft SQL Server 2017
Best Answer
This should work for you. At least for the entries in the PowerCurve table that I copied over, it works correctly. This should get you close. See comments in the code for more details.
Assistance from https://stackoverflow.com/questions/9873990/round-to-5-or-1-0-in-sql on how to round to nearest .5/0.
Get the data