“ Microsoft SQL:')'附近的语法不正确。”

时间:2019-04-18 15:04:59

标签: sql powerbi

尝试将SQL语句加载到PowerBI中,但出现以下错误:

  

Microsoft SQL:“)”附近的语法不正确

这是SQL:

with  thelicenses as
(
    select 
        CustomerOrderItemID, SupportDurationDays, SupportStarted, 
        ROW_NUMBER() over (partition by CustomerOrderItemID order by Addedwhen desc) as RN
    from 
        Licenses
)
, list as 
(
    select   
        coi.CustomerOrderID as CustomerOrderID,
        0 as IsCancelled,
        coi.ProductPrice as 'ProductPrice',
        coi.SupportPrice as 'SupportPrice',
        coi.Qty as 'Qty',
        coi.NumberOfUsers,
        coi.ProductDescription,
        isnull(coi.SupportDescription, '') as SupportDescription,
        isnull((select SupportID from SupportPrices where SupportPrices.ID = coi.SupportPriceID), -1) as SupportID,
        inv.AddedWhen as RateDate,
        coi.ID as CustomerOrderItemID
    from     
        CustomerOrderItems coi
    inner join 
        CustomerOrders co on co.ID = coi.CustomerOrderID
    inner join 
        InvoiceNumbers inv on inv.ID = co.InvoiceNumber
    left join 
        CustomerOrderStatus coss on coss.OrderID = coi.CustomerOrderID 
                                 and coss.OrderStatusID = 6

    union all

    select   
        ic.CustomerOrderID as CustomerOrderID,
        1 as IsCancelled,
        coi.ProductPrice * -1 as 'ProductPrice',
        coi.SupportPrice * -1 as 'SupportPrice',
        coi.Qty as 'Qty',
        coi.NumberOfUsers,
        coi.ProductDescription,
        isnull(coi.SupportDescription, '') as SupportDescription,
        isnull((select SupportID from SupportPrices where SupportPrices.ID = coi.SupportPriceID),-1) as SupportID,
        ic.AddedWhen as RateDate,
        coi.ID as CustomerOrderItemID
    from     
        InvoiceCancellations ic
    inner join 
        CustomerOrders co on co.ID = ic.CustomerOrderID
    inner join 
        InvoiceNumbers inv on inv.ID = co.InvoiceNumber
    left join 
        CustomerOrderStatus coss on coss.OrderID = ic.CustomerOrderID 
                                 and coss.OrderStatusID = 6
    left join 
        CustomerOrderItems coi on coi.CustomerOrderID = ic.CustomerOrderID
)
select  
    cy.Description as [Currencies Description],
    cer.Rate,
    c.Territory,
    c.Description as [Countries Description],
    inv.AddedWhen,
    co.LicenseeCompanyName,
    co.CurrencyID,
    list.ProductPrice,
    list.SupportPrice,
    list.Qty,
    case 
       when list.ProductDescription like '%Signature Manager Exchange%'
          then 'Signature Manager Exchange Edition'
       when list.ProductDescription like '%Signature Manager Outlook%'
          then 'Signature Manager Outlook Edition'
       when list.ProductDescription like '%Signature Manager Office 365%'
          then 'Signature Manager Office 365 Edition'
       when list.ProductDescription like '%Mail Disclaimers%'
          then 'Mail Disclaimers'
       when list.ProductDescription like '%Signature Manager%'
          then 'Signature Manager'
       when list.ProductDescription like '%Auto Responder%'
          then 'Auto Responder'
       when list.ProductDescription like '%Exclaimer Mail Utilities 2007%'
          then 'Mail Utilities 2007'
       when list.ProductDescription like '%Exclaimer Mail Utilities%'
          then 'Mail Utilities'
       when list.ProductDescription like '%Exclaimer SBS Suite%'
          then 'SBS Suite'
       when list.ProductDescription like '%Template Editor%'
          then 'Template Editor'
       when list.ProductDescription like '%Template Services%'
          then 'Template Services'
       when list.ProductDescription like '%Remote Installation%'
          then 'Remote Installation'
       when list.ProductDescription like '%Anti-spam%'
          then 'Anti-spam'
       when list.ProductDescription like '%Image Analyzer%'
          then 'Image Analyzer'
       when list.ProductDescription like '%Mail Archiver%'
          then 'Mail Archiver 3'
       when list.ProductDescription like '%Alias Manager%'
          then 'Alias Manager'
       when list.ProductDescription like '%Address Tagging%'
          then 'Address Tagging'
       when list.ProductDescription like '%Exclaimer Cloud - Signatures for Office 365%'
          then 'Exclaimer Cloud - Signatures for Office 365'
       when list.ProductDescription like '%Exclaimer Cloud - Signatures for G Suite%'
          then 'Exclaimer Cloud - Signatures for G Suite'
       when list.ProductDescription like '%Exclaimer Cloud - API%'
          then 'Exclaimer Cloud - API'
       when list.ProductDescription like '%Order costs%' or
                 list.ProductDescription like '%Orderkosten%' or
                 list.ProductDescription like '%Lieferung auf Rechnung%'
          then 'Extra Invoice Costs'
       else 'Mail Utilities Workstation'
    end as 'ProductName',
    case when list.IsCancelled = 1 then 'Cancelled'
                 when co.IsRenewal = 1 then 'Renewal'
                 else 'New Sale'
            end as 'Type',
            case when list.ProductDescription like '%SBS%' then 'SBS'
                 when list.ProductDescription like '%10000%' then '10000'
                 when list.ProductDescription like '%9000%' then '9000'
                 when list.ProductDescription like '%8000%' then '8000'
                 when list.ProductDescription like '%7000%' then '7000'
                 when list.ProductDescription like '%6000%' then '6000'
                 when list.ProductDescription like '%5000%' then '5000'
                 when list.ProductDescription like '%4500%' then '4500'
                 when list.ProductDescription like '%4000%' then '4000'
                 when list.ProductDescription like '%3500%' then '3500'
                 when list.ProductDescription like '%3000%' then '3000'
                 when list.ProductDescription like '%2500%' then '2500'
                 when list.ProductDescription like '%2000%' then '2000'
                 when list.ProductDescription like '%1500%' then '1500'
                 when list.ProductDescription like '%1000%' then '1000'
                 when list.ProductDescription like '%875%' then '875'
                 when list.ProductDescription like '%750%' then '750'
                 when list.ProductDescription like '%625%' then '625'
                 when list.ProductDescription like '%500%' then '500'
                 when list.ProductDescription like '%350%' then '350'
                 when list.ProductDescription like '%300%' then '300'
                 when list.ProductDescription like '%250%' then '250'
                 when list.ProductDescription like '%200%' then '200'
                 when list.ProductDescription like '%150%' then '150'
                 when list.ProductDescription like '%125%' then '125'
                 else '100 or less'
            end as 'Size',
            list.IsCancelled,
            (
              select top 1
                        ic.Reason
              from      InvoiceCancellations ic
              where     ic.CustomerOrderID = co.ID
            ) as 'CancellationReason',
        case when list.NumberOfUsers > 90000 then 10 else list.NumberOfUsers end as NumberOfUsers,
        rn.NextRenewDate as RenewalDate,
        isnull(cov.ResellerID, cov.CustomerID) as CustomerID,
        inv.ID as InvoiceNumber,
        list.SupportDescription,
        case when cust.CustomerType = 0 then 'End User'
         when cust.CustomerType = 1 then 'Reseller'
         when cust.CustomerType = 2 then 'Partner'
         when cust.CustomerType = 3 then 'Placeholder'
         when cust.CustomerType = 4 then 'Inactive'
         when cust.CustomerType = 5 then 'Prospect' end as CustomerType,
        case when list.IsCancelled = 1 then 1 
         when (select top 1 1 from list ll where ll.IsCancelled = 1 and ll.CustomerOrderID = co.ID) = 1 then 1 else 0 end as HasCancellation,
        DATEDIFF(MONTH, l.SupportStarted, DateADD(d, l.SupportDurationDays, l.SupportStarted)) as SupportMonths, reseller.id as resellerid, reseller.companyname as resellername
    from    
        list as list
    inner join 
        CustomerOrders as co on list.CustomerOrderID = co.ID
    inner join 
        View_CustomerOrders cov on cov.OrderID = co.ID
    inner join 
        InvoiceNumbers inv on inv.ID = co.InvoiceNumber
    inner join 
        Currencies as cy on co.CurrencyID = cy.ID
    inner join 
        Customers cust on cust.ID = isnull(cov.ResellerId, cov.CustomerID)
    left join 
        Countries as c on cov.CountryID = c.ID
    left join 
        CurrencyExchangeRates cer on cer.CurrencyID = co.CurrencyID 
                                  and list.RateDate between cer.DateFrom and cer.DateTo
    left join 
        thelicenses l on l.CustomerOrderItemID = list.CustomerOrderItemID 
                      and l.RN = 1
    left join 
        Renewals rn on rn.OrderID = co.ID
    left join 
        Customers as reseller on reseller.id = cov.ResellerID
    where   
        exists (select 1
                from CustomerOrderStatus as coss
                where (coss.OrderID = co.ID ) 
                  and (coss.OrderStatusID in (6, 14))) 
        and )
order by 
    inv.AddedWhen
-- Force optimizer to use this order
-- No join predicate issue
option  ( force order )

2 个答案:

答案 0 :(得分:1)

"Msg 102, Level 15, State 1, Line 165
Incorrect syntax near ')'."  

WHERE子句格式错误:

 where   exists ( select 1
                     from   CustomerOrderStatus as coss
                     where  ( coss.OrderID = co.ID ) and
                            ( coss.OrderStatusID in ( 6, 14 ) ) ) and

        )
    order by inv.AddedWhen

删除

                                                              and

)

答案 1 :(得分:0)

下面的'and)'似乎不合适。

    where   exists ( select 1
                     from   CustomerOrderStatus as coss
                     where  ( coss.OrderID = co.ID ) and
                            ( coss.OrderStatusID in ( 6, 14 ) ) ) and

        )