有没有一种方法可以重写此语句而无需子查询?

时间:2019-12-10 20:26:03

标签: sql

我试图将4个查询合并为一个月度报告,这样就不必单独运行它们。我们的内部会计软件似乎不支持子查询,因此该语句不起作用。

select left(salesgroupcode,4) as "Sales Group",
       count(Number_of_products),
       count(Number_of_discontinued),
       count(Number_not_uploaded),
       count(Number_sitting)
from (select
       case when quantityavailable > 1 then 1 end Number_of_products,
       case when quantityavailable > 1 and discontinued = true then 1 end Number_of_discontinued,
       case when quantityavailable > 1 and z_datefirstuploaded is null then 1 end Number_not_uploaded,
       case when quantityavailable > 1 and z_datefirstuploaded is null and dateoflastsale <= '01/01/2019' then 1 end Number_sitting
     from icprod
 ) icprod

我没有有关软件允许的特定规则等的其他信息,因此我很乐意尝试任何事情。

先谢谢了。 感谢您的帮助

2 个答案:

答案 0 :(得分:3)

是的,有可能:

this.kill = function () {
            invaderGroup3D.children.forEach(function (invader) {
                if(invader.position.x <= -250){
                    invaderGroup3D.remove(invader);
                }

            });
        }

答案 1 :(得分:2)

只需使用条件聚合:

select sum(case when quantityavailable > 1 then 1 else 0 end ) as Number_of_products,
       sum(case when quantityavailable > 1 and discontinued = true then 1 else 0 end) as Number_of_discontinued,
       sum(case when quantityavailable > 1 and z_datefirstuploaded is null then 1 else 0 end) as Number_not_uploaded,
       sum(case when quantityavailable > 1 and z_datefirstuploaded is null and dateoflastsale <= '2019-01-01' then 1 else 0 end) as Number_sitting
from icprod;

请注意,我将日期常数更改为ISO 8601标准格式。在某些数据库中,您可能需要在其之前加上date

这可以简化为:

select count(*) as Number_of_products,
       sum(case when discontinued = true then 1 else 0 end) as Number_of_discontinued,
       sum(case when z_datefirstuploaded is null then 1 else 0 end) as Number_not_uploaded,
       sum(case when z_datefirstuploaded is null and dateoflastsale <= '2019-01-01' then 1 else 0 end) as Number_sitting
from icprod
where quantityavailable > 1