简体   繁体   中英

Getting unexpected result while apllying Join SQL query

I am fetching data from 4 different tables:

  1. leads
  2. payu_transactions
  3. corporate_user_rides
  4. corporate_users

And there are some conditions:

  1. user rides should be grater than 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.

For eg-

Lets say corporate x actually having 38 rides and 23 users but it's showing 7866 rides and 7866 users.

Another corporate y is actually having 18 rides and 5 users but it's showing 90 rides and 90 users.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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