1

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
6
  • 1
    order by clause does not work with union! Commented May 7, 2012 at 11:00
  • @Murtaza Is there a way to order those data? Commented May 7, 2012 at 11:03
  • I think you need to clarify what exactly you are after. You are unioning together two result sets, but you want the results from the first set sorted by a particular column? How is the second set to be ordered? Commented May 7, 2012 at 11:05
  • @MiikaL. Yes, the results from the first set needs to be ordered by a particular column, bu the results from other set stays the way they are now Commented May 7, 2012 at 11:12
  • @a_horse_with_no_name I am using sql server 2005 Commented May 7, 2012 at 11:13

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

your statement was not what I needed, but you gave me an idea, what worked :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.