在apllying时获得意外结果加入SQL查询

时间:2016-03-26 16:18:54

标签: mysql sql

我从4个不同的表中获取数据:

  1. lead
  2. payu_transactions
  3. corporate_user_rides
  4. corporate_users
  5. 还有一些条件:

    1. 用户游乐设施应该大于0
    2. 应该有一些注册和活跃用户
    3. 会有一段时间
    4. 我已经编写了一些SQL查询,但我没有得到预期的结果 - 问题在于游乐设施数和用户数。

      例如 -

      让我们说企业x实际上有38个游乐设施和23个用户,但它显示了7866个游乐设施和7866个用户。

      另一家公司实际上有18个游乐设施和5个用户,但它显示了90个游乐设施和90个用户。

      任何人都可以帮忙,我不知道我在这里做什么。

      我试过了 -

      查询

      SELECT l.id             AS leadId,
             l.corporate_id   AS CorporateID,
             "P-1"            AS priority,
             l.source,
             l.user_name      AS FirstName,
             l.user_name      AS LastName,
             l.corporate_name AS corpName,
             l.user_mail_id   AS email,
             l.phone_number   AS phone,
             l.created_At     AS leadCreation,
             l.comments,
             Count(CU.id)     AS users,
             Count(CUR.id)    AS rides,
             PUT.amount       AS payment
      FROM   leads l
             LEFT JOIN payu_transactions PUT
                    ON l.user_mail_id = PUT.email
             LEFT JOIN corporate_user_rides CUR
                    ON l.corporate_id = CUR.corporate_id
             LEFT JOIN corporate_users CU
                    ON l.corporate_id = CU.corporate_id
      WHERE  l.created_at BETWEEN '2015-03-16 12:00:00' AND '2016-03-17 12:00:00'
      GROUP  BY l.user_mail_id
      HAVING Count(CUR.id) > 0
             AND Count(CU.id) > 0
             AND Count(CASE
                         WHEN CU.status IN ( 'active' ) THEN 1
                       END) > 0;
      

      帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用Count (Distinct column.name)代替Count(column.name)