SQL 用多个聚合函数汇总查询结果
我是一个初学SQL的小白,写了一段代码,想汇总查询一下公司的生产任务完成情况,CLOSE_ID分别在表MF_MO和MF_TZ中,是完工标识。写完了之后提示CLOSE_ID...
我是一个初学SQL的小白,写了一段代码,想汇总查询一下公司的生产任务完成情况,CLOSE_ID分别在表MF_MO 和MF_TZ中,是完工标识。写完了之后提示CLOSE_ID不在聚合函数之中,也不在GROUP BY中,在GROUP BY中加入CLOSE_ID后,显示的结果又乱七八糟的。反正,删掉CASE WHEN的那三个分句,就能得到我想要一部分结果,但是加上之后就出错,请高手教教我怎么个搞法?谢谢哈
SELECT DISTINCT
'计划号'=A.BAT_NO,'订单货品名称'=C.NAME,
'种类总数'=COUNT(DISTINCT A.MO_NO),
'件数总数'=SUM(A.QTY),
'工时总数'=SUM(B.USED_TIME),
'完成件数'=(CASE WHEN (A.CLOSE_ID='T') THEN SUM(DISTINCT A.QTY) ELSE 0 END),
'完成种数'=(CASE WHEN (A.CLOSE_ID='T') THEN COUNT(DISTINCT A.MO_NO) ELSE 0 END),
'完成工时'=(CASE WHEN (B.CLOSE_ID='T') THEN SUM(DISTINCT B.USED_TIME) ELSE 0 END)
FROM MF_MO A LEFT JOIN MF_TZ B ON A.MO_NO=B.MO_NO
LEFT JOIN PRDT C ON A.MRP_NO_SO=C.PRD_NO
WHERE BIL_MAK='T'
GROUP BY A.BAT_NO,C.NAME
ORDER BY A.BAT_NO,C.NAME 展开
SELECT DISTINCT
'计划号'=A.BAT_NO,'订单货品名称'=C.NAME,
'种类总数'=COUNT(DISTINCT A.MO_NO),
'件数总数'=SUM(A.QTY),
'工时总数'=SUM(B.USED_TIME),
'完成件数'=(CASE WHEN (A.CLOSE_ID='T') THEN SUM(DISTINCT A.QTY) ELSE 0 END),
'完成种数'=(CASE WHEN (A.CLOSE_ID='T') THEN COUNT(DISTINCT A.MO_NO) ELSE 0 END),
'完成工时'=(CASE WHEN (B.CLOSE_ID='T') THEN SUM(DISTINCT B.USED_TIME) ELSE 0 END)
FROM MF_MO A LEFT JOIN MF_TZ B ON A.MO_NO=B.MO_NO
LEFT JOIN PRDT C ON A.MRP_NO_SO=C.PRD_NO
WHERE BIL_MAK='T'
GROUP BY A.BAT_NO,C.NAME
ORDER BY A.BAT_NO,C.NAME 展开
2个回答
展开全部
SELECT DISTINCT
'计划号'=A.BAT_NO,'订单货品名称'=C.NAME,
'种类总数'=COUNT(DISTINCT A.MO_NO),
'件数总数'=SUM(A.QTY),
'工时总数'=SUM(B.USED_TIME),
'完成件数'=SUM(DISTINCT A.QTY) ,
'完成种数'=COUNT(DISTINCT A.MO_NO) ,
'完成工时'=SUM(DISTINCT B.USED_TIME)
FROM MF_MO A LEFT JOIN MF_TZ B ON A.MO_NO=B.MO_NO
LEFT JOIN PRDT C ON A.MRP_NO_SO=C.PRD_NO
WHERE BIL_MAK='T' and A.CLOSE_ID='T' and B.CLOSE_ID='T'
GROUP BY A.BAT_NO,C.NAME
ORDER BY A.BAT_NO,C.NAME
看这样是你想要的结果吗?
'计划号'=A.BAT_NO,'订单货品名称'=C.NAME,
'种类总数'=COUNT(DISTINCT A.MO_NO),
'件数总数'=SUM(A.QTY),
'工时总数'=SUM(B.USED_TIME),
'完成件数'=SUM(DISTINCT A.QTY) ,
'完成种数'=COUNT(DISTINCT A.MO_NO) ,
'完成工时'=SUM(DISTINCT B.USED_TIME)
FROM MF_MO A LEFT JOIN MF_TZ B ON A.MO_NO=B.MO_NO
LEFT JOIN PRDT C ON A.MRP_NO_SO=C.PRD_NO
WHERE BIL_MAK='T' and A.CLOSE_ID='T' and B.CLOSE_ID='T'
GROUP BY A.BAT_NO,C.NAME
ORDER BY A.BAT_NO,C.NAME
看这样是你想要的结果吗?
展开全部
既然group by就不必再用distinct了
SELECT '计划号'=A.BAT_NO,
'订单货品名称'=C.NAME,
'种类总数'=COUNT(A.MO_NO),
'件数总数'=SUM(A.QTY),
'工时总数'=SUM(B.USED_TIME),
'完成件数'=SUM(CASE WHEN A.CLOSE_ID='T' THEN A.QTY ELSE 0 END),
'完成种数'=SUM(CASE WHEN A.CLOSE_ID='T' THEN A.MO_NO ELSE 0 END),
'完成工时'=SUM(CASE WHEN B.CLOSE_ID='T' THEN B.USED_TIME ELSE 0 END)
FROM MF_MO A LEFT JOIN MF_TZ B ON A.MO_NO=B.MO_NO
LEFT JOIN PRDT C ON A.MRP_NO_SO=C.PRD_NO
WHERE BIL_MAK='T'
GROUP BY A.BAT_NO,C.NAME
ORDER BY A.BAT_NO,C.NAME
SELECT '计划号'=A.BAT_NO,
'订单货品名称'=C.NAME,
'种类总数'=COUNT(A.MO_NO),
'件数总数'=SUM(A.QTY),
'工时总数'=SUM(B.USED_TIME),
'完成件数'=SUM(CASE WHEN A.CLOSE_ID='T' THEN A.QTY ELSE 0 END),
'完成种数'=SUM(CASE WHEN A.CLOSE_ID='T' THEN A.MO_NO ELSE 0 END),
'完成工时'=SUM(CASE WHEN B.CLOSE_ID='T' THEN B.USED_TIME ELSE 0 END)
FROM MF_MO A LEFT JOIN MF_TZ B ON A.MO_NO=B.MO_NO
LEFT JOIN PRDT C ON A.MRP_NO_SO=C.PRD_NO
WHERE BIL_MAK='T'
GROUP BY A.BAT_NO,C.NAME
ORDER BY A.BAT_NO,C.NAME
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询