看看能满足你的要求不。要注意一下字段的统一,编码标记统一给你按文本方式设置了,你要用数字就要将条件中的单引号去掉。
SQL 语句按C++的样式写,以帮助你理解和调试,一对括号就是一个查询,通常从里往外推:
select b.客户,b.定箱编号,b.成套编码,b.中文描述,a.数量2 as 套数,a.金额2 as 金额,a.件数2 as 件数,a.体积2 as 体积,b.编码标记
from
(
select 成套编码,sum(数量) as 数量2,sum(金额) as 金额2,sum(件数) as 件数2,sum(体积) as 体积2
from [tb1$]
where 编码标记='1'
group by 成套编码
) a,
(
select 客户,定箱编号,成套编码,中文描述,编码标记
from [tb1$]
where 编码标记='1'
group by 客户,定箱编号,成套编码,中文描述,编码标记
) b
where a.成套编码=b.成套编码
union all
select b.客户,b.定箱编号,b.产品编码,b.中文描述,a.数量2,a.金额2,a.件数2,a.体积2,b.编码标记
from
(
select 产品编码,sum(数量) as 数量2,sum(金额) as 金额2,sum(件数) as 件数2,sum(体积) as 体积2
from [tb1$]
where 编码标记='0' and 金额>0
group by 产品编码
) a,
(
select 客户,定箱编号,产品编码,中文描述,编码标记
from [tb1$]
where 编码标记='0' and 金额>0
group by 客户,定箱编号,产品编码,中文描述,编码标记
) b
where a.产品编码=b.产品编码
union all
select b.客户,b.定箱编号,b.成套编码,b.中文描述,a.数量2,a.金额2,a.件数2,a.体积2,b.编码标记
from
(
select 成套编码,sum(数量)/count(*) as 数量2,sum(金额) as 金额2,sum(件数) as 件数2,sum(体积) as 体积2
from
(
select *
from [tb1$]
where 编码标记='3'
union all
select *
from [tb1$]
where 编码标记='0' and 金额=0
)
group by 成套编码
) a,
(
select 客户,定箱编号,成套编码,中文描述,编码标记
from [tb1$]
where 编码标记='3'
group by 客户,定箱编号,成套编码,中文描述,编码标记
) b
where a.成套编码=b.成套编码 |