SQL Server 2008 - 语法不正确

Sk1*_*1X1 -1 sql-server-2008

我正在编辑从FireBird到SQL Server 2008的代码,我的语法有问题.我不确定错误在哪里.我没有编写这段代码,我在SQL Server中并不完全理解,所以我需要建议.

select *
from(
  select A.*,floor(Hotove) Hotove,floor(Rozpracovane) Rozpracovane,floor(buffer) buffer from (
  select typ,min(ID) ID ,
   convert(varchar(10), cast(min(case when PocetDnuMy<>-1000 then Dat2 else 
     cast(Dateadd(year, 5, getDate()) as float) end ) as datetime), 104) Datum, 
   min(dat2) dat2, min(Description) Description,
   min(Rada) Rada, min(CisloDokladu) CisloDokladu,
   min(JmenoFirmy) JmenoFirmy, min(Produkt) Produkt, 
   min(CastProduktu) CastProduktu, min(ResStrana) ResStrana, 
  Resitel, min(IC) IC, min(ICID) ICID, 
    case when max(PocetDnuMy)>182 then 'color: red;font-weight:bold;Text-decoration:underline'
         when max(PocetDnuMy )>10  then 'color: red;font-weight:bold'
         when max(PocetDnuMy )>0   then 'color: red'
         when max(PocetDnuMy )<0   then 'color: green' 
    end Color,
   count(ID) Pocet, max( PocetDnuMy ) NejviceMy,
   sum(case when  PocetDnuMy>0 then PocetDnuMy else 0 end) CelkemDnuMy,
   ResitelID, sum(case when PocetDnuMy>0 then 1 else 0 end) PocetProdleni
from( 
select 'b' typ,
       DVB.id as Id, 
       convert(varchar(10), cast(DVB.[DocDate$DATE] as datetime), 104) as Datum,
       DVB.[DocDate$DATE] Dat2,
       DVB.ShortDesc AS Description,
       DQ.code as Rada, 
       DVB.ordnumber as CisloDokladu, 
       F.name as JmenoFirmy,
       DVPR.Code as Produkt, 
       DVPA.Code as CastProduktu,   
       case 
         when (US.userstatuscode='?eká se na ?ešitele')or
              (US.userstatuscode='Neza?.-?eká na p?id?l.?ešitel')or
              (US.userstatuscode='Odsouhlaseno zákazníkem')   
         then cast(getDate() as float)-DVB.[DocDate$DATE]
         else -1000
       end        PocetDnuMy,
       case 
         when (US.userstatuscode='?eká se na ?ešitele')or
              (US.userstatuscode='Neza?.-?eká na p?id?l.?ešitel')or
              (US.userstatuscode='Odsouhlaseno zákazníkem')   
         then 0
         else cast(getDate() as float)-DVB.DocDate$DATE
       end        PocetDnuoni,
       CONVERT(VARCHAR(4),cast(DVB.[DocDate$DATE] as datetime),112) test2,
       case 
         when (US.userstatuscode='?eká se na ?ešitele')or
              (US.userstatuscode='Neza?.-?eká na p?id?l.?ešitel')or
              (US.userstatuscode='Odsouhlaseno zákazníkem')   
         then 'flores.png'
         else 'nopic.jpg'
       end AS ResStrana,
       SU.Name AS Resitel,
       SU.ID as ResitelID,
       case
         when LEFT(DVB.X_Zkratka2, 1)='C' then DVB.X_Zkratka2
         when LEFT(DVB.X_Zkratka2, 1)<>'C' then ''
       end AS IC,
       RIGHT(DVB.X_Zkratka2, 6) AS ICID
from DEVBUGS DVB
join devproducts AS DVPR on DVB.Product_ID=DVPR.ID
join devparts AS DVPA on DVB.Part_ID=DVPA.ID and CHARINDEX('H',DVPA.Code)<>1
join userstatuses US on DVB.status_id=US.id
join periods P on DVB.period_id=P.id
join docqueues DQ on DVB.docqueue_id=DQ.id and DQ.Code<>'POZ' 
join firms F on DVB.firm_id=F.id
join SecurityUsers AS SU on DVB.ResponsibleUser_ID=SU.ID
where
  (US.userstatuscode<>'Hotovo' and 
  US.userstatuscode<>'Uzam?eno' and
  US.userstatuscode<>'Odloženo' and
  US.userstatuscode<>'Zamítnuto')
AND (DVPA.Code='A' or DVPA.Code='B' or DVPA.Code='C' or DVPA.Code='HA' or DVPA.Code='HB' or DVPA.Code='HC' or DVPA.Code='Z' or DVPA.Code='OBJ' or DVPA.Code='-') ) B
group by Resitel,ResitelID, typ) A
left outer join (SELECT sum(A.X_Ef_Cas) EfCas,sum(A.X_Ef_Cas*Pomer) HodPrem,A.Worker_ID,
    sum(case when ((S.UserStatusCode  in ('Hotovo','Uzam?eno','Zamítnuto')) or D.X_Zkratka2='Telefonista') then A.X_Ef_Cas*Pomer else 0 end) Hotove,
    sum(case when ((S.UserStatusCode  in ('Hotovo','Uzam?eno','Zamítnuto')) or D.X_Zkratka2='Telefonista') then 0 else A.X_Ef_Cas*Pomer end) Rozpracovane
 FROM DevBugs as D 
  join  DevTimeRecords A on D.id =A.DevDocument_ID
  JOIN UserStatuses as S ON S.ID = D.Status_ID
  join DevProducts DP ON DP.ID=D.Product_ID 
  join devparts AS DVPA on D.Part_ID=DVPA.ID and CHARINDEX('H',DVPA.Code)<>1
  join 
   (select D.ID,case when sum(A.X_Ef_Cas)>EstimatedTime then EstimatedTime/sum(A.X_Ef_Cas) else 1 end Pomer
     FROM DevBugs as D 
     join  DevTimeRecords A on D.id =A.DevDocument_ID
     join DevProducts DP ON DP.ID=D.Product_ID 
       WHERE  x_PremieObd=0
       and  (CHARINDEX('Cestovné Km', D.ShortDesc)<=0)
       and  (CHARINDEX('?as na cest?', D.ShortDesc)<=0)
       and  (CHARINDEX('ST', D.X_Zkratka2)<>1)
      and (D.X_Zkratka2<> 'RE')
      and DP.Code in ('ZP','SI','FLORES')
      and (DP.Code<>'FLORES' or (DP.Code='FLORES' and not A.Worker_ID in ('DD00000101','FD00000101')))  
     group by D.ID,D.EstimatedTime) Pomery on Pomery.ID=D.ID
  WHERE    
      (D.X_Zkratka2<> 'RE')
       and  (CHARINDEX('Cestovné Km', D.ShortDesc)<=0)
       and  (CHARINDEX('?as na cest?', D.ShortDesc)<=0)
       and  (CHARINDEX('ST', D.X_Zkratka2)<>1)
      and DP.Code in ('ZP','SI','FLORES')
      and (DP.Code<>'FLORES' or (DP.Code='FLORES' and not A.Worker_ID in ('DD00000101','FD00000101')))  
     and x_PremieObd=0
     group by Worker_ID ) premie on Worker_ID=ResitelID
left outer join (select sum(buffer) buffer,ResponsibleUser_ID from 
    (select D.ID, ResponsibleUser_ID, case when (sum(A.X_Ef_Cas)>EstimatedTime )  then 0 when  sum(A.X_Ef_Cas)is  null then EstimatedTime else (EstimatedTime- sum(A.X_Ef_Cas)) end buffer
     FROM DevBugs as D 
     left outer join  DevTimeRecords A on D.id =A.DevDocument_ID
     join DevProducts DP ON DP.ID=D.Product_ID 
     join userstatuses US on D.status_id=US.id
     join devparts AS DVPA on D.Part_ID=DVPA.ID and CHARINDEX('H',DVPA.Code)<>1
      WHERE 
        (CHARINDEX('Cestovné Km', D.ShortDesc)<=0)
        and  (CHARINDEX('?as na cest?', D.ShortDesc)<=0)
        and  (CHARINDEX('ST', D.X_Zkratka2)<>1)
        and (D.X_Zkratka2<> 'RE')
        and D.X_Zkratka2<>'Telefonista'
      and DP.Code in ('ZP','SI','FLORES')
      and (DP.Code<>'FLORES' or (DP.Code='FLORES' and not A.Worker_ID in ('DD00000101','FD00000101')))  
   and  
  US.userstatuscode<>'Hotovo' and 
  US.userstatuscode<>'Uzam?eno' and
  US.userstatuscode<>'Odloženo' and
  US.userstatuscode<>'Zamítnuto'
group by D.ID, D.ResponsibleUser_ID, D.EstimatedTime)
group by ResponsibleUser_ID ) buffer on buffer.ResponsibleUser_ID =ResitelID
union  all
select 'a' typ,
       DVB.id as Id, 
       convert(varchar(10), cast(DVB.[DocDate$DATE] as datetime), 104) as Datum,
       DVB.[DocDate$DATE] Dat2,
       DVB.ShortDesc AS Description,
       DQ.code as Rada, 
       DVB.ordnumber as CisloDokladu, 
       F.name as JmenoFirmy,
       DVPR.Code as Produkt, 
       DVPA.Code as CastProduktu,   
       case 
         when (US.userstatuscode='?eká se na ?ešitele')or
              (US.userstatuscode='Neza?.-?eká na p?id?l.?ešitel')or
              (US.userstatuscode='Odsouhlaseno zákazníkem')   
         then 'flores.png'
         else 'nopic.jpg'
       end AS ResStrana,
       SU.Name AS Resitel,
       case
         when LEFT(DVB.X_Zkratka2, 1)='C' then DVB.X_Zkratka2
         when LEFT(DVB.X_Zkratka2, 1)<>'C' then ''
       end AS IC,
       RIGHT(DVB.X_Zkratka2, 6) AS ICID,
 case
   when 
    (DVB.[DocDate$DATE]< cast(getDate() as float)-182)
    then 'color: red;font-weight:bold;Text-decoration:underline'
   when 
    (DVB.[DocDate$DATE]< cast(getDate() as float)-10)
    then 'color: red;font-weight:bold'
   when 
    (DVB.[DocDate$DATE]< cast(getDate() as float))
    then 'color: red'
   when 
    (DVB.[DocDate$DATE]> cast(getDate() as float) )
    then 'color: green'
 end as Color,
 0 as Pocet, 0 as NejviceMy, 0 as CelkemMy, ' ' ResitelID,
 0 Hotove, 0 Rozpracovane, 0 buffer, 0 PocetProdleni
from DEVBUGS DVB
join devproducts AS DVPR on DVB.Product_ID=DVPR.ID
join devparts AS DVPA on DVB.Part_ID=DVPA.ID
join userstatuses US on DVB.status_id=US.id
join periods P on DVB.period_id=P.id
join docqueues DQ on DVB.docqueue_id=DQ.id and DQ.Code<>'POZ'
join firms F on DVB.firm_id=F.id
join SecurityUsers AS SU on DVB.ResponsibleUser_ID=SU.ID
where
  (US.userstatuscode<>'Hotovo' and 
  US.userstatuscode<>'Uzam?eno' and
  US.userstatuscode<>'Odloženo' and
  US.userstatuscode<>'Zamítnuto')
AND (DVPA.Code='A' or DVPA.Code='B' or DVPA.Code='C' or DVPA.Code='HA' or DVPA.Code='HB' or DVPA.Code='HC' or DVPA.Code='Z' or DVPA.Code='OBJ' or DVPA.Code='-')
) A 
ORDER BY typ,NejviceMy desc,Dat2, Resitel ASC 
Run Code Online (Sandbox Code Playgroud)

错误,我得到的是

SQL错误[156] [S1000]:关键字"group"附近的语法不正确.
SQL错误[102] [42000]:')'附近的语法不正确.
关键字"group"附近的语法不正确

请问哪里有问题?谢谢你的所有建议.

Aar*_*and 7

你有:

group by D.ID, D.ResponsibleUser_ID, D.EstimatedTime)
group by ResponsibleUser_ID )
Run Code Online (Sandbox Code Playgroud)

你需要:

group by D.ID, D.ResponsibleUser_ID, D.EstimatedTime) AS some_alias
group by ResponsibleUser_ID )
Run Code Online (Sandbox Code Playgroud)

但是,我没有兴趣解析意大利面以确定它是否需要特定的别名或任何别名.

这可能不是语法问题的结束.不要亲自接受,但这段代码很糟糕.您应该尝试以逻辑方式简化和构建您尝试使用查询完成的工作,而不是仅仅通过搜索和啄食来使语法"正常工作".

  • @mattytommo恰恰相反,我想指出这个问题是100%的缺失别名就足够了.仅仅因为他没有重写整个查询并不意味着它不是一个答案. (3认同)
  • @mattytommo所以不要抱怨我的答案,为什么不解析意大利面并创建一个更好的查询版本?测试OP是一件非常简单的事情.他们可以添加我建议的别名,如果他们然后得到列关联错误,错误可以指向他们引用他们从未创建的别名的位置. (2认同)
  • @mattytommo好的,谢谢你的意见.这是我的表达方式,他有一个难以维护的混乱.如果你有一个更好的方式来说,请随意添加你的意见的答案.不过,我想对亚马逊*进行*不同的*荒谬的类比是必要的吗? (2认同)
  • @ Sk1X1是的,继续吗? (2认同)