2017-04-18 133 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