我在數據庫中有4個表。倉庫包含客戶擁有的箱子,箱子裏有文件。有一個Client
表,一個Warehouse
表,一個Boxes
表和一個Files
表。加入3個表的SQL COUNT
所以Client
表有WarehouseID
作爲外鍵,Boxes
表有ClientID
作爲外鍵,和Files
表有BoxID
作爲外鍵。我想統計每個客戶端在我的查詢中包含的盒子和文件的數量,以及進出倉庫的盒子數量。 Boxes
和Files
表上的Status
字段確定框和文件是否在倉庫內或倉庫外。我跑的箱子下面的查詢和號碼是正確的:
SELECT
[c].[ClientID],
[c].[Name] AS [ClientName],
[w].[Name] AS [WarehouseName],
COUNT(DISTINCT [b].[BoxID]) AS [BoxCount],
SUM(CASE WHEN [b].[Status] = @IN THEN 1 ELSE 0 END)) AS [BoxesIn],
SUM(CASE WHEN [b].[Status] = @OUT THEN 1 ELSE 0 END) AS [BoxesOut],
SUM(CASE WHEN [b].[DestructionDate] <= GETDATE() THEN 1 ELSE 0 END) AS [BoxesForDestruction],
FROM [Clients] AS [c] INNER JOIN [Boxes] AS [b]
ON [c].[ClientID] = [b].[ClientID]
INNER JOIN [Warehouses] AS [w]
ON [c].WarehouseID = [w].[WarehouseID]
WHERE [c].[ClientID] = @ClientID
GROUP BY
[c].[ClientID],
[c].[Name],
[w].[Name]
這將產生的輸出:
ClientID | ClientName | WarehouseName | BoxCount | BoxesIn | BoxesOut | BoxesForDestruction
1 | ACME Corp. | FooFactory | 22744 | 22699 | 45 | 7888
計數的輸出是正確的。當我將Files
表添加到INNER JOIN
時,那麼這些數字就會膨脹。下面是SQL:
SELECT
[c].[ClientID],
[c].[Name] AS [ClientName],
[w].[Name] AS [WarehouseName],
COUNT(DISTINCT [b].[BoxID]) AS [BoxCount],
COUNT(DISTINCT [f].[FileID]) AS [FileCount], -- *NEW*
SUM(CASE WHEN [b].[Status] = @IN THEN 1 ELSE 0 END)) AS [BoxesIn],
SUM(CASE WHEN [b].[Status] = @OUT THEN 1 ELSE 0 END) AS [BoxesOut],
SUM(CASE WHEN [b].[DestructionDate] <= GETDATE() THEN 1 ELSE 0 END) AS [BoxesForDestruction],
FROM [Clients] AS [c] INNER JOIN [Boxes] AS [b]
ON [c].[ClientID] = [b].[ClientID]
INNER JOIN [Warehouses] AS [w]
ON [c].[WarehouseID] = [w].[WarehouseID]
INNER JOIN [Files] AS [f] -- *NEW*
ON [b].[BoxID] = [f].[BoxID] -- *NEW*
WHERE [c].[ClientID] = @ClientID
GROUP BY
[c].[ClientID],
[c].[Name],
[w].[Name]
這給了我下面的計數輸出(我省略第3列,因爲他們沒有相關的):
BoxCount | FilesCount | BoxesIn | BoxesOut | BoxesForDestruction
19151 | 411961 | 411381 | 580 | 144615
的FilesCount
是正確的,但其他號碼關閉。我知道這是爲什麼發生,但我不知道如何解決它。由於框和文件上的連接返回多行,所以會創建額外的行。當執行SUM
時,多餘的行膨脹了計數。由於倉庫只有一行,因此該連接不會影響計數。如何修改我的查詢以獲取正確數量的文件和盒子進出倉庫?
這很好,謝謝。 – Nse