1

我有一个查询:

select * from
(
select i.id, i.art_id, i.c_izm, i.c_zime_izm, i.ac_izm, i.rc_izm, i.f_nos, convert(nvarchar,dateadd(mi,-30,i.datums),100) as dat
from nol_art_izmaina i
inner join nol_art a on i.art_id=a.id
where datepart(year,print_dat)=2005
order by a.nos --when I add this row, the error shows
union all
select distinct null,null,'','','','', f_nos, min(dat)
from nol_art_izmaina 
where datepart(year,print_dat)=2005
group by f_nos
)tablePlusHeaders
order by dat desc

我需要什么?我需要第一个查询的数据按 a.nos 值排序。我怎么做?

编辑
结果表应如下所示,但仅包含我的数据:

Name   ImpFile/Job         Year
--------------------------------
       Imp01 20.01.2012              This is from set2
John   Clerk               1986    This is from set1
James  Assistant           1990    This is from set1
       Imp02 26.02.2012              This is from set2
Anna   Manager             1982    This is from set1
Sam    Salesman            1985    This is from set1
Dean   Cleaner             1985    This is from set1
4

1 回答 1

1

该错误可能是因为没有列号,而是f_nos。要订购联合,您需要为每个选择添加一个标记,然后按 f_nos 和该标记排序:

select * from
(
  select i.id, i.art_id, i.c_izm, i.c_zime_izm, i.ac_izm, i.rc_izm, i.f_nos, 
         convert(nvarchar,dateadd(mi,-30,i.datums),100) as dat,
         0 set_id
    from nol_art_izmaina i
   inner join nol_art a on i.art_id=a.id
   where datepart(year,print_dat)=2005
   union all
  select distinct null,null,'','','','', f_nos, min(dat),
         1 set_id
    from nol_art_izmaina 
   where datepart(year,print_dat)=2005
   group by f_nos
) tablePlusHeaders
order by f_nos, set_id, dat desc
于 2012-05-07T11:43:12.963 回答