22,301
社区成员




select * from
(select top 100 percent id, businessNum,exportTime,shipSide,shipOwner from counter where type = 1 and year(exportTime) = year(getdate()) order by 3 ) a
order by exportTime;
;WITH temp1 AS
(
SELECT id = 1, exportTime = CAST('2011-1-1' AS DATETIME) UNION ALL
SELECT 2, '2011-1-3' UNION ALL
SELECT 3, '2011-1-2'
),
temp2 AS
(
SELECT id = 1, exportTime = CAST('2010-1-7' AS DATETIME) UNION ALL
SELECT 2, '2010-1-6' UNION ALL
SELECT 3, '2010-1-5'
)
SELECT * FROM
(SELECT TOP(2) * FROM temp1 ORDER BY 2 DESC) A
UNION ALL
SELECT * FROM
(SELECT TOP(2) * FROM temp2 ORDER BY 2 DESC) B
select * from
(select top 100 percent id, businessNum,exportTime,shipSide,shipOwner
from counter where type = 1 and year(exportTime) = year(getdate())
order by 3
union
select top 100 percent id, businessNum,exportTime,shipSide,shipOwner
from counter where type = 2 and year(exportTime) = year(getdate())
order by 3)U
select * from (
(select top 100 0 as ord,percent id, businessNum,exportTime,shipSide,shipOwner from counter
where type = 1 and year(exportTime) = year(getdate()))
union
select * from
(select top 100 1 as ord,percent id, businessNum,exportTime,shipSide,shipOwner from counter
where type = 2 and year(exportTime) = year(getdate()))) b
order by ord,exportTime
select * from (
(select top 100 percent id, businessNum,exportTime,shipSide,shipOwner from counter where type = 1 and year(exportTime) = year(getdate()))
union
select * from
(select top 100 percent id, businessNum,exportTime,shipSide,shipOwner from counter where type = 2 and year(exportTime) = year(getdate()))) b order by 3
select * from
(select top 100 percent id, businessNum,exportTime,shipSide,shipOwner from counter where type = 1 and year(exportTime) = year(getdate()) order by 3 ) a
--这样的语句在子查询里对某个字段排序是没有必要的,SQL当然会提示你无法按...排序!
--你可以把3换成1、2、4等其他的试试。
order by 3
写在sql句末试试。