期初结存sql的问题(100分)

  • 主题发起人 handsome1234
  • 开始时间
H

handsome1234

Unregistered / Unconfirmed
GUEST, unregistred user!
table: input a1,a2,a3 名称、数量、日期
output b1,b2,b3 名称、数量、日期
表input中有a1,a2,a3,只有a2是数字型,其他均为字符型,
表output有b1,b2,b3,只有b2是数值型,其他均为字符型
数据库为access
要求期初结存:a3=20020801入库以前的 ,减去b3=20020801出库的,即为期初结存
我写了如下代码,可是通不过,还望高人帮帮忙,指出一下是那里的问题
select input.a1,input1.a22 - output1.b22
from (select input.a1 as a11,input.a2 as a22,input.a3 as a33
from input where input.a3 <'20020801' ) input1
left join
( select output.b1 as b11,output.b2 as b22,output.b3 as b33
from output where output.b3 <'20020801' ) output1 on input1.a11=output1.b11
group by input.a1
错误信息如下:
[Microsoft][ODBC Microsoft Access 驱动程序] 试图执行的查询中不包含作为合计函数一部分的特定表达式 'input1.a22-output1.b22' 。
其中左、右连接的子查询我已经独立的运行过,结果是正确的,
 
应该为
select input.a1, SUM(input1.a22 - output1.b22)
from (select input.a1 as a11,input.a2 as a22,input.a3 as a33
from input where input.a3 <'20020801' ) input1
left join
( select output.b1 as b11,output.b2 as b22,output.b3 as b33
from output where output.b3 <'20020801' ) output1 on input1.a11=output1.b11
group by input.a1
 
结果是:
[Microsoft][ODBC Microsoft Access 驱动程序] 参数不足,期待是 1。
 
看这个 样子是 有的 字段 不 存在 或是 哪个 字段 拼写不对.

>> [Microsoft][ODBC Microsoft Access 驱动程序] 参数不足,期待是 1。

我以前也有 时 碰到这样的 问题.
 
sorry, 你的数据结构还未说清楚,为什么不把input和output放一个表?

算是设计上的失误

放一个表,加一个标志字段,值为I或O,且数量是正数或负数。要求总数,正负一抵就可以了
 
我最初的代码不真确,所以大家不必衣我的写法,

只要能达到目的就行了,

期初结存:a3=20020801入库以前的 ,减去b3=20020801出库的,即为期初结存
 
老大,表你就不用帮我了把,我是说sql啊
 
这个怎样:
Select a1,Sum(a2) As BeginAmount
From (
Select a1,a2 From Input Where a3<=''20020801''
Union All
Select b1,-b2 From Output Where b3<=''20020801''
) Group By a1


得出所有货品的期初
 
to :glhglhglh,
你的结果不对,少了一些不同品名的记录
 
SELECT a1, sum(a2-a4) from (SELECT a1, a2, 0 as a4, a3 from input where a3<'20020801'
union
select b1 as a1, 0 as a2, b2 as a4, b3 as a3 from output where b3<'20020801')
group by a1

这个思路和:天空还下着沙:的相似,把两张表模拟成一张表即可

try it

 
>>select input.a1,input1.a22 - output1.b22
>>from (select input.a1 as a11,input.a2 as a22,input.a3 as a33
>> from input where input.a3 <'20020801' ) input1
>> left join
>> ( select output.b1 as b11,output.b2 as b22,output.b3 as b33
>> from output where output.b3 <'20020801' ) output1 on input1.a11=output1.b11
>>group by input.a1
select a.a1,a.a22-b.b22
from a left join b on a.a11=b.a11
and a.a3<'20020802' and b.a3<'20020802'
 
多人接受答案了。
 

Similar threads

H
回复
8
查看
84
handsome1234
H
L
回复
8
查看
80
house_txw
H
回复
13
查看
100
风中的狼
顶部