56,678
社区成员
发帖
与我相关
我的任务
分享
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
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
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, '0'
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
在UNION ALL 数据集的查询中,將 dep_id 添加进去。
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, '0'
FROM
(
SELECT 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
SELECT t2.dep_name, t1.out_remark, t1.total, t1.out_date FROM
(
SELECT dep_id, out_remark, SUM(out_total_amount) AS total, out_date
FROM out_warehouse
GROUP BY dep_id
UNION ALL
SELECT dep_id, out_remark, out_total_amount, out_date
FROM out_warehouse
) AS t1
LEFT JOIN department AS t2
ON t1.dep_id= t2.dep_id