Brezhnews
Brezhnews

Reputation: 1569

Order sql query data

I have a query:

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

What do I need? I need that the data from first query are ordered by a.nos values. How do I do that?

EDIT
The result table should look like this, but only with my data:

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

Upvotes: 1

Views: 123

Answers (1)

Nikola Markovinović
Nikola Markovinović

Reputation: 19346

The error is probably because there is no column nos, but f_nos. To order union you need to add a mark to each select, and then order by f_nos and that mark:

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

Upvotes: 1

Related Questions