I have a view has a total of about 10 left joins in it.
If I execute the view directly, I get results but they aren't complete. If I execute the query that makes up the view directly, I get a full set of results.
I ran sp_refreshView
and the issue is still present.
I've never encountered an issue like this and I'm not sure exactly what to do to resolve it. Does anybody have any potential solutions or troubleshooting steps I can take to try to resolve the issue?
I can confirm that both queries are being run against the same database. The db is on my local instance of SQL Server and it is the only db I can run against.
I am calling the view with the following query and getting no results:
SELECT *
FROM [DHTraxxDev].[Inventory].[vwInventoryList]
WHERE ProjectID = 46
This works with some project numbers and not with others.
If I add an and clause to the end of the create view where ps.ProjectID = 46
, then I get the relevant information.
The code to create the view is
CREATE VIEW [Inventory].[vwInventoryList]
AS
SELECT
ps.ID AS ProjectSpecID,
psrd.id AS 'ProjectSpecReceiveDetailId',
ps.ModelNumber,
p.Identifier, p.ProjectName, p.SiteName,
ps.ProjectID, ps.SpecNumber, ps.ProjectSpecImportFileID,
ps.GenericDescription, ps.DetailedDescription,
ps.LeadTime, ps.Quarter, ps.QuarterYear,
ps.ESACategory, ps.Scope AS ESAScope,
ps.GLCode AS ESAGLCode, ps.ShipToDestinationId,
ps.StatusID AS ProjectSpecStatus,
ISNULL(ps.AtticQuantity, 0) AS AtticQuantity,
ISNULL(ps.Quantity, 0) AS Quantity,
ISNULL(ps.AtticQuantity, 0) + ISNULL(ps.Quantity, 0) AS TotalQuantity,
ISNULL(ps.UnitCost, 0) AS UnitCost,
v.Name AS Vendor, luc.Description AS UnitType,
ps.EstimatedDeliveryDate, ps.EstimatedShippingDate,
ps.ProcurementNotes,
lpsl.Description AS 'ShipTo',
psc.Description AS 'ComponentDescription',
po.ID AS PoId, po.StatusID AS POStatus,
po.Identifier AS PurchaseOrderIdentifier,
psc.ID AS ProjectSpecComponentID,
psc.SpecNumber AS ComponentSpecNumber,
psc.Multiplier,
(ISNULL(ps.AtticQuantity, 0) + ISNULL(ps.Quantity, 0)) + COALESCE(psrToVendor.ToVendor, 0) - COALESCE(psrFromVendor.FromVendor, 0) AS 'QuantityAtVendor',
COALESCE(psrToWarehouse.ToWarehouse, 0) - COALESCE(psrFromWarehouse.FromWarehouse, 0) AS QuantityAtWarehouse,
COALESCE(psrToSite.ToSite, 0) - COALESCE(psrFromSite.FromSite, 0) AS QuantityAtSite,
COALESCE(psrComponentToWarehouse.ToWarehouse, 0) - COALESCE(psrComponentFromWarehouse.FromWarehouse, 0) AS ComponentQuantityAtWarehouse,
COALESCE(psrComponentToSite.ToSite, 0) - COALESCE(psrComponentFromSite.FromSite, 0) AS ComponentQuantityAtSite,
(ISNULL(ps.AtticQuantity, 0) + ISNULL(ps.Quantity, 0)) * ISNULL(ps.UnitCost, 0) AS TotalCost,
(COALESCE(psrToWarehouse.ToWarehouse, 0) - COALESCE(psrFromWarehouse.FromWarehouse, 0)) * ISNULL(ps.UnitCost, 0) AS WarehouseCost,
(COALESCE(psrToSite.ToSite, 0) - COALESCE(psrFromSite.FromSite, 0)) * ISNULL(ps.UnitCost, 0) AS SiteCost,
COALESCE(pendingInventory.Pending, 0) AS Pending,
COALESCE(allocatedInventory.Allocated, 0) AS Allocated
FROM
Project.ProjectSpec AS ps
INNER JOIN
Project.Project AS p ON p.ID = ps.ProjectID
LEFT OUTER JOIN
Project.lkupProjectSpecLocation AS lpsl ON lpsl.ID = ps.ShipToDestinationId
LEFT OUTER JOIN
DH.Vendor AS v ON ps.VendorID = v.ID
LEFT OUTER JOIN
PurchaseOrder.lkupUnitCost AS luc ON luc.ID = ps.UnitCostID
LEFT OUTER JOIN
Project.ProjectSpecComponent AS psc ON psc.ProjectSpecID = ps.ID
LEFT OUTER JOIN
PurchaseOrder.PurchaseOrder AS po ON po.ID = ps.PurchaseOrderID
LEFT OUTER JOIN
Project.ProjectSpecReceivedDetails psrd on psrd.ProjectSpecID = ps.ID
LEFT JOIN
(SELECT
ProjectSpecID, SUM(ri.amount) AS 'Pending'
FROM
Inventory.Request r
INNER JOIN
inventory.requestinventory ri ON r.id = ri.RequestID
WHERE
RequestTypeID = 1 AND r.Deleted = 0 AND ri.Deleted = 0
AND RequestStatusID = 3
GROUP BY
ProjectSpecID) pendingInventory ON pendingInventory.ProjectSpecID = ps.ID
LEFT JOIN
(SELECT
ProjectSpecID, SUM(ri.amount) AS 'Allocated'
FROM
Inventory.Request r
INNER JOIN
inventory.requestinventory ri ON r.id = ri.RequestID
WHERE
RequestTypeID = 2 AND r.Deleted = 0
AND ri.Deleted = 0 AND RequestStatusID = 4
GROUP BY
ProjectSpecID) allocatedInventory ON allocatedInventory.ProjectSpecID = ps.ID
LEFT JOIN
(SELECT
psrd.ProjectSpecID, psrd.ProjectSpecComponentID,
ToSite = SUM(psrd.QuantityReceived)
FROM
Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.ToProjectSpecLocationID = 3
AND psrd.ProjectSpecComponentID IS NOT NULL
GROUP BY
psrd.ProjectSpecID, psrd.ProjectSpecComponentID) psrComponentToSite ON psrComponentToSite.ProjectSpecID = ps.ID
LEFT JOIN
(SELECT
psrd.ProjectSpecID, psrd.ProjectSpecComponentID
,FromSite = SUM(psrd.QuantityReceived)
FROM Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.FromProjectSpecLocationID = 3
and psrd.ProjectSpecComponentID is not null
GROUP BY psrd.ProjectSpecID, psrd.ProjectSpecComponentID
) psrComponentFromSite ON psrComponentFromSite.ProjectSpecID = ps.ID
LEFT JOIN
(
SELECT
psrd.ProjectSpecID, psrd.ProjectSpecComponentID
,ToWarehouse = SUM(psrd.QuantityReceived)
FROM Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.ToProjectSpecLocationID = 2
and psrd.ProjectSpecComponentID is not null
GROUP BY psrd.ProjectSpecID, psrd.ProjectSpecComponentID
) psrComponentToWarehouse ON psrComponentToWarehouse.ProjectSpecID = ps.ID
LEFT JOIN
(
SELECT
psrd.ProjectSpecID, psrd.ProjectSpecComponentID
,FromWarehouse = SUM(psrd.QuantityReceived)
FROM Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.FromProjectSpecLocationID = 2
and psrd.ProjectSpecComponentID is not null
GROUP BY psrd.ProjectSpecID, psrd.ProjectSpecComponentID
) psrComponentFromWarehouse ON psrComponentFromWarehouse.ProjectSpecID = ps.ID
LEFT JOIN
(
SELECT
psrd.ProjectSpecID
,ToSite = SUM(psrd.QuantityReceived)
FROM Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.ToProjectSpecLocationID = 3
and psrd.ProjectSpecComponentID is null
GROUP BY psrd.ProjectSpecID
) psrToSite ON psrToSite.ProjectSpecID = ps.ID
LEFT JOIN
(
SELECT
psrd.ProjectSpecID
,FromSite = SUM(psrd.QuantityReceived)
FROM Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.FromProjectSpecLocationID = 3
and psrd.ProjectSpecComponentID is null
GROUP BY psrd.ProjectSpecID
) psrFromSite ON psrFromSite.ProjectSpecID = ps.ID
LEFT JOIN
(
SELECT
psrd.ProjectSpecID
,ToWarehouse = SUM(psrd.QuantityReceived)
FROM Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.ToProjectSpecLocationID = 2
and psrd.ProjectSpecComponentID is null
GROUP BY psrd.ProjectSpecID
) psrToWarehouse ON psrToWarehouse.ProjectSpecID = ps.ID
LEFT JOIN
(
SELECT
psrd.ProjectSpecID
,FromWarehouse = SUM(psrd.QuantityReceived)
FROM Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.FromProjectSpecLocationID = 2
and psrd.ProjectSpecComponentID is null
GROUP BY psrd.ProjectSpecID
) psrFromWarehouse ON psrFromWarehouse.ProjectSpecID = ps.ID
LEFT JOIN
(
SELECT
psrd.ProjectSpecID
,ToVendor = SUM(psrd.QuantityReceived)
FROM Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.ToProjectSpecLocationID = 1
and psrd.ProjectSpecComponentID is null
GROUP BY psrd.ProjectSpecID
) psrToVendor ON psrToVendor.ProjectSpecID = ps.ID
LEFT JOIN
(
SELECT
psrd.ProjectSpecID
,FromVendor = SUM(psrd.QuantityReceived)
FROM Project.ProjectSpecReceivedDetails psrd
WHERE
psrd.Deleted = 0
AND psrd.FromProjectSpecLocationID = 1
and psrd.ProjectSpecComponentID is null
GROUP BY psrd.ProjectSpecID
) psrFromVendor ON psrFromVendor.ProjectSpecID = ps.ID
WHERE (ps.Deleted = 0) AND (psc.ID IS NULL) OR
(ps.Deleted = 0) AND (psc.Deleted = 0)
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties =
Begin PaneConfigurations =
Begin PaneConfiguration = 0
NumPanes = 4
Configuration = "(H (1[40] 4[20] 2[20] 3) )"
End
Begin PaneConfiguration = 1
NumPanes = 3
Configuration = "(H (1 [50] 4 [25] 3))"
End
Begin PaneConfiguration = 2
NumPanes = 3
Configuration = "(H (1 [50] 2 [25] 3))"
End
Begin PaneConfiguration = 3
NumPanes = 3
Configuration = "(H (4 [30] 2 [40] 3))"
End
Begin PaneConfiguration = 4
NumPanes = 2
Configuration = "(H (1 [56] 3))"
End
Begin PaneConfiguration = 5
NumPanes = 2
Configuration = "(H (2 [66] 3))"
End
Begin PaneConfiguration = 6
NumPanes = 2
Configuration = "(H (4 [50] 3))"
End
Begin PaneConfiguration = 7
NumPanes = 1
Configuration = "(V (3))"
End
Begin PaneConfiguration = 8
NumPanes = 3
Configuration = "(H (1[56] 4[18] 2) )"
End
Begin PaneConfiguration = 9
NumPanes = 2
Configuration = "(H (1 [75] 4))"
End
Begin PaneConfiguration = 10
NumPanes = 2
Configuration = "(H (1[66] 2) )"
End
Begin PaneConfiguration = 11
NumPanes = 2
Configuration = "(H (4 [60] 2))"
End
Begin PaneConfiguration = 12
NumPanes = 1
Configuration = "(H (1) )"
End
Begin PaneConfiguration = 13
NumPanes = 1
Configuration = "(V (4))"
End
Begin PaneConfiguration = 14
NumPanes = 1
Configuration = "(V (2))"
End
ActivePaneConfig = 0
End
Begin DiagramPane =
Begin Origin =
Top = 0
Left = 0
End
Begin Tables =
Begin Table = "ps"
Begin Extent =
Top = 6
Left = 38
Bottom = 136
Right = 295
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "p"
Begin Extent =
Top = 6
Left = 333
Bottom = 136
Right = 572
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "lpsl"
Begin Extent =
Top = 6
Left = 610
Bottom = 102
Right = 780
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "v"
Begin Extent =
Top = 6
Left = 818
Bottom = 136
Right = 1014
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "luc"
Begin Extent =
Top = 102
Left = 610
Bottom = 215
Right = 780
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "psc"
Begin Extent =
Top = 138
Left = 38
Bottom = 268
Right = 295
End
DisplayFlags = 280
TopColumn = 0
End
Begin Table = "po"
Begin Extent =
Top = 138
Left = 333
Bottom = 268
Right = 549
End
DisplayFlags = 280
TopColumn = 0
' , @level0type=N'SCHEMA',@level0name=N'Inventory', @level1type=N'VIEW',@level1name=N'vwInventoryList'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N' End
Begin Table = "rdi"
Begin Extent =
Top = 138
Left = 818
Bottom = 268
Right = 1044
End
DisplayFlags = 280
TopColumn = 0
End
End
End
Begin SQLPane =
End
Begin DataPane =
Begin ParameterDefaults = ""
End
End
Begin CriteriaPane =
Begin ColumnWidths = 11
Column = 1440
Alias = 900
Table = 1170
Output = 720
Append = 1400
NewValue = 1170
SortType = 1350
SortOrder = 1410
GroupBy = 1350
Filter = 1350
Or = 1350
Or = 1350
Or = 1350
End
End
End
' , @level0type=N'SCHEMA',@level0name=N'Inventory', @level1type=N'VIEW',@level1name=N'vwInventoryList'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=2 , @level0type=N'SCHEMA',@level0name=N'Inventory', @level1type=N'VIEW',@level1name=N'vwInventoryList'
GO