SELECT 和 VIEW 内的 SELECT 结果不同
SELECT 语句的结果与 VIEW 内的 SELECT 的结果不同。如何解决问题并从视图中获得相同的结果?
操作表:
+--+---------+--------------+-----------+------+
|id|person_id|action_type_id|currency_id|sum |
+--+---------+--------------+-----------+------+
|1 |1 |1 |1 | 1.00 |
|2 |1 |1 |1 | 5.00 |
|3 |1 |1 |2 |10.00 |
|4 |1 |2 |1 | 2.00 |
|5 |2 |1 |1 |20.00 |
|6 |2 |2 |2 | 5.00 |
+--+---------+--------------+-----------+------+
选择:
SELECT person_id AS p, currency_id AS c,
(
CAST(
COALESCE(
(SELECT SUM(sum) FROM actions WHERE action_type_id=1 AND person_id=p AND currency_id=c)
, 0)
AS DECIMAL(11,2)) -
CAST(
COALESCE(
(SELECT SUM(sum) FROM actions WHERE action_type_id=2 AND person_id=p AND currency_id=c)
, 0)
AS DECIMAL(11,2))
) AS sum
FROM actions
GROUP BY currency_id, person_id
ORDER BY person_id, currency_id;
结果:
+--+--+------+
|p |c |sum |
+--+--+------+
|1 |1 | 4.00 |
|1 |2 |10.00 |
|2 |1 |20.00 |
|2 |2 |-5.00 |
+--+--+------+
在视图内选择:
CREATE VIEW p_sums AS
SELECT person_id AS p, currency_id AS c,
(
CAST(
COALESCE(
(SELECT SUM(sum) FROM actions WHERE action_type_id=1 AND person_id=p AND currency_id=c)
, 0)
AS DECIMAL(11,2)) -
CAST(
COALESCE(
(SELECT SUM(sum) FROM actions WHERE action_type_id=2 AND person_id=p AND currency_id=c)
, 0)
AS DECIMAL(11,2))
) AS sum
FROM actions
GROUP BY currency_id, person_id
ORDER BY person_id, currency_id;
SELECT * FROM p_sums;
结果:
+--+--+------+
|p |c |sum |
+--+--+------+
|1 |1 |29.00 |
|1 |2 |29.00 |
|2 |1 |29.00 |
|2 |2 |29.00 |
+--+--+------+
Result from SELECT statement is different from the result from SELECT inside VIEW. How to fix the problem and get the same result from in view?
actions table:
+--+---------+--------------+-----------+------+
|id|person_id|action_type_id|currency_id|sum |
+--+---------+--------------+-----------+------+
|1 |1 |1 |1 | 1.00 |
|2 |1 |1 |1 | 5.00 |
|3 |1 |1 |2 |10.00 |
|4 |1 |2 |1 | 2.00 |
|5 |2 |1 |1 |20.00 |
|6 |2 |2 |2 | 5.00 |
+--+---------+--------------+-----------+------+
select:
SELECT person_id AS p, currency_id AS c,
(
CAST(
COALESCE(
(SELECT SUM(sum) FROM actions WHERE action_type_id=1 AND person_id=p AND currency_id=c)
, 0)
AS DECIMAL(11,2)) -
CAST(
COALESCE(
(SELECT SUM(sum) FROM actions WHERE action_type_id=2 AND person_id=p AND currency_id=c)
, 0)
AS DECIMAL(11,2))
) AS sum
FROM actions
GROUP BY currency_id, person_id
ORDER BY person_id, currency_id;
Result:
+--+--+------+
|p |c |sum |
+--+--+------+
|1 |1 | 4.00 |
|1 |2 |10.00 |
|2 |1 |20.00 |
|2 |2 |-5.00 |
+--+--+------+
select inside view:
CREATE VIEW p_sums AS
SELECT person_id AS p, currency_id AS c,
(
CAST(
COALESCE(
(SELECT SUM(sum) FROM actions WHERE action_type_id=1 AND person_id=p AND currency_id=c)
, 0)
AS DECIMAL(11,2)) -
CAST(
COALESCE(
(SELECT SUM(sum) FROM actions WHERE action_type_id=2 AND person_id=p AND currency_id=c)
, 0)
AS DECIMAL(11,2))
) AS sum
FROM actions
GROUP BY currency_id, person_id
ORDER BY person_id, currency_id;
SELECT * FROM p_sums;
Result:
+--+--+------+
|p |c |sum |
+--+--+------+
|1 |1 |29.00 |
|1 |2 |29.00 |
|2 |1 |29.00 |
|2 |2 |29.00 |
+--+--+------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能不这样做吗:
即摆脱子查询,只建立一个单一的总和(使action_type_id 2值为负)
Can you not do:
I.e. get rid of the subqueries, and just build a single sum up (making action_type_id 2 values negative)