我有一个查询,从两个表中获取数据,一个实时和一个存档,并使用联合所有查询加入两个,但我需要结果以日期顺序出来我怎么能实现这一点?这是我的查询
SELECT CustomerAccountNumber,
CustomerAccountName,
DateTimeCreated,
DocumentNo,
TotalValue
FROM (
(SELECT SOPOrderReturn.DocumentNo,
SLCustomerAccount.CustomerAccountNumber,
SLCustomerAccount.CustomerAccountName,
SUM(SOPOrderReturnLine.LineTotalValue) AS TotalValue,
CONVERT(date, SOPOrderReturnLine.DateTimeCreated) AS DateTimeCreated
FROM SOPOrderReturnLine
INNER JOIN SOPOrderReturn ON SOPOrderReturnLine.SOPOrderReturnID = SOPOrderReturn.SOPOrderReturnID
INNER JOIN SLCustomerAccount ON SOPOrderReturn.CustomerID = SLCustomerAccount.SLCustomerAccountID
WHERE (SOPOrderReturnLine.AnalysisCode1 LIKE 'Angela%')
AND (SOPOrderReturnLine.DateTimeCreated BETWEEN CONVERT(DATETIME, '2015-02-16 00:00:00', 102) AND CONVERT(DATETIME, '2015-02-24 23:59:59', 102))
GROUP BY SOPOrderReturn.DocumentNo,
SLCustomerAccount.CustomerAccountName,
SLCustomerAccount.CustomerAccountNumber,
CONVERT(date, SOPOrderReturnLine.DateTimeCreated),
SOPOrderReturn.DocumentTypeID
HAVING (SOPOrderReturn.DocumentTypeID <> '1') )
UNION ALL
(SELECT SOPOrderReturnArch.DocumentNo,
SLCustomerAccount.CustomerAccountNumber,
SLCustomerAccount.CustomerAccountName,
SUM(SOPOrderReturnLineArch.LineTotalValue) AS TotalValue,
CONVERT(date, SOPOrderReturnLineArch.DateTimeCreated) AS DateTimeCreated
FROM SOPOrderReturnLineArch
INNER JOIN SOPOrderReturnArch ON SOPOrderReturnLineArch.SOPOrderReturnID = SOPOrderReturnArch.SOPOrderReturnID
INNER JOIN SLCustomerAccount ON SOPOrderReturnArch.CustomerID = SLCustomerAccount.SLCustomerAccountID
WHERE (SOPOrderReturnLineArch.AnalysisCode1 LIKE 'Angela%')
AND (SOPOrderReturnLineArch.DateTimeCreated BETWEEN CONVERT(DATETIME, '2015-02-16 00:00:00', 102) AND CONVERT(DATETIME, '2015-02-24 23:59:59', 102))
GROUP BY SOPOrderReturnArch.DocumentNo,
SLCustomerAccount.CustomerAccountName,
SLCustomerAccount.CustomerAccountNumber,
CONVERT(date, SOPOrderReturnLineArch.DateTimeCreated),
SOPOrderReturnArch.DocumentTypeID
HAVING (SOPOrderReturnArch.DocumentTypeID <> '1') ) ) x
结果就是这样。
CustomerAccountNumber CustomerAccountName DateTimeCreated DocumentNo TotalValue
4063 Account Name 16/02/2015 25/08/2538 12/05/1901
10306 Account Name 16/02/2015 28/08/2538 10/08/1900
2063 Account Name 16/02/2015 29/08/2538 26/11/1900
169 Account Name 17/02/2015 10/09/2538 07/01/1902
7335 Account Name 18/02/2015 02/10/2538 06/02/1900
9159 Account Name 19/02/2015 11/10/2538 15/07/1901
7578 Account Name 19/02/2015 13/10/2538 22/10/1900
2144 Account Name 20/02/2015 02/11/2538 14/12/1900
169 Account Name 20/02/2015 07/11/2538 22/11/1900
9633 Account Name 23/02/2015 28/11/2538 21/07/1901
10152 Account Name 23/02/2015 29/11/2538 23/09/1902
10247 Account Name 23/02/2015 05/12/2538 11/06/1901
所以我希望DateTimeCreated列按日期顺序
答案 0 :(得分:0)
将此作为单个查询,将所有内部括号()放在一起,并为其命名,然后相应地写入顺序
喜欢:
Select * from (Your query)<tableName> order by DateTimeCreated desc
答案 1 :(得分:0)
将其作为功能
create function funcx
returns table
as return
(SELECT CustomerAccountNumber, ...)
select *
from
dbo.funcx
order by
DateTimeCreated