Question

I need to create TSQL query which return column based on number of days on the month, and if the day is weekend then use last working days value.

I have table where we add price for each security for every day (working day) in the rates table. We have 1700 securities in the table, So we add 1700 security price every single working day.

We don't add price for weekend

Table looks like following..........

**SECID           DATE           RATE**

AN8068571086            01/01/2014              89.84
AT0000606306            01/01/2014          0.68
AT0000818802            01/01/2014          105.00
BBG000BNSZP1            01/01/2014            0.00
BBG005MWN105            01/01/2014            0.00
BE0003565737            01/01/2014           41.13
BE0003796134            01/01/2014            0.04
BE0117584202            01/01/2014            1.02
BE0119198829            01/01/2014            1.00
BE0374557404            01/01/2014            1.13
BE0932894455            01/01/2014            0.30
BE0934378747            01/01/2014            1.02
BE0934531337            01/01/2014            1.00
BE0934924383            01/01/2014            0.01
BE0934986036            01/01/2014            1.49
BMG5876H1051            01/01/2014           14.52
BMG6260K1264            01/01/2014            0.02
BMG6260K1264XHKG        01/01/2014            0.24
BMG657731060            01/01/2014            9.77
CA00829Q1019            01/01/2014           55.50
AN8068571086            02/01/2014                89.84
AT0000606306            02/01/2014            0.68
AT0000818802            02/01/2014          105.00
BBG000BNSZP1            02/01/2014            0.00
BBG005MWN105            02/01/2014            0.00
BE0003565737            02/01/2014           41.13
BE0003796134            02/01/2014            0.04
BE0117584202            02/01/2014            1.02
BE0119198829            02/01/2014            1.00
BE0374557404            02/01/2014                1.13
BE0932894455            02/01/2014            0.30
BE0934378747            02/01/2014            1.02
BE0934531337            02/01/2014            1.00
BE0934924383            02/01/2014            0.01
BE0934986036            02/01/2014            1.49
BMG5876H1051            02/01/2014           14.52
BMG6260K1264            02/01/2014            0.02
BMG6260K1264XHKG        02/01/2014            0.24
BMG657731060            02/01/2014            9.77
CA00829Q1019            02/01/2014           55.50





SECID        01/01/2014 02/01/2014 03/01/2014 04/01/2014 05/01/2014 06/01/2014........ 31/01/2014
A                                           
AN8068571086            89.84   89.12   88.56   88.56   88.56   87.55
AT0000606306            0.68    0.68    0.68    0.68    0.68    0.68
AT0000818802            105 107 105 105 105 102.5
BBG000BNSZP1                                
BBG005MWN105            0   0   0   0   0   0
BE0003565737            41.135  40.4    41.005  41.005  41.005  41.27
BE0003796134            0.04    0.04    0.04    0.04    0.04    0.04
BE0117584202            1.024   1.024   1.024   1.024   1.024   1.024
BE0119198829            1   1   1   1   1   1
BE0374557404            1.13279 1.13281 1.13303 1.13303 1.13303 1.13428
BE0932894455            0.3062  0.3062  0.3062  0.3062  0.3062  0.3062
BE0934378747            1.02217 1.02089 1.0192  1.0192  1.0192  1.02051
BE0934531337            1   1   1   1   1   1
BE0934924383            0.0114254   0.0114504   0.0114628   0.0114628   0.0114628   0.0114888
BE0934986036            1.49556 1.49624 1.49735 1.49735 1.49735 1.50383
BMG5876H1051            14.52   14.09   14.08   14.08   14.08   14.57
BMG6260K1264            0.02    0.02    0.03    0.03    0.03    0.0222
BMG6260K1264XHKG        0.241   0.24    0.235   0.235   0.235   0.237
BMG657731060            9.77    9.57    9.56    9.56    9.56    9.76
CA00829Q1019            55.5    56.75   56.75   56.75   56.75   56.75
CA0329001022            0.015   0.015   0.015   0.015   0.015   0.015
CA0679011084            17.48   18.18   18.42   18.42   18.42   18.32
CA067901184                                 
CA09228F1036            7.4 7.61    7.54    7.54    7.54    7.84
CA1319111094            1.78    1.8 1.77    1.77    1.77    1.78
CA2768551038            0.075   0.075   0.075   0.075   0.075   0.075
CA29250N1050            43.71   43.44   43.29   43.29   43.29   42.7
CA3359341052            19.1    19.13   19.03   19.03   19.03   19.05
CA3809564097            21.67   22.57   22.66   22.66   22.66   22.45
CA42979J1075            1.39    1.39    1.39    1.39    1.39    1.39
CA5503721063            4.59    4.62    4.66    4.66    4.66    4.58
CA6565685089            0.0081  0.0086  0.0081  0.0081  0.0081  0.0096
CA656585089             0.0081  0.0086  0.0081  0.0081  0.0081  0.0096
CA71921K1021            0.045   0.045   0.045   0.045   0.045   0.045

I need to create query which returns value in the following way. And Number of Columns returned will depend on number of days (including weekend) in that months.

We can't use pivot as database compatibility level is SQL 2000. I am using SQL 2005 server

As you can see in the result I am getting one column for each day of month of January and days whicg falls on weekend (4th and 5th January) it uses last working days (3rd January) price.

I have tried but can't get my head round to this issue. I don't want to hardcode each (day) columns, as in some month we will 30 days or 28 days.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top