我有一个查询,无论如何我都想显示行。如果返回了一些数据,查询会返回我需要的正确信息,但是在所有行都为空的情况下,我什么也得不到,显然,如果没有,我需要一行返回全 0,而不是什么都没有。
我曾尝试使用isnull()
andcoalesce()
来解释这一点,但显然我做得不对。以下是我需要为此目的调整的基本查询。
SELECT
Vendor
,SUM(TotalAssigned) as 'Total Production Assigned Not Completed'
,SUM(AssignedFees) as 'Work in process Fees'
,SUM(TotalSubmitted) as 'Total Production Submitted'
,SUM(SubmittedFees) as 'Submitted Production Fees'
FROM(
SELECT
distinct
v.ContactFirstName+' '+v.ContactLastName AS Vendor
,oi.orderid, oi.orderitemid
,CASE WHEN oi.QueueID > 0 and oi.lastmilestoneid < 70 THEN 1 ELSE 0 END AS TotalAssigned
,CASE WHEN oi.QueueID > 0 and oi.lastmilestoneid < 70 THEN dbo.GetAdjustedVendorFee(oi.OrderID, oi.OrderItemID) END AS AssignedFees
,CASE WHEN oim.MilestoneID = 70 THEN 1 ELSE 0 END AS TotalSubmitted
,CASE WHEN oim.MilestoneID = 70 THEN dbo.GetAdjustedVendorFee(oi.OrderID, oi.OrderItemID) END AS SubmittedFees
FROM
OrderItems oi
LEFT JOIN OrderItemMilestones oim ON oim.OrderID = oi.OrderID and oim.OrderItemID = oi.OrderItemID
LEFT JOIN Vendors v ON v.VendorID = oi.VendorID
WHERE
oim.MilestoneDate BETWEEN dbo.TruncateDate(dateadd(dd, -1, Current_Timestamp)) and dbo.TruncateDate(Current_Timestamp)
and oi.VendorID in (105144)
and oi.productid not in (105)
)x
GROUP BY Vendor