sqlserver中的Oracle连接查询

时间:2013-10-25 15:46:26

标签: sql-server sql-server-2008

以下是我需要更改为SQLSERVER 2008的示例oracle查询。基本上它从table2获取scode的描述,如果没有或使用'case'的null描述则将其设置为'unknown'。怎么办。

      select a.scode,b.description,a.amt,a.purid 
       from
      (select scode,ISNULL(SUM(AMOUNT),0) AS AMT,count(pur_ID)
       from table1
       where scode is not null 
       group by scode)A, table2 B  WHERE  A.SOURCE_CODE =+B.SOURCE

1 个答案:

答案 0 :(得分:0)

试试这个(请注意我加入了=):

Select A.Scode, A.Amt, A.Counts, IsNull(B.Description, 'Unknown') Descripton
From (
      select Scode, SOURCE_CODE, Isnull(sum(amount),0) as Amt, count(pur_ID) Counts
      from table1 
      where scode is not null 
      group by scode, SOURCE_CODE
    ) A left join  table2 B on A.SOURCE_CODE = B.SOURCE

根据评论进行修改

Select A.Scode, Isnull(sum(A.amount),0) as Amt, 
      count(A.pur_ID) Counts, IsNull(Max(B.description), 'Unknown') description
From Table1 A Left Join Table2 B
        On A.SOURCE_CODE = B.SOURCE
Where A.Scode is not null 
Group by A.Scode