我有一个SQL,它显示了我在jasper报告中所需的不同结果
我有2个相同的行:3个表中的OFFICER和DOC_TYP。我想为每位军官做一个所有doc_typ的总和,并按军官姓名分组。这是我的代码
但最后我的结果是OFFICER名称没有排序,我发现了很多次同名。
SELECT DAT, VAL, TYP, OFFICER, som
FROM ( ------------------1-visa
SELECT VR.DOC_DAT DAT,
VR.DOC_VAL VAL,
VR.DOC_TYP TYP,
VR.OFFICER OFFICER,
COUNT(*) as som
FROM ASYEVR_VRQ_TAB vr
WHERE vr.doc_dat > = to_date($P{p1},'dd/mm/yyyy')
AND vr.doc_dat < = to_date($P{p2},'dd/mm/yyyy')
AND ( ($P{p5} is null)
OR (vr.officer = $P{p5})
)
GROUP BY vr.officer, vr.doc_typ, vr.doc_dat, vr.doc_val
---------------------2-work permit
UNION
SELECT GT.DOC_DAT DAT,
GT.DOC_VAL VAL,
GT.DOC_TYP TYP,
GT.OFFICER OFFICER,
COUNT(*) as som
FROM ASYEVR_ASYWPE_TAB gt
WHERE gt.doc_dat > = to_date($P{p1},'dd/mm/yyyy')
AND gt.doc_dat < = to_date($P{p2},'dd/mm/yyyy')
AND ( ($P{p5} is null)
OR (gt.officer = $P{p5})
)
GROUP BY gt.officer, gt.doc_typ, gt.doc_dat, gt.doc_val
---------------------3-residency
UNION
SELECT RS.DOC_DAT DAT,
RS.DOC_VAL VAL,
RS.DOC_TYP TYP,
RS.OFFICER OFFICER,
COUNT(*) as som
FROM ASYEVR_RESIDENCY_TAB rs
WHERE rs.doc_dat > = to_date($P{p1},'dd/mm/yyyy')
AND rs.doc_dat < = to_date($P{p2},'dd/mm/yyyy')
AND ( ($P{p5} is null)
OR (rs.officer = $P{p5})
)
GROUP BY rs.officer, rs.doc_typ, rs.doc_dat, rs.doc_val
)
<field name="OFFICER" class="java.lang.String"/>
<field name="TYP" class="java.lang.String"/>
<field name="som" class="java.math.BigDecimal"/>
我看到的就像那样
officer DOC_TYP SUM
Mike visa 7
mike visa 8
mike work permit 2
laura work permit 1
laura work permit 2
laura work permit 3
但我需要的就是这样看
mike visa 7
work permit 5
laura visa 9
work permit 2