-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]: キーワード 'group' 付近の構文が正しくありません。
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 に答える