locked
Sql Query. -- optimize RRS feed

  • Question

  • Hello friends

    I need immediate help to optimize this query :-

    select * from
    (
        Select
            case when
            (air_annualflights  is null and WonOpportunities is not null ) or
            (air_annualflights  is not null and WonOpportunities is null ) or
            (air_annualflights  is not null and WonOpportunities is not null and
            air_annualflights <>WonOpportunities) then 1 else 0 end as flag, *
        from
        (
             select name, accountid, air_annualflights,
                 (
                    select
                    case when count(*) = 0 then  NULL else count(*) end as  opp
                    from [dbo].[T2]  as o with (Nolock)
                    where statecodename ='Won' and [actualclosedate] >=DATEADD(year,-1,GETDATE())  and o.accountid=a.accountid
                   ) as WonOpportunities
    from [dbo].[T1] as a with (Nolock)
    group by accountid, name, air_annualflights
    ) as B

    ) as C

    where flag =1
    order by name

    Please let know any Idea

    Friday, May 6, 2016 8:17 AM

Answers