我有一个查询,并且使用with rollup
进行报告。现在,我需要跳过要汇总的列,不需要以下查询中的额外摘要行,例如part
SELECT coalesce(K.ShipName,'Grand Total')ShipName, K.AREA_ID, coalesce(K.PostingName,'Ship Total')PostingName, coalesce(K.Branch,'Unit Total')Branch, coalesce(K.RANK_NAME,'Branch Total')RANK_NAME , K.Part, ifnull(sum(K.Borne),0)Borne, ifnull(sum(K.sanction),0)sanction, sum(K.TotalIn)TotalIn, sum(K.TotalOut)TotalOut
FROM (
SELECT ShipName, AREA_ID, PostingName,Branch, RANK_NAME, ifnull(Part,'') Part, Borne, sanction, TotalIn, TotalOut
FROM (
SELECT sh.NAME ShipName,sh.AREA_ID, pu.NAME PostingName, b.BRANCH_NAME Branch, r.RANK_NAME , p.Name Part, COUNT(SAILORID)Borne,
(SELECT SUM(us.SanctionNo)sanction FROM unitwisesanction us WHERE us.RankID = s.RANKID AND us.PostingUnitID = s.POSTINGUNITID
AND us.PartIIID = s.FIRSTPARTID GROUP BY us.PostingUnitID,us.RankID,us.PartIIID) sanction,
(SELECT COUNT(t.TransferID)TotalOut FROM transfer t LEFT JOIN sailor ts ON t.SailorID = ts.SAILORID WHERE ts.RANKID = s.RANKID
AND ts.FIRSTPARTID = s.FIRSTPARTID AND t.PostingUnitID = s.POSTINGUNITID )TotalIn,
(SELECT COUNT(t.TransferID)TotalOut FROM transfer t LEFT JOIN sailor ts ON t.SailorID = ts.SAILORID WHERE ts.RANKID = s.RANKID
AND ts.FIRSTPARTID = s.FIRSTPARTID AND ts.POSTINGUNITID = s.POSTINGUNITID )TotalOut
FROM sailor s
LEFT JOIN bn_branch b ON s.BRANCHID = b.BRANCH_ID
LEFT JOIN bn_rank r ON s.RANKID = r.RANK_ID
LEFT JOIN partii p ON s.FIRSTPARTID = p.PartIIID
LEFT JOIN bn_daogroup a ON b.DAO_GROUPID = a.GROUP_ID
LEFT JOIN bn_ship_establishment sh ON s.SHIPESTABLISHMENTID = sh.SHIP_ESTABLISHMENTID
LEFT JOIN bn_posting_unit pu ON s.POSTINGUNITID = pu.POSTING_UNITID
WHERE SAILORSTATUS = 1 AND s.ZONEID IN (1) AND s.SHIPESTABLISHMENTID IN (53,125) AND a.GROUP_ID IN (1,2,3,4,5,7)
GROUP BY s.POSTINGUNITID, RANKID, FIRSTPARTID) a
) K
GROUP BY ShipName, PostingName, Branch, RANK_NAME, Part WITH ROLLUP
HAVING Part IS NOT NULL OR RANK_NAME IS NULL
在下面的屏幕截图中,我确实不需要颜色标记的记录
有人可以帮助我吗?
答案 0 :(得分:0)
我已经通过使用以下查询解决了这个问题
SELECT coalesce(K.ShipName,'Area Total')ShipName, K.AREA_ID, coalesce(K.PostingName,'Ship Total')PostingName, coalesce(K.Branch,'Unit Total')Branch, coalesce(K.RANK_NAME,'Branch Total')RANK_NAME , K.Part, sum(K.Borne)Borne, sum(K.sanction)sanction, sum(K.TotalIn)TotalIn, sum(K.TotalOut)TotalOut
FROM (
SELECT ShipName, AREA_ID, PostingName,Branch, RANK_NAME, ifnull(Part,'') Part, Borne, sanction, TotalIn, TotalOut
FROM (
SELECT sh.NAME ShipName,sh.AREA_ID, pu.NAME PostingName, b.BRANCH_NAME Branch, r.RANK_NAME , p.Name Part, COUNT(SAILORID)Borne,
(SELECT SUM(us.SanctionNo)sanction FROM unitwisesanction us WHERE us.RankID = s.RANKID AND us.PostingUnitID = s.POSTINGUNITID
AND us.PartIIID = s.FIRSTPARTID GROUP BY us.PostingUnitID,us.RankID,us.PartIIID) sanction,
(SELECT COUNT(t.TransferID)TotalOut FROM transfer t LEFT JOIN sailor ts ON t.SailorID = ts.SAILORID WHERE ts.RANKID = s.RANKID
AND ts.FIRSTPARTID = s.FIRSTPARTID AND t.PostingUnitID = s.POSTINGUNITID )TotalIn,
(SELECT COUNT(t.TransferID)TotalOut FROM transfer t LEFT JOIN sailor ts ON t.SailorID = ts.SAILORID WHERE ts.RANKID = s.RANKID
AND ts.FIRSTPARTID = s.FIRSTPARTID AND ts.POSTINGUNITID = s.POSTINGUNITID )TotalOut
FROM sailor s
LEFT JOIN bn_branch b ON s.BRANCHID = b.BRANCH_ID
LEFT JOIN bn_rank r ON s.RANKID = r.RANK_ID
LEFT JOIN partii p ON s.FIRSTPARTID = p.PartIIID
LEFT JOIN bn_daogroup a ON b.DAO_GROUPID = a.GROUP_ID
LEFT JOIN bn_ship_establishment sh ON s.SHIPESTABLISHMENTID = sh.SHIP_ESTABLISHMENTID
LEFT JOIN bn_posting_unit pu ON s.POSTINGUNITID = pu.POSTING_UNITID
WHERE SAILORSTATUS = 1 AND s.ZONEID IN (1) AND s.SHIPESTABLISHMENTID IN (53,125) AND a.GROUP_ID IN (1,2,3,4,5,7)
GROUP BY s.POSTINGUNITID, RANKID, FIRSTPARTID) a
) K
GROUP BY ShipName, PostingName, Branch, RANK_NAME, Part WITH ROLLUP
HAVING Part IS NOT NULL OR RANK_NAME IS NULL