select * from( select (select dep_name from department where dep_id= o.dep_id) depName, out_remark ,sum(out_total_amount) total,out_date From out_warehouse o where dep_id in (select dep_id from department) Group By dep_id Having Count(*) > 1 ORDER BY dep_id ) t1 union all select * from ( select (select dep_name from department where dep_id = o.dep_id) depName, out_remark,sum(out_total_amount) total,out_date From out_warehouse o Group By dep_id Having Count(*) = 1 ORDER BY dep_id ) t2 union all select * from ( select (select dep_name from department where dep_id = o1.dep_id) depName,out_remark,out_total_amount,out_date from out_warehouse o1 where dep_id in ( select dep_id From out_warehouse o where dep_id in (select dep_id from department) Group By dep_id Having Count(*) > 1 ORDER BY dep_id ) order by dep_id ) p1 union all select * from ( select (select dep_name from department where dep_id = o1.dep_id) depName,out_remark,out_total_amount,out_date from out_warehouse o1 where dep_id not in ( select dep_id From out_warehouse o where dep_id in (select dep_id from department) Group By dep_id Having Count(*) > 1 ORDER BY dep_id ) order by dep_id ) p2
查询结果
depName out_remark total out_date
维修科 9号楼修漏 16.84 2015/8/28
供水科 xxx号井用 11.2 2015/8/26
门市部 马步村修漏用5.6 2015/8/28
维修科 9号楼修漏 3.08 2015/8/10
维修科 8号楼修漏 8.16 2015/8/16
维修科 10号楼修漏 5.6 2015/8/21
供水科 xxx号井用 5.6 2015/8/26
供水科 1号楼修漏 5.6 2015/8/28
门市部 马步村修漏用5.6 2015/8/29
需要实现效果是:
查询出总的金额 后面是 详细金额数据
请教sql语句 怎么样实现 和 优化 ?
解决方案
40
这是本人的全部模拟,字段不对的,你跟着本人修改成你本人的。
CREATE TABLE `department` ( `dep_id` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL, `dep_name` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci -- dep_id dep_name 1 维修科 2 供水科 3 门市部 -- CREATE TABLE `out_warehouse` ( `dep_id` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL, `out_remark` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL, `out_total_amount` DECIMAL(18,2) DEFAULT NULL, `out_date` DATE DEFAULT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci -- dep_id out_remark out_total_amount out_date 1 9号楼修漏 3.08 2015-08-10 1 8号楼修漏 8.16 2015-08-16 1 10号楼修漏 5.60 2015-08-21 2 xxx号井用 5.60 2015-08-27 2 1号楼修漏 5.60 2015-08-28 3 马步村修漏用 5.60 2015-08-29 -- SELECT t.dep_name, t.out_remark, t.total, t.out_date FROM ( SELECT ( SELECT COUNT(*)+1 FROM out_warehouse WHERE (dep_id=t1.dep_id AND out_date<t1.out_date) OR (dep_id=t1.dep_id AND out_date<t1.out_date AND out_remark < t1.out_remark) ) AS sort, ( SELECT COUNT(*)+1 FROM out_warehouse WHERE (dep_id=t1.dep_id AND out_date<t1.out_date) OR (dep_id=t1.dep_id AND out_date<t1.out_date AND out_remark < t1.out_remark) ) AS dep_name, t1.dep_id, t1.out_remark, t1.out_total_amount AS total, t1.out_date FROM out_warehouse AS t1 UNION ALL SELECT "0", t2.dep_name, t1.dep_id, "费用合计", t1.total, "" FROM ( SELECT dep_id, SUM(out_total_amount) AS total FROM out_warehouse GROUP BY dep_id ) AS t1 LEFT JOIN department AS t2 ON t1.dep_id= t2.dep_id ) AS t ORDER BY t.dep_id, t.sort -- -- 结果: dep_name out_remark total out_date 维修科 费用合计 16.84 1 9号楼修漏 3.08 2015-08-10 2 8号楼修漏 8.16 2015-08-16 3 10号楼修漏 5.60 2015-08-21 供水科 费用合计 11.20 1 xxx号井用 5.60 2015-08-27 2 1号楼修漏 5.60 2015-08-28 门市部 费用合计 5.60 1 马步村修漏用 5.60 2015-08-29