【问题标题】:Using Where clause in between Inner Join Query在内部联接查询之间使用 Where 子句
【发布时间】:2017-11-02 21:44:43
【问题描述】:

我正在寻找记录,我遇到了一个场景,我必须在使用内部连接的选择查询之间包含额外的 where 子句。

select stp.sales_person as "Sales_Person",
max(case when stp.jan_month is null then 0 else stp.jan_month end) as "January",
select sum(so.amount_total) from sale_order so inner join res_users ru on ru.id=so.user_id
where date(so.confirmation_date) > '2017-01-01' and date(so.confirmation_date) < '2017-01-30', 
max(case when stp.feb_month is null then 0 else stp.feb_month end) as "February",   
max(case when stp.march_month is null then 0 else stp.march_month end) as "March",   
max(case when stp.dec_month is null then 0 else stp.dec_month end) as "December"
from sales_target_record stp
inner join res_partner rp on rp.name=stp.sales_person inner join res_users ru on ru.partner_id = rp.id inner join crm_team ct on ru.sale_team_id = ct.id     
where ct.name = 'Direct Sales' group by stp.sales_person

我必须像我尝试使用 sum 一样插入列,但不能作为连接查询工作

【问题讨论】:

  • MySQL SQL Server PostgreSQL。请标记您实际使用的那个。
  • 您的so.amount_total 之后没有END 用于您的Case 声明
  • Select 声明必须包含在 () 中,如果您希望它作为一个列。
  • 但在这种情况下,使用此特定选择的内部连接会导致问题

标签: postgresql openerp


【解决方案1】:

如果这是真正的 SQL Server,则您的查询中存在语法问题

select 
    stp.sales_person as Sales_Person,
    max(case 
            when stp.jan_month is null 
                then 0 
                else stp.jan_month 
        end) as January,
        --This needed parenthese since it's a subquery. Though, it's uncorrelated
        (   select sum(so.amount_total) 
             from sale_order so 
             inner join res_users ru on ru.id=so.user_id
             where cast(so.confirmation_date as date) > '2017-01-01' and cast(so.confirmation_date as date) < '2017-01-30'
             --here you need to add something like stp.someColumn = so.SomeColumn to correlate it to the outer query
        ) as SomeNewColumnUnCorrelated, 
    max(case 
            when stp.feb_month is null 
                then 0 
                else stp.feb_month 
        end) as February,   
    max(case 
            when stp.march_month is null 
                then 0 
                else stp.march_month 
        end) as March,   
    max(case 
            when stp.dec_month is null 
                then 0
                else stp.dec_month 
        end) as December
from 
    sales_target_record stp
inner join 
    res_partner rp on 
    rp.name=stp.sales_person 
inner join
    res_users ru on 
    ru.partner_id = rp.id     
where 
    ct.name = 'Direct Sales'
group by 
    stp.sales_person

【讨论】:

  • select sum 的内部连接正在获取错误的记录
  • 我不怀疑......那是因为它是不相关的,正如我所说的。您必须为该子查询提供适当的 where 子句。我们无法确定应该是什么
  • 这就是我对先生的要求
  • 我们无法知道与我们的 ERD 的联接
猜你喜欢
  • 1970-01-01
  • 2012-03-21
  • 2014-10-18
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
相关资源
最近更新 更多