English 中文(简体)
ql
原标题:reorder columns after cte in sql
  • 时间:2012-04-18 22:53:21
  •  标签:
  • sql

我需要改变这一结果,但希望其将各栏重新排列为具体顺序。 命令如欲,应晚于星期一,应明天,今天,应周知

任何人都可以帮助

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 
最佳回答

我改变了<代码>SlectT条款的顺序。

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 
问题回答

替换<代码>。

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

选择:

www.un.org/spanish/sc 应付账款 应自





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签