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
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
#6
根据部门分组,时间升序,生成一个排序的字段出来
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
#7
厉害 膜拜
#8
加入 order by t1.dep_id,t2.dep_id,total desc 跟四条 多链接查询效果相同 。
#9
厉害 膜拜
是你想复杂了。。。
而且, order by t1.dep_id,t2.dep_id,total desc 排序,也跟 order by t1.dep_id, total desc 是一样的額。
#10
根据部门分组,时间升序,生成一个排序的字段出来
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
代码结构合理 不过报了个小错误
#11
厉害 膜拜
是你想复杂了。。。
而且, order by t1.dep_id,t2.dep_id,total desc 排序,也跟 order by t1.dep_id, total desc 是一样的額。
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 添加进去。
#14
厉害 膜拜
是你想复杂了。。。
而且, order by t1.dep_id,t2.dep_id,total desc 排序,也跟 order by t1.dep_id, total desc 是一样的額。
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
--------------------------------------------------
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
--------------------------------------------------
--------------------------------------------------
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
--------------------------------------------------
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
#6
根据部门分组,时间升序,生成一个排序的字段出来
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
#7
你那四个查询数据集,跟下面的查询结果是否一致?
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
厉害 膜拜
#8
根据部门分组,时间升序,生成一个排序的字段出来
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
厉害 膜拜
加入 order by t1.dep_id,t2.dep_id,total desc 跟四条 多链接查询效果相同 。
#9
厉害 膜拜
是你想复杂了。。。
而且, order by t1.dep_id,t2.dep_id,total desc 排序,也跟 order by t1.dep_id, total desc 是一样的額。
#10
根据部门分组,时间升序,生成一个排序的字段出来
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
代码结构合理 不过报了个小错误
#11
厉害 膜拜
是你想复杂了。。。
而且, order by t1.dep_id,t2.dep_id,total desc 排序,也跟 order by t1.dep_id, total desc 是一样的額。
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 添加进去。
#14
厉害 膜拜
是你想复杂了。。。
而且, order by t1.dep_id,t2.dep_id,total desc 排序,也跟 order by t1.dep_id, total desc 是一样的額。
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
--------------------------------------------------
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
--------------------------------------------------
--------------------------------------------------
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
--------------------------------------------------