-1

我正在编辑从 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 

我得到的错误是

SQL 错误 [156] [S1000]:关键字“组”附近的语法不正确。
SQL 错误 [102] [42000]:')' 附近的语法不正确。
关键字“组”附近的语法不正确

请问问题出在哪里?感谢所有建议。

4

1 回答 1

7

你有:

group by D.ID, D.ResponsibleUser_ID, D.EstimatedTime)
group by ResponsibleUser_ID )

你需要:

group by D.ID, D.ResponsibleUser_ID, D.EstimatedTime) AS some_alias
group by ResponsibleUser_ID )

但是,我对解析该意大利面条以确定它是否需要特定别名或只是任何别名没有兴趣。

这可能不是您的语法问题的结束。不要个人认为,但这段代码很糟糕。您应该尝试以合乎逻辑的方式简化和构建您试图通过查询完成的内容,而不是仅仅寻找和啄以使语法“工作”。

于 2013-03-12T16:09:57.547 回答