SUM()为所有行提供总计,而不是每行

时间:2018-12-09 23:55:16

标签: sql sum

我在获取一些数据以显示我想要的方式时遇到问题。 这是一种称为SQL的形式,在称为Cabinet Vision的软件中。

这应该做什么: 表1应该显示CabJob1的5 CabQty和CabJob2的3 CabQty 表1当前显示了CabJob1和CabJob2的5个

DoorCount正常工作,但CabCount不能正常工作 参见表1(每周计划) Weekly Schedule Table

下面是我的DoorCount和CabCount两个表的屏幕快照,当前正确地将它们加起来,但是在时间表表中的某个地方几乎选择了TOP CabQty

Door Count Table Cab Count Table

下面是每个表的SQL ------------------ DoorCount SQL

SELECT Doors.[Door Num], Doors.[Material Schedule], Doors.[Cabinet ID], 
Count(Doors.[Door Num]) As DoorCount, Doors.[Door Name]

FROM Doors

WHERE Doors.[Door Num] > 0

GROUP BY Doors.[Door Num], Doors.[Material Schedule], Doors.[Cabinet ID], 
Doors.[Door Name]
----------------------

------------------ CabCount SQL
SELECT SUM(Cabinets.Quantity) AS CabQty, [Job Info].[Job Name]

FROM ([Job Info] 
INNER JOIN Rooms ON [Job Info].ID=Rooms.[Job ID]) 
INNER JOIN Cabinets ON Rooms.RoomNumber=Cabinets.[Room ID]

WHERE Cabinets.[Cabinet Name] LIKE 'W1D%' OR Cabinets.[Cabinet Name] LIKE 
't1D%' OR Cabinets.[Cabinet Name] LIKE 'SB%'

GROUP BY [Job Info].[Job Name]
------------------

---------------Weekly Schedule SQL
SELECT  [Job Info].[Job Number], [Job Info].[Customer Name], [Job Info].[Job 
Name], [Job Info].[Customer Comments], DoorCount.[Material Schedule], 
Sum(DoorCount.[DoorCount]) AS DoorCount,  [Job Info].[Customer Email], [Job 
Info].[Customer Fax], [Job Info].[Customer Mobile], [Job Info].[Customer 
Phone], COUNT(CabCount.CabQty) AS CabQty, CabCount.[Job name], Rooms.[Job 
ID]

FROM ((([Job Info]
INNER JOIN Rooms ON [Job Info].ID=Rooms.[Job ID])
INNER JOIN Cabinets ON Rooms.RoomNumber=Cabinets.[Room ID])  
INNER JOIN DoorCount ON Cabinets.[Cabinet ID]=DoorCount.[Cabinet ID])
INNER JOIN CabCount ON [Job Info].[Job Name]=CabCount.[Job Name]

GROUP BY [Job Info].[Job Name], [Job Info].[Job Number], [Job Info]. 
[Customer Name], DoorCount.[Material Schedule],  [Job Info].[Customer 
Comments], [Job Info].[Customer Email], [Job Info].[Customer Fax], [Job 
Info].[Customer Mobile], [Job Info].[Customer Phone], CabCount.[Job name], 
Rooms.[Job ID]

ORDER BY [Job Info].[Customer Email] ASC
---------------------------

感谢您的时间和投入

0 个答案:

没有答案