如何在SQL查询中对名称进行分类?

时间:2015-11-30 08:11:48

标签: sql sql-server

我的查询如下。有帐户名称。他们给了我所有的数据库记录。但我想要类别。例如,如果有像“工资”这样的帐户名称,那么它们应该属于工资标题。如果它们与“费用”有关,那么它应该属于费用标题。请查看以下查询

SELECT T1.[FormatCode], T1.[AcctCode], T1.[AcctName], T4.[Name] ,T2.[Name],
       T0.[DebLTotal], T0.[DebRLTotal], T3.[Debit], T3.[RefDate]
FROM OBGT T0
  INNER JOIN OACT T1 ON T0.[AcctCode] = T1.[AcctCode]
  INNER JOIN OBGS T2 ON T0.[Instance] = T2.[AbsId]
  INNER JOIN JDT1 T3 ON T1.[AcctCode] = T3.[Account]
  INNER JOIN OASC T4 ON T1.[Project] = T4.[Code]  

我想根据帐户代码进行分类。例如,如果代码是51那么它就是“工资”和全部。请高手帮帮我。我这件事情已经很久了。

1 个答案:

答案 0 :(得分:0)

 SELECT T1.[FormatCode],  ( case when  T1.[AcctCode]=51 then 'salaries' when  T1.[AcctCode]=52 then  'dfds' end) as  AcctName, T4.[Name] ,T2.[Name],T0.[DebLTotal], T0.[DebRLTotal], T3.[Debit], T3.[RefDate] FROM 
OBGT T0 INNER JOIN OACT T1 ON T0.[AcctCode]=T1.[AcctCode] INNER JOIN OBGS T2 ON T0.[Instance] = T2.[AbsId] INNER JOIN JDT1 T3 ON T1.[AcctCode] = T3.[Account] INNER JOIN OASC T4 ON T1.[Project]=T4.[Code]