2015-04-16 4 views
1

У меня есть 2 SQL запросов:Как присоединиться к 2 запросов

SELECT accounts.assigned_user_id, jt0.user_name assigned_user_name, 
SUM(IF(opp.opportunity_type='2', IFNULL(opp.amount,0), 0)) AS amt_revenue, 
SUM(IF(opp.opportunity_type='4', IFNULL(opp.amount,0), 0)) AS amt_return 
FROM accounts 
LEFT JOIN users jt0 
ON jt0.id=accounts.assigned_user_id AND jt0.deleted=0 AND jt0.deleted=0 
LEFT JOIN accounts_opportunities AS a_o 
ON a_o.account_id = accounts.id AND a_o.deleted=0 
LEFT JOIN opportunities AS opp 
ON (opp.id = a_o.opportunity_id AND opp.deleted=0 AND opp.sales_stage = 'Closed Won' AND opp.opportunity_type IN('2', '4')) 
WHERE accounts.deleted=0 
GROUP BY accounts.assigned_user_id, jt0.user_name 
ORDER BY SUM(IFNULL(opp.amount,0)) DESC 

И:

SELECT accounts.assigned_user_id, 
SUM(IFNULL(accounts_collections.amount,0)) AS amount 
FROM accounts 
LEFT JOIN accounts_collections ON(accounts_collections.account_id = accounts.id AND accounts_collections.deleted=0) 
GROUP BY accounts.assigned_user_id 

Как я могу присоединиться к 2 запросы выше?

+0

Присоединяйтесь к ним как? Какую продукцию вы пытаетесь получить, объединив их? – Matt

+0

Какой выход вы хотите получить? Обратите внимание, что вы не можете использовать 'UNION' между этими двумя запросами, как сейчас, потому что вы выбрали 4 столбца в запросе 1 и 2 столбца в запросе 2. –

+0

Спасибо @TimBiegeleisen Я хочу получить amt_revenue, amt_return и сумму для accounts.assigned_user_id. – Kxx

ответ

1

Возможно, вы сможете поместить второй запрос в подзадачу. Есть два способа сделать это. В разделе выбора или в разделе формы. Не уверен, что MySQL принимает, но оба работают в других SQL-машинах.

SELECT 
    accounts.assigned_user_id, 
    jt0.user_name assigned_user_name, 
    SUM(IF(opp.opportunity_type='2', 
     IFNULL(opp.amount,0), 0) 
    ) AS amt_revenue, 
    SUM(IF(opp.opportunity_type='4', 
     IFNULL(opp.amount,0), 0) 
    ) AS amt_return, 
    (
     SELECT SUM(IFNULL(accounts_collections.amount,0)) AS amount 
     FROM accounts 
      LEFT JOIN accounts_collections ON(accounts_collections.account_id = accounts.id AND accounts_collections.deleted=0) 
     GROUP BY accounts.assigned_user_id 
    ) AS amount 
FROM accounts 
    LEFT JOIN users jt0 
     ON jt0.id=accounts.assigned_user_id AND jt0.deleted=0 AND jt0.deleted=0 
    LEFT JOIN accounts_opportunities AS a_o 
     ON a_o.account_id = accounts.id AND a_o.deleted=0 
    LEFT JOIN opportunities AS opp 
     ON (opp.id = a_o.opportunity_id AND opp.deleted=0 AND opp.sales_stage = 'Closed Won' AND opp.opportunity_type IN('2', '4')) 
WHERE accounts.deleted=0 
GROUP BY accounts.assigned_user_id, jt0.user_name 
ORDER BY SUM(IFNULL(opp.amount,0)) DESC 



SELECT 
    accounts.assigned_user_id, 
    jt0.user_name assigned_user_name, 
    SUM(IF(opp.opportunity_type='2', 
     IFNULL(opp.amount,0), 0) 
    ) AS amt_revenue, 
    SUM(IF(opp.opportunity_type='4', 
     IFNULL(opp.amount,0), 0) 
    ) AS amt_return, 
    amount_tbl.amount 
FROM accounts 
    LEFT JOIN users jt0 
     ON jt0.id=accounts.assigned_user_id AND jt0.deleted=0 AND jt0.deleted=0 
    LEFT JOIN accounts_opportunities AS a_o 
     ON a_o.account_id = accounts.id AND a_o.deleted=0 
    LEFT JOIN opportunities AS opp 
     ON (opp.id = a_o.opportunity_id AND opp.deleted=0 AND opp.sales_stage = 'Closed Won' AND opp.opportunity_type IN('2', '4')) 
    JOIN (
     SELECT accounts.assigned_user_id, 
      SUM(IFNULL(accounts_collections.amount,0)) AS amount 
     FROM accounts 
      LEFT JOIN accounts_collections ON(accounts_collections.account_id = accounts.id AND accounts_collections.deleted=0) 
     GROUP BY accounts.assigned_user_id 
    ) AS amount_tbl ON (amount_tbl.assigned_user_id = accounts.assigned_user_id) 
WHERE accounts.deleted=0 
GROUP BY accounts.assigned_user_id, jt0.user_name 
ORDER BY SUM(IFNULL(opp.amount,0)) DESC 
+0

спасибо за вашу помощь. Он работал нормально. – Kxx