質問編集履歴

1

ソースを追加しました。何卒よろしくお願いいたします。

2018/02/21 06:00

投稿

theoryxyz
theoryxyz

スコア11

test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,177 @@
17
17
  アドバイスなどいただけますと幸いです。
18
18
 
19
19
  何卒よろしくお願いいたします。
20
+
21
+
22
+
23
+
24
+
25
+ **どうにも先が見えない状況にてソースを掲載させていただきます。
26
+
27
+ 問題点は@current_value_with_point_before_roundの小数点第一位が四捨五入されてしまうことでしたが、ISNULL(@current_point, 0)の@current_pointにNULLでない数値が代入された次の行からは、小数点以下が切り上げられず(0にならず)入力されることが分かりました。
28
+
29
+ 説明不足かもしれませんが取り急ぎ。何卒よろしくお願いいたします。**
30
+
31
+
32
+
33
+ CREATE TABLE [dbo].[t_history]
34
+
35
+ ([code] CHAR(6) NOT NULL
36
+
37
+ ,[date] DATETIME NOT NULL
38
+
39
+ ,[value] MONEY
40
+
41
+ ,[compared_previous_day_yen] MONEY DEFAULT NULL
42
+
43
+ ,[compared_previous_day_percent] DECIMAL(5,2) DEFAULT NULL
44
+
45
+ ,[compared_previous_day_percent_before_round] DECIMAL(6,3) DEFAULT NULL
46
+
47
+ ,[value_with_point] MONEY DEFAULT NULL
48
+
49
+ ,[value_with_point_before_round] DECIMAL(19,9) DEFAULT NULL
50
+
51
+ ,[initial_date] INT
52
+
53
+ ,[point] MONEY
54
+
55
+ CONSTRAINT [pk_t_history] PRIMARY KEY([code], [date]))
56
+
57
+
58
+
59
+ DECLARE current_day CURSOR
60
+
61
+ FOR SELECT [date]
62
+
63
+ ,[value]
64
+
65
+ ,[initial_date]
66
+
67
+ ,[point]
68
+
69
+ FROM [dbo].[t_history]
70
+
71
+ ORDER BY [code]
72
+
73
+ ,[date]
74
+
75
+
76
+
77
+ DECLARE @current_date DATETIME
78
+
79
+ ,@current_value MONEY
80
+
81
+ ,@previous_value MONEY
82
+
83
+ ,@current_initial_date INT
84
+
85
+ ,@current_point MONEY
86
+
87
+ ,@previous_point MONEY
88
+
89
+ ,@current_value_with_point MONEY
90
+
91
+ ,@current_value_with_point_before_round DECIMAL(19,9)
92
+
93
+ ,@previous_value_with_point MONEY
94
+
95
+ ,@previous_value_with_point_before_round DECIMAL(19,9)
96
+
97
+
98
+
99
+ OPEN current_day
100
+
101
+
102
+
103
+ FETCH NEXT FROM current_day
104
+
105
+ INTO @current_date
106
+
107
+ ,@current_value
108
+
109
+ ,@current_initial_date
110
+
111
+ ,@current_point
112
+
113
+
114
+
115
+ WHILE @@FETCH_STATUS = 0
116
+
117
+ BEGIN
118
+
119
+ IF @current_initial_date = YEAR(@current_date) * 10000 + MONTH(@current_date) * 100 + DAY(@current_date)
120
+
121
+ BEGIN
122
+
123
+ SET @current_value_with_point_before_round = @current_value
124
+
125
+ SET @current_value_with_point = @current_value
126
+
127
+ UPDATE [dbo].[t_history]
128
+
129
+ SET [compared_previous_day_yen] = NULL
130
+
131
+ ,[compared_previous_day_percent] = NULL
132
+
133
+ ,[value_with_point] = @current_value_with_point
134
+
135
+ ,[value_with_point_before_round] = @current_value_with_point_before_round
136
+
137
+ WHERE CURRENT OF current_day
138
+
139
+ END
140
+
141
+ ELSE
142
+
143
+ BEGIN
144
+
145
+ SET @current_value_with_point_before_round = @previous_value_with_point_before_round * ((1.000000000 * (@current_value + ISNULL(@current_point, 0))) / @previous_value)
146
+
147
+ SET @current_value_with_point = ROUND(@current_value_with_point_before_round, 0)
148
+
149
+ UPDATE [dbo].[t_history]
150
+
151
+ SET [compared_previous_day_yen] = @current_value - @previous_value
152
+
153
+ ,[compared_previous_day_percent] = ROUND((1.000000000 * @current_value / @previous_value - 1) * 100, 2)
154
+
155
+ ,[compared_previous_day_percent_before_round] = (1.000000000 * @current_value / @previous_value - 1) * 100
156
+
157
+ ,[value_with_point] = @current_value_with_point
158
+
159
+ ,[value_with_point_before_round] = @current_value_with_point_before_round
160
+
161
+ WHERE CURRENT OF current_day
162
+
163
+ END
164
+
165
+
166
+
167
+ SET @previous_value = @current_value
168
+
169
+ SET @previous_point = @current_point
170
+
171
+ SET @previous_value_with_point = @current_value_with_point
172
+
173
+ SET @previous_value_with_point_before_round = @current_value_with_point_before_round
174
+
175
+
176
+
177
+ FETCH NEXT FROM current_day
178
+
179
+ INTO @current_date
180
+
181
+ ,@current_value
182
+
183
+ ,@current_initial_date
184
+
185
+ ,@current_point
186
+
187
+ END
188
+
189
+
190
+
191
+ CLOSE current_day
192
+
193
+ DEALLOCATE current_day