无法使用 UNION ALL 解决 postgresql 问题

Mel*_*nda 0 postgresql

我在 postgresql 9.2.15 中工作,我试图在我的输出中创建一个 SUBTOTAL ROW。我在我的 UNION ALL 中遇到了这个错误,我尝试在应用了 COUNT 的列中通过转换为 TEXT 来修复它,但我仍然遇到相同的错误。
任何帮助/方向将不胜感激。谢谢你。

这是我的错误:

ERROR:  UNION types numeric and text cannot be matched
LINE 117: SELECT II.school_id || ' Subtotal', null, null, null, null, ...
                 ^
********** Error **********

ERROR: UNION types numeric and text cannot be matched
SQL state: 42804
Character: 4725 
Run Code Online (Sandbox Code Playgroud)

这是我的查询代码:

SELECT II.school_id, II.syear, II.student_id, II.last_name, 
     II.first_name,
     II.AddressSchool, II.EnrolledSchool, II.Income_SchoolYear, 
     II.NumberInHousehold, 
     II.HouseholdMember, II.PayType, II.Income, II.AddedOn, II.AddedBy,
     II.CompletedEarlyChildhoodDocumentation, II.ApprovedByPrincipal, 
     II.ApprovedByEarlyChildhood,
     QI.QualifyingInfo_SchoolYear, QI.MethodOfQualifying, QI.Notes, 
     QI.ApprovedBy, QI.ApprovedDate
FROM II
INNER JOIN QI
ON II.student_id = QI.student_id
WHERE II.ApprovedByPrincipal IS NULL
AND II.ApprovedByEarlyChildhood IS NULL
UNION ALL
SELECT II.school_id || ' Subtotal', null, null, null, null,  
    null, null, null, null, 
    null, null, null, null, null,
    null, count(II.ApprovedByPrincipal)::text, null,
    null, null, null, null, null
FROM II
INNER JOIN QI
ON II.student_id = QI.student_id
WHERE II.ApprovedByPrincipal IS NOT NULL
AND II.ApprovedByEarlyChildhood IS NULL
GROUP by 1
ORDER BY II.school_id, II.student_id, II.last_name asc, II.first_name
Run Code Online (Sandbox Code Playgroud)

Alb*_*ert 5

我通过手动将 null 设置为 int4 来解决这个问题。例如,

select 
first name,
last name,
address,
quantity
from table1
union all
first name,
last name,
null::int4 as quantity
from table2
Run Code Online (Sandbox Code Playgroud)