简体   繁体   English

在应用Join SQL查询时获得意外结果

[英]Getting unexpected result while apllying Join SQL query

I am fetching data from 4 different tables: 我正在从4个不同的表中获取数据:

  1. leads 线索
  2. payu_transactions payu_transactions
  3. corporate_user_rides Corporate_user_rides
  4. corporate_users Corporate_users

And there are some conditions: 并且有一些条件:

  1. user rides should be grater than 0 用户游乐设施应大于0
  2. There should be some number of registered and active users 应该有一定数量的注册用户和活动用户
  3. There will be some time period 会有一段时间

I have written some SQL queries but i am not getting expected result- The problem is with number of rides count and user count. 我已经写了一些SQL查询,但没有得到预期的结果-问题在于游乐设施数和用户数。

For eg- 对于例如

Lets say corporate x actually having 38 rides and 23 users but it's showing 7866 rides and 7866 users. 可以说,企业x实际有38个游乐设施和23个用户,但显示的是7866个游乐设施和7866个用户。

Another corporate y is actually having 18 rides and 5 users but it's showing 90 rides and 90 users. 另一个公司y实际上拥有18个游乐设施和5个用户,但显示90个游乐设施和90个用户。

Can anyone please help, i am not sure what's i am doing here. 谁能帮忙,我不确定我在这里做什么。

I tried this- 我尝试了这个

query 询问

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;

Help will be appreciated. 帮助将不胜感激。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM