【发布时间】:2019-09-22 06:21:12
【问题描述】:
我正在寻找正确的语法
我可以设置不同的查询,但我正在寻找一个查询
表格:
T1
Country Metal Date Pnum
Austria Gold 20-Dec-18 P31182
Austria Gold 20-Dec-18 P31181
Belgium Grass 6-Dec-17 P30703
Italy Gold 16-Nov-17 P30702
Zimbabwe Copper 8-Jul-17 P29999
T2
Pnum T Week Type Color Descr Status Buy
P31182 2 2019-23 Apple Black Description one Ready 200
P31181 1 2019-23 Apple Black Description two Ready 150
P30703 2 2019-28 Apple Black Description four Waiting 120
T3
Work Hr Pr Pnum
AFNA 4 3.50 P31182
CONS 3 1.25 P31182
TREX 2.5 3.00 P31182
AFNA 3 3.50 P31181
CONS 3 1.25 P31181
TREX 2.5 3.00 P31181
AFNA 4 3.50 P30703
MEET 5 5.00 P30703
TREX 2.5 3.00 P30703
T4
Used Pnum
3010.50 P31182
500.00 P31182
2254.47 P31181
555.55 P30703
155.25 P30703
期望的结果:(减去括号中的计算,这是为了清楚起见)
Country Date Week Type Color Metal Pnum Descr Status Hr*T Hr*T*Pr Used*T Buy*T Work
Austria 20-Dec-18 2019-23 Apple Black Gold P31182 Description one Ready 19 50.50 7021 400
Austria (4*2) (4*2*3.50) (3010.50*2) (200*2) AFNA
Austria (3*2) (3*2*1.25) (500*2) CONS
Austria (2.5*2) (2.5*2*3) TREX
Austria 20-Dec-18 2019-23 Apple Black Gold P31181 Description two Ready 8.5 25.50 2254.47 150
Austria (2.5*1) (2.5*1*3.00) (2254.47*1) (150*1) AFNA
Austria (3*1) (3*1*3.50) CONS
Austria (2.5*1) (2.5*1*3.00) TREX
Belgium 6-Dec-17 2019-28 Apple Black Grass P30703 Description four Waiting 23 93.00 1421.60 240
Belgium (4*2) (4*2*3.50) (555.55*2) (120*2) AFNA
Belgium (5*2) (5*2*5.00) (155.25) MEET
Belgium (2.5*2) (2.5*2*3) TREX
如果我这样做,我会在 SUM 列下得到错误的总数
SELECT Country, Date, Week, Type, Color, Metal, Pnum, Desc, Status, SUM(Hr*T), SUM(Hr*T*Pr), (Used*T), (Buy*T), Work
FROM T2
INNER JOIN T1 table1 on table1.Pnum = table2.Pnum
INNER JOIN T3 table3 on table3.Pnum = table2.Pnum
INNER JOIN T4 table4 on table4.Pnum = table2.Pnum
GROUP BY Pnum
编辑 抱歉,我忘了在第一个表中包含一个 Pnum。我会添加它,并会玩弄答案。 表 T1 不是很重要。它只需要那里的一些信息,具体取决于 T2 中的 Pnum
【问题讨论】:
-
GROUP BY Pnum- 其中Pnum -
对于 SQL Server,您的查询在语法上不正确。
-
T1 表没有
Pnum字段 -
INNER JOIN T1 table1 on table1.Pnum = table2.Pnum你的T1表在这里没有任何所说的列。 -
您的查询如何运行?它肯定不会在 SQL Server 中运行。
标签: sql sql-server sum