2017-04-18 155 views
-3

我执行查询,我想输出作为SQL查询结果的格式

CompanyType SFCount SNCount 
Customer  47  3 
Vendor  8  3 
Internal  11  1 

但输出来了,如:

enter image description here

+0

包括你的查询和表结构,是文本,而不是图像。 –

+0

请考虑在格式化文本中添加预期结果。 同时向我们显示您当前的查询尝试 – TheGameiswar

+0

GroupType by CompanyType应该完成这项工作。 –

回答

1

环绕你查询了一个子查询和sum()结果如下:

SELECT sub.CompanyType, sum(sub.sfcount) as sfcount, sum(sub.sncount) as sncount 
FROM ( 
     SELECT businesstype AS [CompanyType], 
      count(*) AS [SFCount], 
      '' AS [SNCount] 
     FROM account 
     WHERE businesstype IN ('Customer', 'Vendor', 'Internal')  ) 
     GROUP BY businesstype 

     UNION 

     SELECT 'Customer' AS [CompanyType], 
      '' AS [SFCount], 
      count(*) AS [SNCount] 
     FROM sn_core_company 
     WHERE customer = 1 

     UNION 

     SELECT 'Vendor' AS [CompanyType], 
      '' AS [SFCount], 
      count(*) AS [SNCount] 
     FROM sn_core_company 
     WHERE vendor = 1 

     UNION 

     SELECT 'Internal' AS [CompanyType], 
      '' AS [SFCount], 
      count(*) AS [SNCount] 
     FROM sn_core_company 
     WHERE u_internal = 1 

) sub 
GROUP BY businesstype; 
-3

只需在条件中添加全部选择声明

其中SFCount> 0,SNCount> 0按照您的要求

+2

这是行不通的。 OP最终会得到0个结果行。 – JNevill