Sql 未找到数据时打印消息

Sql 未找到数据时打印消息,sql,oracle,Sql,Oracle,需要查询以获取员工姓名、每位员工使用的燃油总量。 如果员工未使用燃油,则第二列应具有 文本“未使用燃油” 以下是两个表格: Table1: EmployeeID, FirstName 1 Vikas 2 nikita 3 Ashish 4 Nikhil 5 anish 其中,table2.ID列是table1.EmployeeID的外键 这是我写的代码,很可能是错的 select ID, FirstName, sum(table2.Fuel) sum_fuel from table2,tab

需要查询以获取员工姓名、每位员工使用的燃油总量。 如果员工未使用燃油,则第二列应具有 文本“未使用燃油”

以下是两个表格:

Table1: EmployeeID, FirstName

1 Vikas
2 nikita
3 Ashish
4 Nikhil
5 anish

其中,
table2.ID
列是
table1.EmployeeID
的外键

这是我写的代码,很可能是错的

select ID, FirstName, sum(table2.Fuel) sum_fuel
from table2,table1
where EmployeeID=ID IN (
select ID, coalesce(ID, 'No-fuel used') as ID
from table1 t1
left join table2 t2 on t2.ID = t1.EmployeeID
)
group by fuel
order by ID DESC;
从两个表中可以看出,表1中1到5的员工在表2中。因此,对于这些员工,我需要显示每个人使用的燃油总量。对于ID为6到14的员工,表1中不可用,因此对于这些员工,应打印“未使用燃油”消息。

这可能有效---


您可以使用
左连接
。这样,每当表格的Id值不匹配时,您将获得
sum(fuel)
值的空值,并将使用
nvl()
函数为
sum\u fuel
列分配字符串
'No fuel used'

with table1( EmployeeID, FirstName ) as
(
 select 1,'Vikas'  from dual union all 
 select 2,'nikita' from dual union all 
 select 3,'Ashish' from dual union all 
 select 4,'Nikhil' from dual union all 
 select 5,'anish'  from dual union all 
 select 15,'pratteek'  from dual 
), table2( ID, Fuel ) as
(
 select 1, 10  from dual union all
 select 2, 9   from dual union all
 select 3, 8   from dual union all
 select 4, 6   from dual union all
 select 5, 12  from dual union all
 select 6, 11  from dual union all
 select 7, 10  from dual union all
 select 8, 9   from dual union all
 select 9, 8   from dual union all
 select 10, 10 from dual union all
 select 11, 9  from dual union all
 select 12, 12 from dual union all
 select 13, 7  from dual union all
 select 14, 15 from dual       
)
select EmployeeID, FirstName, nvl(to_char(sum(t2.Fuel)),'No fuel used') as sum_fuel 
  from table1 t1
  left join table2 t2 
    on t1.EmployeeID = t2.ID 
 group by EmployeeID, FirstName
 order by EmployeeID desc;

 EMPLOYEEID FIRSTNAME   SUM_FUEL
 ---------- ---------   ------------
 15         pratteek    No fuel used
  5         anish       12
  4         Nikhil      6
  3         Ashish      8
  2         nikita      9
  1         Vikas       10

表2.ID
表1.EmployeeID
的外键?@RobertKock是。您能告诉我们预期的输出(列和数据)吗?可能您需要“未找到员工”这样的消息,因为记录6-14中肯定使用了某些燃料。如果table2.ID是table1.EmployeeID的外键,则“对于ID为6到14的员工,表1中不可用”是不可能的
SELECT ID
     , FirstName
     , CASE 
               WHEN SUM(f.Fuel) > 0 THEN  CAST(SUM(f.Fuel)  AS NVARCHAR(25))
               ELSE 'No fuel used'
      END sum_fuel

FROM #emp e
LEFT JOIN #fuel f ON e.EmployeeID = f.id
GROUP BY ID,FirstName
ORDER BY ID DESC
with table1( EmployeeID, FirstName ) as
(
 select 1,'Vikas'  from dual union all 
 select 2,'nikita' from dual union all 
 select 3,'Ashish' from dual union all 
 select 4,'Nikhil' from dual union all 
 select 5,'anish'  from dual union all 
 select 15,'pratteek'  from dual 
), table2( ID, Fuel ) as
(
 select 1, 10  from dual union all
 select 2, 9   from dual union all
 select 3, 8   from dual union all
 select 4, 6   from dual union all
 select 5, 12  from dual union all
 select 6, 11  from dual union all
 select 7, 10  from dual union all
 select 8, 9   from dual union all
 select 9, 8   from dual union all
 select 10, 10 from dual union all
 select 11, 9  from dual union all
 select 12, 12 from dual union all
 select 13, 7  from dual union all
 select 14, 15 from dual       
)
select EmployeeID, FirstName, nvl(to_char(sum(t2.Fuel)),'No fuel used') as sum_fuel 
  from table1 t1
  left join table2 t2 
    on t1.EmployeeID = t2.ID 
 group by EmployeeID, FirstName
 order by EmployeeID desc;

 EMPLOYEEID FIRSTNAME   SUM_FUEL
 ---------- ---------   ------------
 15         pratteek    No fuel used
  5         anish       12
  4         Nikhil      6
  3         Ashish      8
  2         nikita      9
  1         Vikas       10