在SQL Server 2005上运行 遇到2个查询有点麻烦。第一个返回如下表:
NAME PAstDUe DueTomorr Due Today Due MONd Due Beyond CLIENT 23 10 8 13 32
第二个返回一个相同信息的列表,当我在excel中的数据透视表中运行时,它不会添加到同一个表/数据中。我在视觉工作室中将这些用于两个不同的数据源/目的,所以我不能使用它。我没有看到什么出错但我以不同的方式计算结果,所以我不知道我的数学是否已关闭或类似的东西。这非常重要,因为我需要这些数据在日常工作中准确无误。如果你想知道什么是dbo.TruncateDate正在做什么,它会引用我们系统中订单的订单号,当运行涉及日期时,这可以确保查询的一部分根据我们系统中包含所有内容的表忽略所有周末和假日这些日期。希望有道理。如果我能提供更多信息,请告诉我。
查询1:
with cte AS (SELECT cl.Name,
SUM(CASE WHEN CURRENT_TIMESTAMP > oi.RequiredByDate THEN 1 ELSE 0 END) as PastDue
,SUM(CASE WHEN DATEADD(dd, DATEDIFF(dd, 0, oi.RequiredByDate), 0) = dateadd(day, datediff(day, '19000101',CURRENT_TIMESTAMP),'19000102') then 1 ELSE 0 END) as DueTomorrow
,SUM(CASE WHEN dbo.TruncateDate(CURRENT_TIMESTAMP) = dbo.TruncateDate(oi.RequiredByDate) THEN 1 Else 0 END) as DueToday
,SUM(CASE WHEN DateDiff(day, getdate(), RequiredByDate) BETWEEN 2 and 7 AND DateName(weekday, RequiredByDate) = 'Monday' Then 1 ELSE 0 END) as DueMonday
,SUM(CASE WHEN DATEADD(DAY, 2,dbo.TruncateDate(CURRENT_TIMESTAMP)) <= dbo.TruncateDate(oi.RequiredByDate) THEN 1 ELSE 0 END) as DueBeyond
FROM OrderItems oi
JOIN Orders o ON o.OrderID = oi.OrderID
JOIN Counties c ON c.FIPS = o.FIPS
JOIN Clients cl ON cl.ClientID = o.ClientID
JOIN Milestones m ON m.MilestoneID = oi.LastMilestoneID
JOIN Products p ON p.ProductID = oi.ProductID
JOIN Vendors v ON v.VendorID = oi.VendorID
LEFT JOIN ClientBranches clb ON clb.ClientID = o.ClientID
WHERE QueueID > 0 AND cl.Name NOT LIKE 'TES%'
AND cl.NAME LIKE 'HLC%'
GROUP BY cl.Name)
Select * FROM cte
查询2:
SELECT cl.Name as Client, clb.Name as ClientBranch
,
CASE
WHEN CURRENT_TIMESTAMP > oi.RequiredByDate THEN 'Past Due'
WHEN dbo.TruncateDate(CURRENT_TIMESTAMP) = dbo.TruncateDate (oi.RequiredByDate) THEN 'Due Today'
WHEN DATEADD(dd, DATEDIFF(dd, 0, oi.RequiredByDate), 0) = dateadd(day, datediff(day, '19000101',CURRENT_TIMESTAMP),'19000102') then 'Due Tomorrow'
WHEN DateDiff(day, getdate(), RequiredByDate) BETWEEN 2 and 7 AND DateName(weekday, RequiredByDate) = 'Monday' Then 'Due Monday'
WHEN DATEADD(DAY, 2,dbo.TruncateDate(CURRENT_TIMESTAMP)) <= dbo.TruncateDate(oi.RequiredByDate) THEN 'Due Beyond'
ELSE 'WRONG' end
as DeliveryStatus
FROM OrderItems oi
JOIN Orders o ON o.OrderID = oi.OrderID
JOIN Counties c ON c.FIPS = o.FIPS
JOIN Clients cl ON cl.ClientID = o.ClientID
JOIN Milestones m ON m.MilestoneID = oi.LastMilestoneID
JOIN Products p ON p.ProductID = oi.ProductID
JOIN Vendors v ON v.VendorID = oi.VendorID
LEFT JOIN ClientBranches clb ON clb.ClientID = o.ClientID
WHERE QueueID > 0
and cl.Name not like ('Tes%')
and cl.Name Like 'HLC%'
答案 0 :(得分:1)
请你运行一下:
with cte AS (SELECT cl.Name,
SUM(CASE WHEN CURRENT_TIMESTAMP > oi.RequiredByDate THEN 1 ELSE 0 END) as PastDue
,SUM(CASE WHEN DATEADD(dd, DATEDIFF(dd, 0, oi.RequiredByDate), 0) = dateadd(day, datediff(day, '19000101',CURRENT_TIMESTAMP),'19000102') then 1 ELSE 0 END) as DueTomorrow
,SUM(CASE WHEN dbo.TruncateDate(CURRENT_TIMESTAMP) = dbo.TruncateDate(oi.RequiredByDate) THEN 1 Else 0 END) as DueToday
,SUM(CASE WHEN DateDiff(day, getdate(), RequiredByDate) BETWEEN 2 and 7 AND DateName(weekday, RequiredByDate) = 'Monday' Then 1 ELSE 0 END) as DueMonday
,SUM(CASE WHEN DATEADD(DAY, 2,dbo.TruncateDate(CURRENT_TIMESTAMP)) <= dbo.TruncateDate(oi.RequiredByDate) THEN 1 ELSE 0 END) as DueBeyond,
COUNT(*) AS cnt
FROM OrderItems oi
JOIN Orders o ON o.OrderID = oi.OrderID
JOIN Counties c ON c.FIPS = o.FIPS
JOIN Clients cl ON cl.ClientID = o.ClientID
JOIN Milestones m ON m.MilestoneID = oi.LastMilestoneID
JOIN Products p ON p.ProductID = oi.ProductID
JOIN Vendors v ON v.VendorID = oi.VendorID
LEFT JOIN ClientBranches clb ON clb.ClientID = o.ClientID
WHERE QueueID > 0 AND cl.Name NOT LIKE 'TES%'
AND cl.NAME LIKE 'HLC%'
GROUP BY cl.Name)
并确保cnt
等于所有其他字段的总和?
<强>更新强>
请运行此
SELECT *
FROM (
SELECT cl.Name,
CASE WHEN CURRENT_TIMESTAMP > oi.RequiredByDate THEN 1 ELSE 0 END as PastDue,
CASE WHEN DATEADD(dd, DATEDIFF(dd, 0, oi.RequiredByDate), 0) = dateadd(day, datediff(day, '19000101',CURRENT_TIMESTAMP),'19000102') then 1 ELSE 0 END as DueTomorrow,
CASE WHEN dbo.TruncateDate(CURRENT_TIMESTAMP) = dbo.TruncateDate(oi.RequiredByDate) THEN 1 Else 0 END as DueToday,
CASE WHEN DateDiff(day, getdate(), RequiredByDate) BETWEEN 2 and 7 AND DateName(weekday, RequiredByDate) = 'Monday' Then 1 ELSE 0 END as DueMonday,
CASE WHEN DATEADD(DAY, 2,dbo.TruncateDate(CURRENT_TIMESTAMP)) <= dbo.TruncateDate(oi.RequiredByDate) THEN 1 ELSE 0 END as DueBeyond,
FROM OrderItems oi
JOIN Orders o ON o.OrderID = oi.OrderID
JOIN Counties c ON c.FIPS = o.FIPS
JOIN Clients cl ON cl.ClientID = o.ClientID
JOIN Milestones m ON m.MilestoneID = oi.LastMilestoneID
JOIN Products p ON p.ProductID = oi.ProductID
JOIN Vendors v ON v.VendorID = oi.VendorID
LEFT JOIN ClientBranches clb ON clb.ClientID = o.ClientID
WHERE QueueID > 0 AND cl.Name NOT LIKE 'TES%'
AND cl.NAME LIKE 'HLC%'
) q
WHERE PastDue + DueTomorrow + DueToday + DueMonday + DueBeyond > 1
并查看不止一次计算的记录。