MySQL - 我希望我的表JOIN根据条件返回一行

时间:2016-07-30 07:12:22

标签: mysql

在下面的代码中,'引导'中有多个条目。具有相同' account_id'的表格。我希望它返回一行 - 具有另一个字段的最小值的那一行' date_entered'。我不能使用' group by'在account_id上,因为我打算使用' group by'在BU上并相应地得到总和。请帮忙。

select uc.business_unit_dp_c,
FORMAT(SUM(CASE 
        WHEN lc.source_leads_c not in ('Discovery','Discovery SuperEmail','Self     Generated','Partner','Channel_Partner') and k.id<>'' THEN k.order_value
        WHEN lc.source_leads_c not in ('Discovery','Discovery SuperEmail','Self     Generated','Partner','Channel_Partner') and s.id<>'' THEN s.sivr_aiv_inr
        ELSE 0 
    END),0)
as Online,
FORMAT(SUM(CASE 
        WHEN lc.source_leads_c in ('Discovery', 'Discovery SuperEmail') and k.id<>'' THEN   k.order_value
        WHEN lc.source_leads_c in ('Discovery', 'Discovery SuperEmail') and s.id<>'' THEN   s.sivr_aiv_inr
        ELSE 0 
    END),0) 
as Discovery,
FORMAT(SUM(CASE 
        WHEN lc.source_leads_c in ('Partner','Channel_Partner') and k.id<>'' THEN   k.order_value
        WHEN lc.source_leads_c in ('Partner','Channel_Partner') and s.id<>'' THEN   s.sivr_aiv_inr
        ELSE 0 
    END),0) 
as Self_Generated_CP
from opportunities as o
left join opportunities_cstm as oc on o.id=oc.id_c
left join opportunities_knw_caf_1_c as ok on    o.id=ok.opportunities_knw_caf_1opportunities_ida
left join knw_caf as k on ok.opportunities_knw_caf_1knw_caf_idb=k.id
left join opportunities_knw_sivr_caf_1_c as os on   os.opportunities_knw_sivr_caf_1opportunities_ida=o.id
left join knw_sivr_caf as s on s.id=os.opportunities_knw_sivr_caf_1knw_sivr_caf_idb
left join accounts_opportunities as ao on ao.opportunity_id=o.id
left join leads as l on l.account_id=ao.account_id and l.account_id <> ''
left join leads_cstm as lc on lc.id_c=l.id
left join users_cstm as uc on uc.id_c=o.assigned_user_id
where o.sales_stage='clw' and
(k.id<>'' or s.id<>'') and o.jira_raise_date <> '' and
(o.tranjection_type in ('Fresh Plan / New Customer','Number Activation','Revival','Balance  Amount') or o.transaction_sivr in ('Paid Project','Number Allocation','New Feature')) and
o.jira_raise_date between '2016-06-01' and curdate()
group by uc.business_unit_dp_c

2 个答案:

答案 0 :(得分:0)

按照您描述的那样编写SQL

 Select * 
 from from opportunities o
     left join opportunities_cstm oc 
        on o.id = oc.id_c
     left join opportunities_knw_caf_1_c ok 
        on o.id = ok.opportunities_knw_caf_1opportunities_ida
     left join knw_caf k 
        on ok.opportunities_knw_caf_1knw_caf_idb = k.id
     left join opportunities_knw_sivr_caf_1_c os 
        on os.opportunities_knw_sivr_caf_1opportunities_ida=o.id
     left join knw_sivr_caf s 
        on s.id = os.opportunities_knw_sivr_caf_1knw_sivr_caf_idb
     left join accounts_opportunities ao 
        on ao.opportunity_id=o.id
     left join leads l 
        on l.account_id=ao.account_id 
           and l.account_id <> ''
     left join leads_cstm lc 
        on lc.id_c = l.id
     left join users_cstm uc 
        on uc.id_c = o.assigned_user_id
where o.sales_stage = 'clw' and
   and (k.id <> '' or s.id <> '') 
   and o.jira_raise_date <> '' 
   and (o.tranjection_type in 
        ('Fresh Plan / New Customer',
         'Number Activation','Revival','Balance  Amount') or
       o.transaction_sivr in 
        ('Paid Project','Number Allocation','New Feature')) 
   and o.jira_raise_date between '2016-06-01' and curdate()

- 接下来,将此附加谓词添加到Where子句......    使用表w / DateEntered列

   and date_entered =
       (Select Min(date_entered) 
        From accounts_opportunities os
            join tableWithDateEntered dr -- Table w/DateEntered
               on ?????                  -- proper join criteria here
        Where os.account_id = l.account_id)

---或由op构造(由我简化,因为account_id和date_entered都在表格中,这是唯一需要在子查询中引用的表格).....

   and l.date_entered = 
      (select min(date_entered) 
       from leads
       where account_id = l.account_id)

答案 1 :(得分:-1)

select min(C.date),C.Customer_Code from (
select InvoiceNo,month(InvoiceDate) as date,Customer_Code,Createddate from tbl_Invoices A Inner Join tbl_customer B on A.customer_Code=B.CustomerCode
where YEAR(InvoiceDate)='2017'
and CustomerCode not in (select CustomerCode from tbl_customer where year(createddate) in (year(getdate())))
and CustomerCode not in (select customer_Code from tbl_Invoices where year(InvoiceDate) in (year(getdate())-1))
and CustomerCode in (select customer_Code from tbl_Invoices where year(InvoiceDate) not in (year(getdate())))
--group by Customer_Code,Createddate,InvoiceNo
)C group by C.Customer_Code