我正在尝试从2个表中查询并计算一个值。我正在使用他们的查询工具在归纳自动化点火中进行此操作。然后使用该值我要进行2次计算。最终我发现我需要一个嵌套查询,并且我相信我已经接近了,但是标题出现了错误。
SELECT
[PalletTally Table 2].PartNumber,
[PalletTally Table 2].ChosenWeight,
[PalletTally Table 2].[Line Speed],
[PalletTally Table 2].PalletCount,
([PalletTally Table 2].[ChosenWeight]*[Part Pallet Info Table].[CUT
LENGTH]/12*[Part Pallet Info Table].[PIECES PER BALE]+[Part Pallet Info
Table].[TARE WT]) AS CalcPalletWeight,
(TotalPalletWeight / (1 + (CalcPalletWeight * PalletCount)) * [Line Speed]
* 12 / 60) AS [Lbs./Hr.]
FROM [PalletTally Table 2]
WHERE
(
Select
([PalletTally Table 2].[Pallet 1 Weight] +
[PalletTally Table 2].[Pallet 2 Weight] +
[PalletTally Table 2].[Pallet 3 Weight] +
[PalletTally Table 2].[Pallet 4 Weight] +
[PalletTally Table 2].[Pallet 5 Weight] +
[PalletTally Table 2].[Pallet 7 Weight] +
[PalletTally Table 2].[Pallet 6 Weight] +
[PalletTally Table 2].[Pallet 8 Weight] +
[PalletTally Table 2].[Pallet 9 Weight] +
[PalletTally Table 2].[Pallet 10 Weight] +
[PalletTally Table 2].[Pallet 11 Weight] +
[PalletTally Table 2].[Pallet 12 Weight] +
[PalletTally Table 2].[Pallet 13 Weight] +
[PalletTally Table 2].[Pallet 14 Weight] +
[PalletTally Table 2].[Pallet 15 Weight] +
[PalletTally Table 2].[Pallet 16 Weight] +
[PalletTally Table 2].[Pallet 17 Weight] +
[PalletTally Table 2].[Pallet 18 Weight] +
[PalletTally Table 2].[Pallet 19 Weight] +
[PalletTally Table 2].[Pallet 20 Weight] +
[PalletTally Table 2].[Pallet 21 Weight] +
[PalletTally Table 2].[Pallet 22 Weight] +
[PalletTally Table 2].[Pallet 23 Weight] +
[PalletTally Table 2].[Pallet 24 Weight] +
[PalletTally Table 2].[Pallet 25 Weight] +
[PalletTally Table 2].[Pallet 26 Weight] +
[PalletTally Table 2].[Pallet 27 Weight] +
[PalletTally Table 2].[Pallet 28 Weight] +
[PalletTally Table 2].[Pallet 30 Weight] +
[PalletTally Table 2].[Pallet 31 Weight] +
[PalletTally Table 2].[Pallet 29 Weight] +
[PalletTally Table 2].[Pallet 32 Weight] +
[PalletTally Table 2].[Pallet 33 Weight] +
[PalletTally Table 2].[Pallet 34 Weight] +
[PalletTally Table 2].[Pallet 35 Weight] +
[PalletTally Table 2].[Pallet 36 Weight] +
[PalletTally Table 2].[Pallet 37 Weight] +
[PalletTally Table 2].[Pallet 39 Weight] +
[PalletTally Table 2].[Pallet 40 Weight] +
[PalletTally Table 2].[Pallet 38 Weight] +
[PalletTally Table 2].[Pallet 41 Weight] +
[PalletTally Table 2].[Pallet 43 Weight] +
[PalletTally Table 2].[Pallet 42 Weight] +
[PalletTally Table 2].[Pallet 44 Weight] +
[PalletTally Table 2].[Pallet 45 Weight] +
[PalletTally Table 2].[Pallet 46 Weight] +
[PalletTally Table 2].[Pallet 47 Weight] +
[PalletTally Table 2].[Pallet 48 Weight] +
[PalletTally Table 2].[Pallet 49 Weight] +
[PalletTally Table 2].[Pallet 50 Weight] +
[PalletTally Table 2].[Pallet 51 Weight] +
[PalletTally Table 2].[Pallet 52 Weight] +
[PalletTally Table 2].[Pallet 53 Weight] +
[PalletTally Table 2].[Pallet 54 Weight] +
[PalletTally Table 2].[Pallet 55 Weight] +
[PalletTally Table 2].[Pallet 56 Weight] +
[PalletTally Table 2].[Pallet 57 Weight] +
[PalletTally Table 2].[Pallet 58 Weight] +
[PalletTally Table 2].[Pallet 59 Weight] +
[PalletTally Table 2].[Pallet 60 Weight] +
[PalletTally Table 2].[Pallet 61 Weight])
AS PalletWeightTotal
FROM [PalletTally Table 2]
INNER JOIN [Part Pallet Info Table] ON [Part Pallet Info Table].[PART
NUMBER]
= [PalletTally Table 2].PartNumber
WHERE [PalletTally Table 2].[Date] > GETDATE() - 5
)
我是真正开发SQL的新手(随时随地学习SQL的工业工程师),所以我完全不了解的任何信息都让我知道。
谢谢。
答案 0 :(得分:0)
很难告诉您最佳的解决方案,但是-只是为了回答您的问题-我会尝试将其减少一些以使其更加明显:
SELECT
[PalletTally Table 2].PartNumber,
--more columns
FROM [PalletTally Table 2]
WHERE
(
Select
(
[PalletTally Table 2].[Pallet 1 Weight] +
[PalletTally Table 2].[Pallet 2 Weight] +
--more of them
) AS PalletWeightTotal
FROM [PalletTally Table 2]
--some joins
WHERE [PalletTally Table 2].[Date] > GETDATE() - 5
)
基本上,这意味着以下内容
SELECT Something
FROM Somewhere
WHERE
(
Select ASumOfManyValues
FROM FromTheSameTable
WHERE SomeCondition is true
)
??????????
您正在告诉引擎过滤SELECT。然后,您计算出一个值-就是这样...假定该值-应该针对边界值(例如>0
)进行测试。
希望这对您有帮助...
我敢肯定,您的表格设计有误。每当您觉得需要 name-number 元素(例如pallet1
,pallet2
)时,这都是指向相关副表的强大指针。
如果表格的设计在您的控制之下,那么采用适当的结构,一切都会变得更加容易...