1.以下查询返回一个值:

select x , y,(x/cast(y as float)*100) from
 (select count(distinct t1.ST_NUM) as x
    from table2 t2,
           table1 t1,
             table t3
where
  t2.CD in ($CD1)
  and t2.ITM_CD=($ITM_CD)
  and t2.div=($div)
  and t2.div= t1.div
   and t2.scn_cd = t1._scn_cd
   and t1.week_end =t3.week_end
   and t3.week_end between ($startdate_1)  and  ($enddate_1))a1
   cross join
   (select count(distinct t1.ST_NUM) as y
         from from table2 t2,
                   table1 t1,
                   table t3
where
  t2.CD in ($CD1)
  and t2.ITM_CD=($ITM_CD)
  and t2.div=($div)
  and t2.div= t1.div
   and t2.scn_cd = t1._scn_cd
   and t1.week_end =t3.week_end
   and t3.week_end between ($startdate_2)  and  ($enddate_2))a2


2.以上查询返回的值必须按如下方式使用
如果值介于a和b之间,则执行此操作
如果值介于d和c之间,则执行此操作
我了解我必须使用案例声明。我不确定如何将值作为输入传递给要编写的新sql脚本

谢谢你的帮助

最佳答案

修改查询的开头,如下所示:

宣告@x int
宣告@y int
宣告@z float

从中选择@ x = x,@ y = y,@ z =(x / cast(y为float)* 100)
{其余查询不变}

现在,您可以在同一查询中的任何后续SQL语句中引用@ x,@ y和@z。如果您需要能够通过完全不同的查询访问它们,则需要将它们存储在更永久的位置(如表)中。

关于mysql - sql脚本返回的值应用作下一个脚本的输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30598837/

10-09 15:21
查看更多