Sql 不同的计数并在另一个表中查找该值
我正在编写一个针对不同预订计数表的查询,其中Sql 不同的计数并在另一个表中查找该值,sql,sql-server,Sql,Sql Server,我正在编写一个针对不同预订计数表的查询,其中类似于搜索'%gov%','%federal%','%army%'搜索billto,发货人,soldto和enduser字段,以及客户应该属于我们的客户表。下面是我尝试过的代码,结果应该是单一的 select (select distinct count (*) from active_customer where active_customer.Country = 'US' ), (sele
类似于搜索'%gov%'
,'%federal%'
,'%army%'
搜索billto
,发货人
,soldto
和enduser
字段,以及客户应该属于我们的客户表。下面是我尝试过的代码,结果应该是单一的
select (select distinct count (*)
from active_customer
where active_customer.Country = 'US'
),
(select count (distinct u.field)
from bookings
unpivot (field
for fields in (billto_name, shipto_name, soldto_name, end_user)
) u
where u.field like '%gov%' or u.field like '%federal%' or u.field like '%Army%'
)
结果:
4808 1105
结果不应组合在单独的行/列中。如果代码正常工作,但分别给出结果,只需附加两列即可。如果仍有问题,请将它们转换为varchar,然后附加它们。下面的示例
select (select distinct count (*)
from active_customer
where active_customer.Country = 'US'
)+' '+
(select count (distinct u.field)
from bookings
unpivot (field
for fields in (billto_name, shipto_name, soldto_name, end_user)
) u
where u.field like '%gov%' or u.field like '%federal%' or u.field like '%Army%'
)
样本数据和期望的结果真的会有帮助。你说的“组合”是什么意思?是否应添加数字?是的,结果应在一行中…需要从bookings表中的四个(billto、shipto、soldto和enduser)中获取不同的计数字段有%gov%或%federal%或%army,还有属于我们的客户……考虑到所有这些,我需要从预订表中获得不同的计数……我不明白。这些数字已经在一行了,不是吗?因此,您需要使用+
,而不是选择之间的,
?