with dm.Q2 do
  begin
    close;
    sql.Clear;
    sql.add('select sum(case Yh_Type=''照明用户'' then 1 else 0 end),'+'sum(case Yh_Type=''动力用户'' then 1 else 0 end),'+'sum(case Yh_Type=''公用变压器'' then 1 else 0 end),'+'sum(case Yh_Type=''私用变压器'' then 1 else 0 end) from alteramm');
    open;
    lzm.Caption:='照明用户换表共:'+fields[0].AsString+'只';
    ldl.Caption:='动力用户换表共:'+fields[1].AsString+'只';
    lpub.Caption:='公用变压器换表共:'+fields[2].AsString+'只';
    lprivy.Caption:='私用变压器换表共:'+fields[3].AsString+'只';
  end;
出错提示为:‘=附近有语法错误’!????????????????????

解决方案 »

  1.   

    类似Yh_Type=''照明用户'' 的改为Yh_Type='+''''+'照明用户'+''''+' then .....
      

  2.   

    sql.add('select sum(case Yh_Type=''''照明用户'''' then 1 else 0 end),'+'sum(case Yh_Type=''''动力用户'''' then 1 else 0 end),'+'sum(case Yh_Type=''''公用变压器'''' then 1 else 0 end),'+'sum(case Yh_Type=''''私用变压器'''' then 1 else 0 end) from alteramm');
      

  3.   

    或:
    sql.add('select sum(case Yh_Type="照明用户" then 1 else 0 end),'+'sum(case Yh_Type="动力用户" then 1 else 0 end),'+'sum(case Yh_Type="公用变压器" then 1 else 0 end),'+'sum(case Yh_Type="私用变压器" then 1 else 0 end) from alteramm');
      

  4.   

    你先别执行SQL,把sql.add()中的SQL打印出来瞧瞧或直接执行就可以发现错误在哪了。
      

  5.   

    你错误的使用了中文引号(在应该使用英文引号的地方)你可以把生成的SQL 拿出来执行一下看错误在哪里
      

  6.   

    你写错了,你本来要几种表分开统计,怎么能写成sum()+sum()+sum()的总数呢?
    应该是sum(),sum(),sum()吧?
      

  7.   

    case when Yh_Type="照明用户" then 1 else 0 end 再看行不行啊,我用总是要加这个 when 的