2012-04-18 91 views
0

我需要這樣的結果不變,但我希望它將列重新排列到特定順序。我想要的順序是由於除此之外,由於週一,截止到明天,今天到期,過期在sql中cte後重新排序列

任何人都可以用這個

with cte AS (SELECT cl.Name, 

SUM(CASE WHEN dbo.TruncateDate(CURRENT_TIMESTAMP) = dbo.TruncateDate(oi.RequiredByDate) THEN 1 Else 0 END) as DueToday 
,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 DateDiff(day, getdate(), RequiredByDate) BETWEEN 2 and 7 AND DateName(weekday, RequiredByDate) = 'Monday' Then 1 ELSE 0 END) as DueMonday 
,SUM(CASE WHEN 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 

回答

0

幫助我改變了SELECT條款的順序。

with cte AS (SELECT cl.Name 

,SUM(CASE WHEN dbo.TruncateDate(CURRENT_TIMESTAMP) <= dbo.TruncateDate(oi.RequiredByDate) THEN 1 ELSE 0 END) 
as DueBeyond 
,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(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 CURRENT_TIMESTAMP > oi.RequiredByDate THEN 1 ELSE 0 END) 
as PastDue 

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 
+0

感謝你的幫助 – wondergoat77 2012-04-18 23:16:05

1

在您選擇:

SELECT DueBeyond, DueMonday, DueTomorrow, DueToday, PastDue FROM cte

2

更換Select * FROM cte

Select DueBeyond, DueMonday, DueTomorrow, DueToday, PastDue 
from cte 
+0

這個工作,但不包括cl.name columnn,無後顧之憂雖然我沒有指明我需要它 – wondergoat77 2012-04-18 23:16:37