Home > Blockchain >  How do I format this SQL Like Statement
How do I format this SQL Like Statement

Time:03-31

Can someone assist with this syntax

   sum((case when isnull(lpc.sales_part_code,'') like 'E%' 
             then 1 else 0 end) as e_line_count
             then 1 else 0 end) as e_line_count
    from sales_order_lines_with_rollup sol    
    inner join edap_dw.fact_sales_order_header soh
    on sol.business_unit_key = soh.business_unit_key
    and sol.sales_order_number = soh.sales_order_number
    left join edap_dw.fact_inventory_on_hand_warehouse w
    on sol.part_key = w.part_key
    and w.snapshot_day_key = sol.order_date_key
    left join edap_dw.dim_part_code lpc 
    on sol.part_key = lpc.part_key and lpc.current_record_ind = 'Y'
    LEFT JOIN #business_days do ON do.time_day_key = sol.order_date_key
    LEFT JOIN #business_days ds ON ds.time_day_key = case when sol.ship_day_key = -1 then format(GETDATE(),'yyyyMMdd') else sol.ship_day_key end
    --LEFT JOIN #business_days dp ON dp.time_day_key = case when sol.promised_ship_day_key = -1 then format(GETDATE(),'yyyyMMdd') else sol.promised_ship_day_key end
    where sol.sales_order_quantity > 0
    and sol.order_date_key > 20170101
    group by sol.sales_order_number,sol.business_unit_key

I need to add another code '%-1%' to the like clause in this section:

sum((case when isnull(lpc.sales_part_code,'') like 'E%' 
             then 1 else 0 end) as e_line_count
             then 1 else 0 end) as e_line_count

so it should look something like

sum((case when isnull(lpc.sales_part_code,'') like 'E%' or when isnull(lpc.sales_part_code,'') = '-1%'
             then 1 else 0 end) as e_line_count
             then 1 else 0 end) as e_line_count

I am getting a syntax error.

CodePudding user response:

I assume you wanted the 2nd evaluation to be a LIKE operator and not equals

SUM(case when lpc.sales_part_code LIKE 'E%' or lpc.sales_part_code LIKE '-1%' then 1 else 0 end) 
  • Related