需要帮助将两个查询合并为一个

时间:2017-11-02 13:35:25

标签: sql sql-server

我需要一些帮助才能将两个不同的查询合并到一个查询中;下面是我正在使用的查询以及单个查询的输出。

查询1:

SELECT 
   count( *) TestCount,
   asset 
FROM hsbc_csmip_dbprotect 
group by asset

输出1:

     TestCount                       Asset
         18                     DB2MOTIV:DBMOTIVA@mxemch010405ads.mx.hsbc
         18                     IBASL01:BASL01@gbl05823.systems.uk.hsbc
         17                     ICOGP01:ITSCOG01@htng-icogp01.de.hsbc
         18                     ICTIP01:CTIP01@arp617fuc3db.ar.hsbc
         18                     IPSSP01:PSSP01@arp498fuc1db.ar.hsbc
         16                     IMXGP01:ITSMXG01@htng-imxgp01.de.hsbc
         19                     IINFP01:INFP01@gbl10805.systems.uk.hsbc
          9                     IHF4P01:HF4P01@mxcl0030006-dbai.mx.hsbc
         19                     IHQAL01:HQAL02@gbl07664.systems.uk.hsbc

查询2:

SELECT 
   count( *) TestPassedCount,
   asset 
FROM hsbc_csmip_dbprotect 
where result_status = 'Not A Finding' 
group by asset

输出2:

   TestPassedCount                Asset
          7                     DB2MOTIV:DBMOTIVA@mxemch010405ads.mx.hsbc
         16                     IBASL01:BASL01@gbl05823.systems.uk.hsbc
         11                     ICOGP01:ITSCOG01@htng-icogp01.de.hsbc
         10                     ICTIP01:CTIP01@arp617fuc3db.ar.hsbc
         11                     IPSSP01:PSSP01@arp498fuc1db.ar.hsbc
          9                     IMXGP01:ITSMXG01@htng-imxgp01.de.hsbc
         17                     IINFP01:INFP01@gbl10805.systems.uk.hsbc
          9                     IHF4P01:HF4P01@mxcl0030006-dbai.mx.hsbc
         18                     IHQAL01:HQAL02@gbl07664.systems.uk.hsbc

我希望看到输出如下

      TestCount          TestPassedCount        Asset
         18                      7              DB2MOTIV:DBMOTIVA@mxemch010405ads.mx.hsbc
         18                      16             IBASL01:BASL01@gbl05823.systems.uk.hsbc
         17                      11             ICOGP01:ITSCOG01@htng-icogp01.de.hsbc
         18                      10             ICTIP01:CTIP01@arp617fuc3db.ar.hsbc
         18                      11             IPSSP01:PSSP01@arp498fuc1db.ar.hsbc
         16                       9             IMXGP01:ITSMXG01@htng-imxgp01.de.hsbc
         19                      17             IINFP01:INFP01@gbl10805.systems.uk.hsbc
          9                       9             IHF4P01:HF4P01@mxcl0030006-dbai.mx.hsbc
         19                      18             IHQAL01:HQAL02@gbl07664.systems.uk.hsbc

2 个答案:

答案 0 :(得分:3)

由于它们是同一个表,因此您可以使用案例陈述。

SELECT 
    count( *) TestCount
    ,count(case when result_status = 'Not A Finding' then 1 end) TestPassedCount
    ,count(case when result_status = 'Not A Finding' then 1 end) / count(*) ThePercentage
    ,asset 
FROM hsbc_csmip_dbprotect
group by asset

答案 1 :(得分:0)

您可以使用以下CASE语句:

SELECT count(*) AS TestCount
    ,count(case when result_status = 'Not A Finding' then 1 else 0 end) AS TestPassedCount, 
    asset 
FROM hsbc_csmip_dbprotect
group by asset