2

ユニオンから結果を取得するクエリを作成しました。しかし、結果は2行で表示され、空白の列は0のままです。しかし、結果は1行で必要であり、ゼロは必要ありません。誰でもこれについて私を助けてください。以下に、2 つの部分に分けて記述したクエリを示します >>>

 SELECT COUNT(gi.id) AS no_of_group ,
       0 AS no_of_borrower
FROM group_info AS gi
WHERE gi.domain_status_id = 1
  AND gi.group_status_id = 1
  AND gi.group_creation_date <= '2013-01-31'
  AND gi.project_info_id = 'BI0000000000000000000001'
  AND gi.branch_info_id = 'BI0000000000000000000363'
UNION
SELECT 0 AS no_of_group ,
       COUNT(outerlat.member_info_id) AS no_of_borrower
FROM
  (SELECT lat.member_info_id,
          MAX(lat.id) AS max_member_id
   FROM loan_account_transaction AS lat
   WHERE lat.project_info_id = 'BI0000000000000000000001'
     AND lat.country_id = 1
     AND lat.domain_status_id = 1
     AND lat.office_info_id = 'BI0000000000000000000363'
     AND lat.transaction_date <= '2013-01-31'
   GROUP BY lat.member_info_id) AS tempData
INNER JOIN loan_account_transaction AS outerlat ON (tempData.max_member_id = outerlat.id)
WHERE outerlat.loan_status_id IN(1,3,4,5)
4

2 に答える 2

2

代わりに、各列をサブクエリとして選択できます。

SELECT
    (SELECT  COUNT(gi.id)
    FROM    group_info AS gi
    WHERE   gi.domain_status_id = 1 AND 
        gi.group_status_id = 1 AND 
        gi.group_creation_date <= '2013-01-31' AND 
        gi.project_info_id = 'BI0000000000000000000001' AND
        gi.branch_info_id = 'BI0000000000000000000363')  AS no_of_group
    ,(SELECT  COUNT(outerlat.member_info_id)
    FROM
        (SELECT lat.member_info_id, MAX(lat.id) AS max_member_id  FROM loan_account_transaction AS lat 
        WHERE lat.project_info_id = 'BI0000000000000000000001' AND lat.country_id = 1 AND lat.domain_status_id = 1
        AND lat.office_info_id = 'BI0000000000000000000363' AND lat.transaction_date <= '2013-01-31' 
    GROUP BY lat.member_info_id) AS tempData
    INNER JOIN loan_account_transaction AS outerlat 
     ON (tempData.max_member_id = outerlat.id )
    WHERE   outerlat.loan_status_id IN(1,3,4,5)) AS no_of_borrower
于 2013-03-19T12:28:25.567 に答える
1

CTE(共通テーブル式)を使用できます-

With AllData as
( SELECT
    Count -- etc
      AS no_of_group,
    0 AS no_of_borrower 
UNION ALL
  SELECT 
    0 AS no_of_group,
    COUNT -- etc
      AS no_of_borrower 
)
SELECT
  Sum(no_of_group)    as no_of_group,
  Sum(no_of_borrower) as no_of_borrower
FROM AllData
于 2013-03-19T12:29:13.063 に答える