如何使用查询结果进行计算?

时间:2015-05-15 16:09:38

标签: sql

我必须通过更改日期范围重新运行相同的查询。

我必须从查询中划分答案并获得一个百分比,如(1/2)* 100。

我该如何实现?这是一个返回数字的查询:

<not set>

1 个答案:

答案 0 :(得分:0)

由于两个查询都返回一行,您可以交叉加入它们:

SELECT x / y * 100 AS percent
FROM   (SELECT COUNT (DISTINCT a.ST_NUM) AS x
        FROM   table2 b, 
               table1 a,
               table 3 c
        WHERE  b.VEND_CD IN ($vendorCD1) AND
               b.ITM_CD_1 = ($ITMCD) AND
               b.area_num = ($area) AND
               b.area_num = a.area_num AND
               b.itm_cd_2 = a.itm_cd_2 AND
               a.week_end = c.week_end AND
               c.week_end BETWEEN ($startdate) AND ($enddate)) t1
CROSS JOIN (SELECT COUNT (DISTINCT a.ST_NUM) AS y
            FROM   table2 b, 
                   table1 a,
                   table 3 c
            WHERE  b.VEND_CD IN ($vendorCD1) AND
                   b.ITM_CD_1 = ($ITMCD) AND
                   b.area_num = ($area) AND
                   b.area_num = a.area_num AND
                   b.itm_cd_2 = a.itm_cd_2 AND
                   a.week_end = c.week_end AND
                   c.week_end BETWEEN ($startdate2) AND ($enddate2)) t2