查询为我提供了From子句错误

时间:2019-10-21 21:27:40

标签: sql sql-server

此SQL查询可以在较旧的MS Access文件中完美运行,但是在为较新的表重新创建该SQL查询后,它在from子句中给我一个语法错误。

select * from ( with dso as (select year, month, product_line_id, third_party_cust_indic as sales_type,  
                    sum(beg_snb_sales) + sum(beg_snb_ra) as beg_snb_sales,  
                    sum(snb_sales) + sum(snb_ra) as snb_sales,  
                    sum(beg_snb_cost) as beg_snb_cost,
                    sum(snb_cost) as snb_cost,
                    sum(spa) as spa_amount, 
                    sum(invoiced_sales) + coalesce(sum(ic_extended_price),0) as invoiced_sales,
                    sum(gross_invoiced_sales) as gross_invoiced_sales, 
                    sum(invoiced_cost)+ coalesce(sum(ic_extended_cost),0) as cost,
                    sum(beg_bl_future) + sum(beg_bl_dated) + sum(beg_bl_alloc) + sum(beg_bl_not_produced) as beginning_backlog,
                    sum(bl_future) + sum(bl_dated) + sum(bl_alloc) + sum(bl_not_produced) as current_backlog, 
                    sum(bl_alloc) as bl_alloc,
                    sum(bl_dated) as bl_dated,
                    sum(bl_future) as bl_future,
                    sum(bl_not_produced) as bl_not_produced,
                    sum(promos) + sum(edi_disc_allowance) + sum(new_store_allowance) + sum(ord_level_disc_allowance) + sum     (rdc_allowance) + sum(cert_rec_allowance) 
                    + sum(defective_allowance) + sum(freight_allowance)  + sum(adv_coop_allowance) + sum(store_svc_allowance) + sum(promo_amt) + sum(below_min_order_fee) as order_level_discounts,
manufacturing_location, inv_legal_entity || ' ' || inv_legal_entity_desc as shipping_location


       from prodsales.monthly_sales_orders
      where year = 2018
        and month = 10
        and (CUST_LEGAL_ENTITY = '001' )
     --  or ar_legal_entity = '001') 
        and PRODUCT_LINE_ID <> '000'
        and BUSINESS_UNIT_ID Not in ('OI','IC','BI','RF')  
      group by year, month, product_line_id, third_party_cust_indic, manufacturing_location, inv_legal_entity || ' ' || inv_legal_entity_desc
     )
, Pl as (select distinct product_line_id as pl from dso)
, YN as (select distinct sales_type as slstyp, year, month, day  from dso)
, pl_indic as (select * from pl, yn)

可能是什么问题?

3 个答案:

答案 0 :(得分:3)

如果这是SQL Server,则with语句需要在select之前:

with dso as (
      select . . .
      . . .
     )
select . . .

答案 1 :(得分:0)

with dso as (select year, month, product_line_id, third_party_cust_indic as sales_type,  
                    sum(beg_snb_sales) + sum(beg_snb_ra) as beg_snb_sales,  
                    sum(snb_sales) + sum(snb_ra) as snb_sales,  
                    sum(beg_snb_cost) as beg_snb_cost,
                    sum(snb_cost) as snb_cost,
                    sum(spa) as spa_amount, 
                    sum(invoiced_sales) + coalesce(sum(ic_extended_price),0) as invoiced_sales,
                    sum(gross_invoiced_sales) as gross_invoiced_sales, 
                    sum(invoiced_cost)+ coalesce(sum(ic_extended_cost),0) as cost,
                    sum(beg_bl_future) + sum(beg_bl_dated) + sum(beg_bl_alloc) + sum(beg_bl_not_produced) as beginning_backlog,
                    sum(bl_future) + sum(bl_dated) + sum(bl_alloc) + sum(bl_not_produced) as current_backlog, 
                    sum(bl_alloc) as bl_alloc,
                    sum(bl_dated) as bl_dated,
                    sum(bl_future) as bl_future,
                    sum(bl_not_produced) as bl_not_produced,
                    sum(promos) + sum(edi_disc_allowance) + sum(new_store_allowance) + sum(ord_level_disc_allowance) + sum     (rdc_allowance) + sum(cert_rec_allowance) 
                    + sum(defective_allowance) + sum(freight_allowance)  + sum(adv_coop_allowance) + sum(store_svc_allowance) + sum(promo_amt) + sum(below_min_order_fee) as order_level_discounts,
manufacturing_location, inv_legal_entity || ' ' || inv_legal_entity_desc as shipping_location


       from prodsales.monthly_sales_orders
      where year = 2018
        and month = 10
        and (CUST_LEGAL_ENTITY = '001' )
     --  or ar_legal_entity = '001') 
        and PRODUCT_LINE_ID <> '000'
        and BUSINESS_UNIT_ID Not in ('OI','IC','BI','RF')  
      group by year, month, product_line_id, third_party_cust_indic, manufacturing_location, inv_legal_entity || ' ' || inv_legal_entity_desc
     )
, Pl as (select distinct product_line_id as pl from dso)
, YN as (select distinct sales_type as slstyp, year, month, day  from dso)
, pl_indic as (select * from pl, yn)
select * from DSO

...尽管您似乎没有使用PL,YN或PL_INDIC。

答案 2 :(得分:0)

您的查询如下:

select * 
from ( 
    with dso as (
        select 
            ...
        from prodsales.monthly_sales_orders
        where
            ...
        )
    and ...
    group by ...
),
Pl as (select ... from dso),
YN as (select ... from dso),
pl_indic as (select ... from YN)

这里有很多问题:

  • 作为戈登·利诺夫(Gordon Linoff)的回答,with语句必须在select之前
  • dso声明结束后的条件没有意义,它们基本上与任何事物都没有关系
  • P1YNwith语句之外,而它们的语法建议它们应该属于它:Pl as (...)

问题太多了,无法提供有关如何重写查询的建议。这也使得它极不可能在任何数据库上运行。您将需要完全修改此查询的逻辑。