sum子查询sql

时间:2015-09-08 16:34:29

标签: sql subquery

我想在子查询中加总百分比:

SELECT id, (
    SELECT COUNT( lead.id ) 
    FROM lead
    INNER JOIN cam c ON lead.cam = c.id
    WHERE c.id = c2.id
) AS leadtotal, (
    SELECT COUNT( landpagevisit.id ) 
    FROM landpagevisit
    WHERE c2.id = landpagevisit.cam
) AS visittotal, (
    SELECT COUNT( lead.id ) 
    FROM lead
    INNER JOIN cam c ON lead.cam = c.id
    WHERE c2.id = lead.cam
) / (
    SELECT COUNT( landpagevisit.id ) 
    FROM landpagevisit
    WHERE c2.id = landpagevisit.cam
) *100 AS conver, (
    SELECT SUM(
        (
            SELECT COUNT( lead.id ) 
            FROM lead
            INNER JOIN cam c ON lead.cam = c.id
            WHERE c.id = lead.cam 
        ) / (
            SELECT COUNT( landpagevisit.id ) 
            FROM landpagevisit
        )
    )
    FROM landpagevisit
    WHERE landpagevisit.cat = c2.cat
) AS totalconver
FROM cam c2
WHERE cat =1

我明白了:

--------------------------------------------------------------------
|cat| camid |countvisitweek|countvisit|countlead|conver|totalconver|
--------------------------------------------------------------------
|1  |25     |6             |6         |378      |63.000|62399      |
--------------------------------------------------------------------
|1  |23     |139           |139       |406      |2.9209|62399      |
--------------------------------------------------------------------
|1  |43     |34            |34        |51       |1.5000|62399      |
--------------------------------------------------------------------
|1  |61     |38            |38        |50       |1.3158|62399      |
--------------------------------------------------------------------

我希望总的转换为融合的和,现在是62399,它的值是68.737。 我需要这将汇总收敛,而不是与所有的线索和访问融合(不是这样:885/217 = 4.078341014)。 你有什么建议吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

看起来您正在以不同方式计算收敛和总转化。

CONVER:

  (SELECT COUNT( lead.id ) FROM lead INNER JOIN cam c ON lead.cam = c.id WHERE c2.id = lead.cam) 
    / 
  (SELECT COUNT( landpagevisit.id ) FROM landpagevisit WHERE c2.id = landpagevisit.cam) *100 AS conver, 

totalconver:

(SELECT SUM( 
  (SELECT COUNT( lead.id ) FROM lead INNER JOIN cam c ON lead.cam = c.id WHERE c.id = lead.cam ) 
  / 
  (SELECT COUNT( landpagevisit.id ) FROM landpagevisit)) FROM landpagevisit WHERE landpagevisit.cat = c2.cat ) AS totalconver 

如果我比较两个子查询之间的第二个count():

conver:
(SELECT COUNT( landpagevisit.id ) FROM landpagevisit WHERE c2.id = landpagevisit.cam)

totalconver:
(SELECT COUNT( landpagevisit.id ) FROM landpagevisit)

这似乎是一个太广泛的网络。试试这个:

(
    SELECT SUM
    ( 
      (SELECT COUNT( lead.id ) FROM lead INNER JOIN cam c ON lead.cam = c.id WHERE c.id = lead.cam ) 
        / 
      (SELECT COUNT( landpagevisit.id ) FROM landpagevisit WHERE c2.id = landpagevisit.cam)
    ) FROM landpagevisit WHERE landpagevisit.cat = c2.cat
  ) AS totalconver

答案 1 :(得分:0)

  

1242 - 子查询返回超过1行

     

这是这写的......   为此:

(
    SELECT SUM
    ( 
      (SELECT COUNT( lead.id ) FROM lead INNER JOIN cam c ON lead.cam = c.id WHERE c.id = lead.cam ) 
        / 
      (SELECT COUNT( landpagevisit.id ) FROM landpagevisit WHERE c2.id = landpagevisit.cam)
    ) FROM landpagevisit WHERE landpagevisit.cat = c2.cat
  ) AS totalconver