使用sql中的case语句将查询与另一个查询连接

时间:2015-12-15 08:25:12

标签: sql sql-server

我有这样的查询

SELECT
  ((SELECT
    F_Cat_Code
  FROM T_Category
  WHERE F_Cat_name = 'Furniture')
  + (SELECT
    F_L1Cat_Code
  FROM T_L1Category
  WHERE F_L1Cat_Name = 'Table')
  + (SELECT
    F_L2Cat_Code
  FROM T_L2Category
  WHERE F_L2Cat_Name = 'Tea Table')
  )

此查询0541123的输出在此我想再添加一个条件

select Max(F_Serial_No) 
from T_assets 
where F_Cat_Code='05' and F_L1Cat_Code = '41' and 
      F_L2Cat_Code ='123'  

如果此查询结果为null,那么我想显示值054112300001

如果该查询具有某些值,例如00002,那么我希望在递增1后显示输出,例如054112300003

2 个答案:

答案 0 :(得分:2)

试试这样:

{{1}}

答案 1 :(得分:1)

试试这个

  SELECT
      (cast((SELECT
        F_Cat_Code
      FROM T_Category
      WHERE F_Cat_name = 'Furniture') as varchar(max))
      + cast((SELECT
        F_L1Cat_Code
      FROM T_L1Category
      WHERE F_L1Cat_Name = 'Table') as varchar(max))
      + cast((SELECT
        F_L2Cat_Code
      FROM T_L2Category
      WHERE F_L2Cat_Name = 'Tea Table') as varchar(max))
      )+cast((select coalesce(Max(F_Serial_No)+1,'00001' )
    from T_assets 
    where F_Cat_Code='05' and F_L1Cat_Code = '41' and 
          F_L2Cat_Code ='123') as varchar(max))