有没有办法在ACCESS中实现同一个表的同一个字段的上下两格相加,并把值表示在另一个字段?
7 个解决方案
#1
将记录及正确结果贴出来,应该没有问题
#2
利用JOIN on id=id-1,然后相加即可,由于不知道你的表结构,给你个思路,剩下的简单了,你自己试一下吧。.
[align=center] = = = = 思 想 重 于 技 巧 = = = =
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
#3
表1:
P/N Date 期初数量
P1 2008-5-1 100
P2 2008-5-1 200
P3 2008-5-1 300
表2:
P/N Receiving Date Qty
P1 2008-5-2 100
P2 2008-5-2 50
P3 2008-5-2 50
表3:
Part ID Trans Date Ship Qty
P1 2008-5-3 100
P2 2008-5-3 20
P3 2008-5-3 50
表4:
P/N Date 期初数量 Received Qty Ship Qty Balance
P1 2008-5-1 100 0 0 100
P1 2008-5-2 0 100 0 200
P1 2008-5-3 0 0 100 100
P2 2008-5-1 200 0 0 200
P2 2008-5-2 0 50 0 250
P2 2008-5-3 0 0 20 230
P3 2008-5-1 300 0 0 300
P3 2008-5-2 0 50 0 350
P3 2008-5-3 0 0 50 300
我就是要得到表4的表.现在有两个问题:
1.如何实现表1、表2和表3中的数据结合进入表4,并按P/N和Date来排序。
2.如何实现对于每个P/N进行计算出Blance.
计算方法:期初数量+Received Qty-Ship Qty+上面一格的Balance=Balance(按P/N来计算,也就是不同的P/N不计算上面一格的Balance)
不知道自己有没有表达清楚.
P/N Date 期初数量
P1 2008-5-1 100
P2 2008-5-1 200
P3 2008-5-1 300
表2:
P/N Receiving Date Qty
P1 2008-5-2 100
P2 2008-5-2 50
P3 2008-5-2 50
表3:
Part ID Trans Date Ship Qty
P1 2008-5-3 100
P2 2008-5-3 20
P3 2008-5-3 50
表4:
P/N Date 期初数量 Received Qty Ship Qty Balance
P1 2008-5-1 100 0 0 100
P1 2008-5-2 0 100 0 200
P1 2008-5-3 0 0 100 100
P2 2008-5-1 200 0 0 200
P2 2008-5-2 0 50 0 250
P2 2008-5-3 0 0 20 230
P3 2008-5-1 300 0 0 300
P3 2008-5-2 0 50 0 350
P3 2008-5-3 0 0 50 300
我就是要得到表4的表.现在有两个问题:
1.如何实现表1、表2和表3中的数据结合进入表4,并按P/N和Date来排序。
2.如何实现对于每个P/N进行计算出Blance.
计算方法:期初数量+Received Qty-Ship Qty+上面一格的Balance=Balance(按P/N来计算,也就是不同的P/N不计算上面一格的Balance)
不知道自己有没有表达清楚.
#4
SELECT F.*,NZ(G.期初数量,0) AS 期初数量,NZ(H.QTY,0) AS [Received Qty],NZ(I.ShipQty,0) AS [Ship Qty] FROM (((
SELECT C.[P/N],C.DATE,
VAL(max(a.期初数量))+VAL(NZ(SUM(D.Qty ),0))-VAL(NZ(SUM(E.ShipQty),0)) AS [Balance]
FROM ((
SELECT A.[P/N],A.期初数量,B.DATE FROM B1 A
LEFT JOIN
(SELECT DATE FROM B1
UNION
SELECT Receiving_Date FROM B2
UNION
SELECT TransDate FROM B3
) B
ON A.DATE<=B.DATE)
C
LEFT JOIN B2 D
ON C.[P/N]=D.[P/N] AND C.DATE>=D.Receiving_Date)
LEFT JOIN B3 E ON C.[P/N]=E.PartID AND C.DATE>=E.TransDate
GROUP BY C.[P/N],C.DATE) F
LEFT JOIN
B1 G ON F.[P/N]=G.[P/N] AND F.DATE=G.DATE)
LEFT JOIN
B2 H ON F.[P/N]=H.[P/N] AND F.DATE=H.[Receiving_Date])
LEFT JOIN
B3 I ON F.[P/N]=I.[PartID] AND F.DATE=I.[TransDate]
SELECT C.[P/N],C.DATE,
VAL(max(a.期初数量))+VAL(NZ(SUM(D.Qty ),0))-VAL(NZ(SUM(E.ShipQty),0)) AS [Balance]
FROM ((
SELECT A.[P/N],A.期初数量,B.DATE FROM B1 A
LEFT JOIN
(SELECT DATE FROM B1
UNION
SELECT Receiving_Date FROM B2
UNION
SELECT TransDate FROM B3
) B
ON A.DATE<=B.DATE)
C
LEFT JOIN B2 D
ON C.[P/N]=D.[P/N] AND C.DATE>=D.Receiving_Date)
LEFT JOIN B3 E ON C.[P/N]=E.PartID AND C.DATE>=E.TransDate
GROUP BY C.[P/N],C.DATE) F
LEFT JOIN
B1 G ON F.[P/N]=G.[P/N] AND F.DATE=G.DATE)
LEFT JOIN
B2 H ON F.[P/N]=H.[P/N] AND F.DATE=H.[Receiving_Date])
LEFT JOIN
B3 I ON F.[P/N]=I.[PartID] AND F.DATE=I.[TransDate]
#5
不明白.怎么这么多表啊?
#6
首先,要生成所有日期,其次要生成累计金额,再与三个表连接,自己试试吧。
#7
明白了,测试成功.谢谢啦...
#1
将记录及正确结果贴出来,应该没有问题
#2
利用JOIN on id=id-1,然后相加即可,由于不知道你的表结构,给你个思路,剩下的简单了,你自己试一下吧。.
[align=center] = = = = 思 想 重 于 技 巧 = = = =
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
#3
表1:
P/N Date 期初数量
P1 2008-5-1 100
P2 2008-5-1 200
P3 2008-5-1 300
表2:
P/N Receiving Date Qty
P1 2008-5-2 100
P2 2008-5-2 50
P3 2008-5-2 50
表3:
Part ID Trans Date Ship Qty
P1 2008-5-3 100
P2 2008-5-3 20
P3 2008-5-3 50
表4:
P/N Date 期初数量 Received Qty Ship Qty Balance
P1 2008-5-1 100 0 0 100
P1 2008-5-2 0 100 0 200
P1 2008-5-3 0 0 100 100
P2 2008-5-1 200 0 0 200
P2 2008-5-2 0 50 0 250
P2 2008-5-3 0 0 20 230
P3 2008-5-1 300 0 0 300
P3 2008-5-2 0 50 0 350
P3 2008-5-3 0 0 50 300
我就是要得到表4的表.现在有两个问题:
1.如何实现表1、表2和表3中的数据结合进入表4,并按P/N和Date来排序。
2.如何实现对于每个P/N进行计算出Blance.
计算方法:期初数量+Received Qty-Ship Qty+上面一格的Balance=Balance(按P/N来计算,也就是不同的P/N不计算上面一格的Balance)
不知道自己有没有表达清楚.
P/N Date 期初数量
P1 2008-5-1 100
P2 2008-5-1 200
P3 2008-5-1 300
表2:
P/N Receiving Date Qty
P1 2008-5-2 100
P2 2008-5-2 50
P3 2008-5-2 50
表3:
Part ID Trans Date Ship Qty
P1 2008-5-3 100
P2 2008-5-3 20
P3 2008-5-3 50
表4:
P/N Date 期初数量 Received Qty Ship Qty Balance
P1 2008-5-1 100 0 0 100
P1 2008-5-2 0 100 0 200
P1 2008-5-3 0 0 100 100
P2 2008-5-1 200 0 0 200
P2 2008-5-2 0 50 0 250
P2 2008-5-3 0 0 20 230
P3 2008-5-1 300 0 0 300
P3 2008-5-2 0 50 0 350
P3 2008-5-3 0 0 50 300
我就是要得到表4的表.现在有两个问题:
1.如何实现表1、表2和表3中的数据结合进入表4,并按P/N和Date来排序。
2.如何实现对于每个P/N进行计算出Blance.
计算方法:期初数量+Received Qty-Ship Qty+上面一格的Balance=Balance(按P/N来计算,也就是不同的P/N不计算上面一格的Balance)
不知道自己有没有表达清楚.
#4
SELECT F.*,NZ(G.期初数量,0) AS 期初数量,NZ(H.QTY,0) AS [Received Qty],NZ(I.ShipQty,0) AS [Ship Qty] FROM (((
SELECT C.[P/N],C.DATE,
VAL(max(a.期初数量))+VAL(NZ(SUM(D.Qty ),0))-VAL(NZ(SUM(E.ShipQty),0)) AS [Balance]
FROM ((
SELECT A.[P/N],A.期初数量,B.DATE FROM B1 A
LEFT JOIN
(SELECT DATE FROM B1
UNION
SELECT Receiving_Date FROM B2
UNION
SELECT TransDate FROM B3
) B
ON A.DATE<=B.DATE)
C
LEFT JOIN B2 D
ON C.[P/N]=D.[P/N] AND C.DATE>=D.Receiving_Date)
LEFT JOIN B3 E ON C.[P/N]=E.PartID AND C.DATE>=E.TransDate
GROUP BY C.[P/N],C.DATE) F
LEFT JOIN
B1 G ON F.[P/N]=G.[P/N] AND F.DATE=G.DATE)
LEFT JOIN
B2 H ON F.[P/N]=H.[P/N] AND F.DATE=H.[Receiving_Date])
LEFT JOIN
B3 I ON F.[P/N]=I.[PartID] AND F.DATE=I.[TransDate]
SELECT C.[P/N],C.DATE,
VAL(max(a.期初数量))+VAL(NZ(SUM(D.Qty ),0))-VAL(NZ(SUM(E.ShipQty),0)) AS [Balance]
FROM ((
SELECT A.[P/N],A.期初数量,B.DATE FROM B1 A
LEFT JOIN
(SELECT DATE FROM B1
UNION
SELECT Receiving_Date FROM B2
UNION
SELECT TransDate FROM B3
) B
ON A.DATE<=B.DATE)
C
LEFT JOIN B2 D
ON C.[P/N]=D.[P/N] AND C.DATE>=D.Receiving_Date)
LEFT JOIN B3 E ON C.[P/N]=E.PartID AND C.DATE>=E.TransDate
GROUP BY C.[P/N],C.DATE) F
LEFT JOIN
B1 G ON F.[P/N]=G.[P/N] AND F.DATE=G.DATE)
LEFT JOIN
B2 H ON F.[P/N]=H.[P/N] AND F.DATE=H.[Receiving_Date])
LEFT JOIN
B3 I ON F.[P/N]=I.[PartID] AND F.DATE=I.[TransDate]
#5
不明白.怎么这么多表啊?
#6
首先,要生成所有日期,其次要生成累计金额,再与三个表连接,自己试试吧。
#7
明白了,测试成功.谢谢啦...