sql查询如何在一个表中使用同一列计算值?

sql查询如何在一个表中使用同一列计算值?,sql,oracle,Sql,Oracle,我想计算一个名为table_name的表中名为value的同一列: 假设我需要在20180201和20180228之间计算value的type=111或222或333/value的type=444,我使用SQL查询如下: select t.code,t.date,t.remark,t1.value/t2.value as val from table_name t right join ( select sum(t.value) fzqsz, t.code from ta

我想计算一个名为table_name的表中名为value的同一列:

假设我需要在20180201和20180228之间计算value的type=111或222或333/value的type=444,我使用SQL查询如下:

select t.code,t.date,t.remark,t1.value/t2.value as val
from table_name t
    right join (
        select sum(t.value) fzqsz, t.code from table_name t
        WHERE remark = '111' OR remark = '222' OR remark = '333'
        group by t.code,t.date
    ) t1 on t.code = t1.code
    left join (
        select sum(t.value) value,t.code from table_name t
        WHERE remark = '444'
        group by t.code,t.date
    ) t2 on t1.code = t2.code
    -- if I put below line in `SQL query` I get nothing(0 results)
    -- where t.code='00001' and t.date >='20180201' and date <= '20180228' 
    code         date              remark                  val
1   00001   2018-02-25 00:00:00.0    111                0.00002929
2   00001   2018-02-25 00:00:00.0    222                0.00002977
3   00002   2018-02-25 00:00:00.0    333                0.00002917
4   00003   2018-02-25 00:00:00.0    444                0.00002987
3   00001   2018-02-25 00:00:00.0    555                0.00002917
4   00002   2018-02-25 00:00:00.0    666                0.00002987

SQL查询正确吗?我确信我的SQL查询有问题。非常感谢您的建议。

使用内部连接和多重连接,或者可以在

您可以在以下情况下使用case:

 select (sum(case when remark in( 111,222 ,333) then t.value else 0 end)/
 sum(case when remark=444 then t.value else 0 end)) as val,
 t.code,t.date from table_name t
 where t.code='00001' and t.date >='20180201' and date <= '20180228'
  group by t.code,t.date

您可以使用select case,而不是合并相同的表

select t2.code, t2.date, t2.remark, t3.val/t3.val2 from 
table_name t2
inner join (
    select t.code, sum(case when t.remark in ('111', '222', '333') then 1 else 0 end) as val
        , sum(case when t.remark in ('444') then 1 else 0 end) as val2
    from table_name t
    where t.code = '00001' and t.date between '20180201' and '20180228'
    group by t.code) t3 on t3.code = t2.code
where t2.remark in ('111', '222', '333', '444')
阅读评论! 艾尔莎,你只对答案做出反应是没有意义的。这两张海报只是猜测你可能想要实现什么。在你的要求下有一些问题,你到目前为止还没有做出反应。您使用的真的是Oracle吗?表的键是什么,即什么唯一地标识一行?为什么在子查询中按日期分组?您希望得到什么样的结果行


由于您似乎没有阅读评论,我将此作为一个答案发布。

我没有您的数据要测试,但是-外部连接闻起来像是罪魁祸首。您确定您在使用Oracle吗?您的创建表在Oracle中无效。您可以按代码和日期分组,但只能在子查询中选择代码。因此,您不知道所选择的值实际上指的是什么。我想你只需要一个t1和一个t2的和,但是什么日期?在t日期?还是给出了整个日期范围?或者不考虑日期?顺便说一下:我会避免右外连接。它们很难阅读。在我看来,将它们与左外连接混合甚至是一件可怕的事情。我必须承认,我必须尝试这个查询,以确定它的作用。t1似乎是查询中的主要数据源,您只在查找匹配项的地方添加t和t2。但是不应该是t1和t2试图连接的主表吗?至于我不知道为什么所有的标记555和666都出现了。是的,因为没有限制。您正在选择所有t,t.code='00001'和t.date>='20180201'和date Hi,谢谢,我也尝试了“111'、'222'、'333'中的内部连接和注释,结果仍然包含其他标记555或666,我认为我的SQL查询是错误的…….嗨,非常感谢再次编辑,只有当我删除和t.date>='20180201'和date时,我才得到结果。我需要将每天的值相乘,然后-1才能得到周期值吗?@Elsa你能分享你的表格数据或摆弄一下吗嗨,我们好像不能把t.remark放在[列]中从表格中选择列,非常感谢你的帮助,现在它工作得很好Hi,非常感谢您为我重新编写了一个SQL查询,很遗憾,它不起作用,太奇怪了!!!结果仍然包含其他的标记555或666,我们计算的val都是什么0@Elsa,我在‘111’、‘222’、‘333’、‘444’中加上t2。备注
 select (sum(case when remark in( 111,222 ,333) then t.value else 0 end)/
 sum(case when remark=444 then t.value else 0 end)) as val,
 t.code,t.date from table_name t
 where t.code='00001' and t.date >='20180201' and date <= '20180228'
  group by t.code,t.date
select t2.code, t2.date, t2.remark, t3.val/t3.val2 from 
table_name t2
inner join (
    select t.code, sum(case when t.remark in ('111', '222', '333') then 1 else 0 end) as val
        , sum(case when t.remark in ('444') then 1 else 0 end) as val2
    from table_name t
    where t.code = '00001' and t.date between '20180201' and '20180228'
    group by t.code) t3 on t3.code = t2.code
where t2.remark in ('111', '222', '333', '444')