日期:2014-05-18  浏览次数:20712 次

SQL 的写法?

select  
ID   =   Identity(Int,   1,   1),
D.FID   AS   FID,
A.finterid   AS   finterid,
C.Fname   as   [客户名称   (Client)],
A.Fbillno   as   [发票号码       (Inv.   No)],  
A.Fdate   as   [发票日期     (Inv.   Date)],
Sum(B.FAmount)   AS   [     货物净值         (Net   value)],  
Sum(B.FTaxAmount)   AS   [税   额   (VAT)],
A.FHeadselfi0453   as   [       运         费             (Transport   cost)],
sum(B.FAmountincludetax)   as   [   价税合计         (Total   Amount)],
A.FHeadselfi0464   as   [应回款日   (Due   date)],
D.FcheckamountFor     as   [实际回款金额       (Amount   cashed)]   ,
D.fcheckdate   as   [实际回款日期   (Date   of   cash)]


into   #ghtemp

from  
ICsale   A
JOIN  
ICsaleentry   B
on     A.finterid   =B.finterid  
JOIN  
t_Organization   C
on     A.FcustID   =C.FitemID    
left   join  
t_RP_NewCheckInfo   D
on   A.finterid=D.Fbillid   and   D.ftype=3
     
GROUP   BY  
A.FInterID,
A.Fbillno,
A.Fdate,
A.FHeadselfi0453,
A.FHeadselfi0464,
C.Fname,
D.FcheckamountFor,
D.fcheckdate,
D.FID


select   E.finterid,E.fid   as   fid,f.fdate   as   fdate  
into   #ghtemp1
from   #ghtemp
JOIN  
t_RP_NewCheckInfo   E
on     #ghtemp.fid=E.FID   AND   E.ftype=5
JOIN  
t_RP_NewReceiveBill   F
on   e.Fbillid=f.Fbillid      

update       #ghtemp   set   [实际回款日期   (Date   of   cash)]   =   #ghtemp1.fdate   from   #ghtemp1   where   #ghtemp.fid=#ghtemp1.fid    


select   *   from   #ghtemp

Union   All

Select
NULL,
Null,
Null,
'bb ',
Null,
Null,
Null,
Null,
Null,
Sum([   价税合计         (Total   Amount)])   as   [   价税合计         (Total   Amount)],
Null,
Sum([实际回款金额       (Amount   cashed)])   as   [实际回款金额       (Amount   cashed)],
Null
From

(

select   (Case   When   Exists   (Select   ID   From   #ghtemp   Where   [发票号码       (Inv.   No)]   =   A.[发票号码       (Inv.   No)]   And   ID   <   A.ID)   Then   0   Else   [   价税合计         (Total   Amount)]   End)   As   [   价税合计         (Total   Amount)],   [实际回款金额       (Amount   cashed)]   from   #ghtemp   A)   B

Drop   Table   #ghtemp1
Drop   Table   #ghtemp


在SQL查询分析器里执行没有问题。在金蝶软件里执行有问题(into   #ghtemp   出错)不用临时表,如何写以上SQL。



------解决方案--------------------
1.把临时表改成实体表试试