首页 > 解决方案 > 不同的计数并查看另一个表中的值

问题描述

我是一个针对不同数量的预订表的写作查询,其中使用、、、和字段进行like搜索,还有一个客户应该属于美国的客户表。下面是我尝试过的代码,结果应该是单一的。'%gov%''%federal%''%army%'billtoshiptosoldtoenduser

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

结果不应组合在单独的行/列中。

标签: sqlsql-server

解决方案


如果您的代码正在运行,但分别为您提供结果只需附加两列,如果您仍然有问题将它们转换为 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%' 
       )

推荐阅读