|
楼主 |
发表于 2009-6-7 22:02
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
提供两种SQL语句的写法:
写法1:
select * from
(
select W, Z,存货名称,单位,
(
select sum(D) from
(
select 存货编码 as W,year('1/1/2000')&"-"& month('1/1/2000') as Z,datevalue('1/1/2000')*1 as T,数量*1 as D, 金额*1 as P from [期初余额$]
UNION ALL
SELECT 存货编码, year(入库日期)&"-"& month(入库日期),datevalue(入库日期)*1 as T,数量*1,单价 *数量 FROM [入库明细表$]
UNION ALL
SELECT 存货编码, year(出库日期)&"-"& month(出库日期),datevalue(出库日期)*1 , 数量*(-1),单价*数量*(-1)
FROM [出库明细表$]
)
where T<=b.S and W=b.W
) as D2,
(
select sum(P) from
(
select 存货编码 as W,year('1/1/2000')&"-"& month('1/1/2000') as Z,datevalue('1/1/2000')*1 as T,数量*1 as D, 金额*1 as P
from [期初余额$]
UNION ALL
SELECT 存货编码, year(入库日期)&"-"& month(入库日期),datevalue(入库日期)*1 as T,数量*1,单价 *数量 FROM [入库明细表$]
UNION ALL
SELECT 存货编码, year(出库日期)&"-"& month(出库日期),datevalue(出库日期)*1 , 数量*(-1),单价*数量*(-1)
FROM [出库明细表$]
)
where T<=b.S and W=b.W
) as P2
from
(
select * from
(
select Z,max(T) as S from
(
SELECT '1/1/2000' as R,year(R)&"-"& month(R) as Z,datevalue(R)*1 as T
from [期初余额$]
UNION ALL
SELECT 入库日期 as R,year(R)&"-"& month(R),datevalue(R)*1
FROM [入库明细表$]
UNION ALL
SELECT 出库日期 as R,year(R)&"-"& month(R),datevalue(R)*1
FROM [出库明细表$]
)
group by Z
) e,
(
SELECT 存货编码 as W
from [期初余额$]
UNION
SELECT 存货编码
FROM [入库明细表$]
UNION
SELECT 存货编码
FROM [出库明细表$]
)
) b, [物料编码$] f
where b.W=f.存货编码
)
where D2<>0
语句分析:
1、1提取所有存货编码:
SELECT 存货编码 as W
from [期初余额$]
UNION
SELECT 存货编码
FROM [入库明细表$]
UNION
SELECT 存货编码
FROM [出库明细表$]
1、2:
提取每个月最大日期:
select Z,max(T) as S from
(
SELECT '1/1/2000' as R,year(R)&"-"& month(R) as Z,datevalue(R)*1 as T
from [期初余额$]
UNION ALL
SELECT 入库日期 as R,year(R)&"-"& month(R),datevalue(R)*1
FROM [入库明细表$]
UNION ALL
SELECT 出库日期 as R,year(R)&"-"& month(R),datevalue(R)*1
FROM [出库明细表$]
)
group by Z
1、3 将存货编码、月份和最大日期组合起来,并命名为b表:
select * from
(
select Z,max(T) as S from
(
SELECT '1/1/2000' as R,year(R)&"-"& month(R) as Z,datevalue(R)*1 as T
from [期初余额$]
UNION ALL
SELECT 入库日期 as R,year(R)&"-"& month(R),datevalue(R)*1
FROM [入库明细表$]
UNION ALL
SELECT 出库日期 as R,year(R)&"-"& month(R),datevalue(R)*1
FROM [出库明细表$]
)
group by Z
) e,
(
SELECT 存货编码 as W
from [期初余额$]
UNION
SELECT 存货编码
FROM [入库明细表$]
UNION
SELECT 存货编码
FROM [出库明细表$]
)
1、4
对b表中的最大日期为最大值进行数量累加和金额累加,并提取出物料编码表中的其他字段:
select W, Z,存货名称,单位,
(
select sum(D) from
(
select 存货编码 as W,year('1/1/2000')&"-"& month('1/1/2000') as Z,datevalue('1/1/2000')*1 as T,数量*1 as D, 金额*1 as P from [期初余额$]
UNION ALL
SELECT 存货编码, year(入库日期)&"-"& month(入库日期),datevalue(入库日期)*1 as T,数量*1,单价 *数量 FROM [入库明细表$]
UNION ALL
SELECT 存货编码, year(出库日期)&"-"& month(出库日期),datevalue(出库日期)*1 , 数量*(-1),单价*数量*(-1)
FROM [出库明细表$]
)
where T<=b.S and W=b.W
) as D2,
(
select sum(P) from
(
select 存货编码 as W,year('1/1/2000')&"-"& month('1/1/2000') as Z,datevalue('1/1/2000')*1 as T,数量*1 as D, 金额*1 as P
from [期初余额$]
UNION ALL
SELECT 存货编码, year(入库日期)&"-"& month(入库日期),datevalue(入库日期)*1 as T,数量*1,单价 *数量 FROM [入库明细表$]
UNION ALL
SELECT 存货编码, year(出库日期)&"-"& month(出库日期),datevalue(出库日期)*1 , 数量*(-1),单价*数量*(-1)
FROM [出库明细表$]
)
where T<=b.S and W=b.W
) as P2
from
(
。。。。。
) b, [物料编码$] f
where b.W=f.存货编码
1、5 整个语句
对各个月的累加数据做0值处理:
select * from
(
。。。。。。
)
where D2<>0
写法2:
select w,z,D2,P2,存货名称,单位 from
(
select a.w,b.z, sum(D) as D2,sum(P) as P2
from
(
select 存货编码 as W,datevalue('1/1/2000')*1 as T,format(T,'yyyy-mm') as z,数量*1 as D, 金额*1 as P
from [期初余额$]
UNION ALL
SELECT 存货编码,datevalue(入库日期)*1 as T,format(T,'yyyy-mm'),数量*1,单价 *数量 FROM [入库明细表$]
UNION ALL
SELECT 存货编码,datevalue(出库日期)*1 as T,format(T,'yyyy-mm'), 数量*(-1),单价*数量*(-1)
FROM [出库明细表$]
) a,
(
select Z
from
(
SELECT datevalue('1/1/2000')*1 as T,format(T,'yyyy-mm') as z
from [期初余额$]
UNION ALL
SELECT datevalue(入库日期)*1 as T,format(T,'yyyy-mm')
FROM [入库明细表$]
UNION ALL
SELECT datevalue(出库日期)*1 as T,format(T,'yyyy-mm')
FROM [出库明细表$]
)
group by Z
) b
where a.z<=b.z
group by a.w,b.z
) j,[物料编码$] f
where D2<>0 and j.W=f.存货编码
语句分析:
2、1 提取年月出来,因为年月用字符型表示,为了进行数据的累加,必须使年月具有相同的字符长度,这里就必须用format(T,'yyyy-mm'),而不能用format(T,'yyyy-m')
SELECT datevalue('1/1/2000')*1 as T,format(T,'yyyy-mm') as z
from [期初余额$]
UNION ALL
SELECT datevalue(入库日期)*1 as T,format(T,'yyyy-mm')
FROM [入库明细表$]
UNION ALL
SELECT datevalue(出库日期)*1 as T,format(T,'yyyy-mm')
FROM [出库明细表$]
2、2
对年月去唯一值
select Z
from
(
。。。
)
group by Z
2、3
对期初余额、入库明细表和出库明细表进行数据组合:
select 存货编码 as W,datevalue('1/1/2000')*1 as T,format(T,'yyyy-mm') as z,数量*1 as D, 金额*1 as P
from [期初余额$]
UNION ALL
SELECT 存货编码,datevalue(入库日期)*1 as T,format(T,'yyyy-mm'),数量*1,单价 *数量 FROM [入库明细表$]
UNION ALL
SELECT 存货编码,datevalue(出库日期)*1 as T,format(T,'yyyy-mm'), 数量*(-1),单价*数量*(-1)
FROM [出库明细表$]
2、4
对年月前面的数据进行累加,这里同写法1的最不同点,写法1是里层求和条件是小于或等于外层的条件,此处是将所有记录组合,按条件进行筛选,网撒得更远,再用聚合函数收回来。
select a.w,b.z, sum(D) as D2,sum(P) as P2
from
(
。。。
) a,
(
。。。
) b
where a.z<=b.z
group by a.w,b.z
2、5
做0值处理和关联物料编码的一些字段:
select w,z,D2,P2,存货名称,单位 from
(
。。。
) j,[物料编码$] f
where D2<>0 and j.W=f.存货编码
[ 本帖最后由 Scarlett_88 于 2009-8-10 22:55 编辑 ] |
|