在 SQL Server 2005 上运行 2 个查询有点麻烦。第一个返回一个像这样的表:
姓名 PAstDUe DueTomorr 今日到期 MONd 逾期 客户 23 10 8 13 32
第二个返回相同信息的列表,当我在 excel 中的数据透视表中运行时,这些信息不会添加到相同的表/数据中。我将这些用于 Visual Studio 中的一个项目的两个不同的数据源/目的,所以我不能不使用一个。我不知道出了什么问题,但我正在以不同的方式计算结果,所以我不知道我的数学是否关闭或类似的东西。这真的很重要,因为我需要这些数据每天都是准确的。如果您想知道 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%'