我有三个表,它们之间没有外键。我需要从所有这3个表中获取count(*)
。我在这些表之间使用union
,并能够获得如下输出。
Output:
10
20
15
但是,我需要在指定表名称或LineOfBusiness的那些行旁边添加其他列。有人可以帮助我实现这一目标。
Expected Output:
10 Customer
20 Provider
15 Merchant
缺少很多信息,因此我将继续使用您提供的信息;简而言之,您要问的是非常简单。您只需要像下面这样在select语句中输入表名即可:
select count(*) as Total, 'RDC' as TableName
from DL_5000_5002 as RDC
where RDC.CallStatusZone1 in ('P')
union all
select count(*) as Total, 'U' as TableName
from DL_5000_5003 as U
WHERE U.CallStatusZone1 in ('P')
union all
select count(*) as Total, 'PM' as TableName
from DL_5005_5005 AS PM
where PM.CallStatusZone1 in ('P')
union all
select count(*) as Total, 'PP' as TableName
from DL_5005_5006 AS PP
where PP.CallStatusZone1 in ('P')
现在,如果您想根据条件将这些表名动态地拉到另一列或其他值中,那将是另一回事了。
结果集如下:
Total TableName
---------- ----------
1835 RDC
0 U
0 PM
0 PP