IIF 或 CASE 子句中的 MONEY 数据类型 CAST 问题
MONEY Datatype CAST issue in IIF or CASE clause
这是一个生成正确输出的示例查询示例:
DECLARE @T TABLE (
keydate DATE,
price MONEY
);
INSERT INTO @T(keydate, price) VALUES
('2014-07-23', 32464),
('2014-07-26', 32478),
('2014-07-27', 32473)
;WITH
cte_tbl (keydate, price, prev_price1)
AS (
SELECT TOP(1)
T.keydate,
T.price,
T.price AS prev_price
FROM @T AS T
ORDER BY T.keydate
UNION ALL
SELECT
T.keydate,
T.price,
T.prev_price
FROM (
SELECT
TT.keydate,
TT.price,
IIF(ABS(TT.price - cte_tbl.prev_price1) < 10, cte_tbl.prev_price1, TT.price) AS prev_price,
ROW_NUMBER() OVER(ORDER BY TT.keydate) AS ROWNUM
FROM @T AS TT
INNER JOIN cte_tbl ON cte_tbl.keydate < TT.keydate
) AS T
WHERE T.ROWNUM = 1
)
SELECT *
FROM cte_tbl
ORDER BY keydate
正确的输出是:
keydate price prev_price1
2014-07-23 32464.00 32464.00
2014-07-26 32478.00 32478.00
2014-07-27 32473.00 32478.00
如果我更改为带点的 MONEY 的十进制价格:
INSERT INTO @T(keydate, price) VALUES
('2014-07-23', 324.64),
('2014-07-26', 324.78),
('2014-07-27', 324.73)
输出将是错误的:
keydate price prev_price1
2014-07-23 324.64 324.64
2014-07-26 324.78 324.64
2014-07-27 324.73 324.64
价格实际上在CTE声明中,但它不拒绝乱七八糟的。
我需要将它们存储为正常价格并具有如上所述的正确输出行为:
('2014-07-23', 324.64),
('2014-07-26', 324.78),
('2014-07-27', 324.73)
输出应该是:
keydate price prev_price1
2014-07-23 32464.00 324.64
2014-07-26 32478.00 324.78
2014-07-27 32473.00 324.78
感谢您的帮助!
因为32464
和32478' is more than 10, but the difference between
324.64and
324.78`没有区别。更改此行:
IIF(ABS(TT.price - cte_tbl.prev_price1) < 10, cte_tbl.prev_price1, TT.price)
至:
IIF(ABS(TT.price - cte_tbl.prev_price1) < 0.10, cte_tbl.prev_price1, TT.price)
如果要将价格降低 10 倍,则需要将比较值降低 10 倍。
这是一个生成正确输出的示例查询示例:
DECLARE @T TABLE (
keydate DATE,
price MONEY
);
INSERT INTO @T(keydate, price) VALUES
('2014-07-23', 32464),
('2014-07-26', 32478),
('2014-07-27', 32473)
;WITH
cte_tbl (keydate, price, prev_price1)
AS (
SELECT TOP(1)
T.keydate,
T.price,
T.price AS prev_price
FROM @T AS T
ORDER BY T.keydate
UNION ALL
SELECT
T.keydate,
T.price,
T.prev_price
FROM (
SELECT
TT.keydate,
TT.price,
IIF(ABS(TT.price - cte_tbl.prev_price1) < 10, cte_tbl.prev_price1, TT.price) AS prev_price,
ROW_NUMBER() OVER(ORDER BY TT.keydate) AS ROWNUM
FROM @T AS TT
INNER JOIN cte_tbl ON cte_tbl.keydate < TT.keydate
) AS T
WHERE T.ROWNUM = 1
)
SELECT *
FROM cte_tbl
ORDER BY keydate
正确的输出是:
keydate price prev_price1
2014-07-23 32464.00 32464.00
2014-07-26 32478.00 32478.00
2014-07-27 32473.00 32478.00
如果我更改为带点的 MONEY 的十进制价格:
INSERT INTO @T(keydate, price) VALUES
('2014-07-23', 324.64),
('2014-07-26', 324.78),
('2014-07-27', 324.73)
输出将是错误的:
keydate price prev_price1
2014-07-23 324.64 324.64
2014-07-26 324.78 324.64
2014-07-27 324.73 324.64
价格实际上在CTE声明中,但它不拒绝乱七八糟的。
我需要将它们存储为正常价格并具有如上所述的正确输出行为:
('2014-07-23', 324.64),
('2014-07-26', 324.78),
('2014-07-27', 324.73)
输出应该是:
keydate price prev_price1
2014-07-23 32464.00 324.64
2014-07-26 32478.00 324.78
2014-07-27 32473.00 324.78
感谢您的帮助!
因为32464
和32478' is more than 10, but the difference between
324.64and
324.78`没有区别。更改此行:
IIF(ABS(TT.price - cte_tbl.prev_price1) < 10, cte_tbl.prev_price1, TT.price)
至:
IIF(ABS(TT.price - cte_tbl.prev_price1) < 0.10, cte_tbl.prev_price1, TT.price)
如果要将价格降低 10 倍,则需要将比较值降低 10 倍。