Solved Query sorting

quest

Member
Local time
Today, 01:44
Joined
Nov 26, 2024
Messages
50
Hi,
I have union query and sorting not working. same expression in select query do the job but not in union query. expression is ORDER BY Left([Field],4), DatePart("yyyy",[field1]), table.Field2, table.Field3
what is wrong.
 
See if it works by sorting on column number

Order by N

where N is the column in the select statement. This is 1 based not zero based.
 
How can we help? Please can you show the Query code as well as not having a proper named table or fields doesn't help
 
How can we help? Please can you show the Query code as well as not having a proper named table or fields doesn't help
SELECT Delovodnik.DelovodnikID, Left([Ознака],4) AS Proba1, DatePart("yyyy",[Датум]) AS ГодинаПребарување1, Delovodnik.ОрганизационаЕдиница, Delovodnik.ОсновенБрој, Delovodnik.Подброеви, Delovodnik.Пренос, Delovodnik.Предмет, Delovodnik.ДатумНаПриемот, Delovodnik.[НазивИседиштеНаПравнотоЛице/органот], Delovodnik.Доставено, Delovodnik.БројИдатум, Format([ОрганизационаЕдиница],"00") & "-" & [ОсновенБрој] & "/" & [Подброеви] AS Proba, Delovodnik.Датум, Delovodnik.Ознака, Delovodnik.Документ, Delovodnik.Забелешка, Delovodnik.FizickaSostojba, Val(Mid([Забелешка],InStr(1,[Забелешка],"л*")+1)) AS Листови, Mid([Забелешка],InStrRev([Забелешка],"и ")+1) AS Expr1
FROM Delovodnik
WHERE (((DatePart("yyyy",[Датум])) Like "*" & [forms]![Delovodnik Query3].[combo58] & "*") AND ((Delovodnik.ОрганизационаЕдиница)<>5) AND ((Delovodnik.Ознака) Like "*трајно*") AND ((Delovodnik.Забелешка)<>"0 лист/а и 0 фотoграфија/и"))
ORDER BY Left([Ознака],4), DatePart("yyyy",[Датум]), Delovodnik.ОсновенБрој, Delovodnik.Подброеви
UNION SELECT tblDelovodnik1.DelovodnikID, Left([Ознака],4) AS Proba1, DatePart("yyyy",[Датум]) AS ГодинаПребарување1, tblDelovodnik1.ОрганизационаЕдиница, tblDelovodnik1.ОсновенБрој, tblDelovodnik1.Подброеви, tblDelovodnik1.Пренос, tblDelovodnik1.Предмет, tblDelovodnik1.ДатумНаПриемот, tblDelovodnik1.[НазивИседиштеНаПравнотоЛице/органот], tblDelovodnik1.Доставено, tblDelovodnik1.БројИдатум, Format([ОрганизационаЕдиница],"00") & "-" & [ОсновенБрој] & "/" & [Подброеви] AS Proba, tblDelovodnik1.Датум, tblDelovodnik1.Ознака, tblDelovodnik1.Документ, tblDelovodnik1.Забелешка, tblDelovodnik1.FizickaSostojba, Val(Mid([Забелешка],InStr(1,[Забелешка],"л*")+1)) AS Листови, Mid([Забелешка],InStrRev([Забелешка],"и ")+1) AS Expr1
FROM tblDelovodnik1
WHERE (((DatePart("yyyy",[Датум])) Like "*" & [forms]![Delovodnik Query3].[combo58] & "*") AND ((tblDelovodnik1.ОрганизационаЕдиница)<>5) AND ((tblDelovodnik1.Ознака) Like "*трајно*") AND ((tblDelovodnik1.Забелешка)<>"0 лист/а и 0 фотoграфија/и"));

here is the whole query. can you understand this or should i translate it.
 
Your ORDER BY is in the middle. Try move it to the end of the SQL statement.
 
This needs to be 3 queries. 1 for the first SELECT, 1 for the second SELECT and then the third should be the UNION that just brings the results together and that is the one that you should ORDER.

You're just trying to do to much in one query and its screwing you up. So take the first SELECT and make it its own query (sub1) . Run it, make sure it returns the right results. Then take the second SELECT and make it its own query (sub2). Run it, make sure it returns the right results. Once both of those are working independently, then you make a third query to just UNION those and ORDER them:

Code:
SELECT * FROM sub1
UNION ALL
SELECT * FROM sub2
ORDER BY Left([Ознака],4), DatePart("yyyy",[Датум]), Delovodnik.ОсновенБрој, Delovodnik.Подброеви
 
If you do not try suggestions, waste of time asking for help? :mad:
it is checked. this union query was made by combining two select queries. both have same sorting and work with them. in union can be only one order by and not as the last line. select query above union is the first one and second select query is below union.
 
This needs to be 3 queries. 1 for the first SELECT, 1 for the second SELECT and then the third should be the UNION that just brings the results together and that is the one that you should ORDER.

You're just trying to do to much in one query and its screwing you up. So take the first SELECT and make it its own query (sub1) . Run it, make sure it returns the right results. Then take the second SELECT and make it its own query (sub2). Run it, make sure it returns the right results. Once both of those are working independently, then you make a third query to just UNION those and ORDER them:

Code:
SELECT * FROM sub1
UNION ALL
SELECT * FROM sub2
ORDER BY Left([Ознака],4), DatePart("yyyy",[Датум]), Delovodnik.ОсновенБрој, Delovodnik.Подброеви
it is made that way and works independently but not in union query.
 
it is checked. this union query was made by combining two select queries. both have same sorting and work with them. in union can be only one order by and not as the last line. select query above union is the first one and second select query is below union.

As was explained more than once in this thread, the ORDER BY clause in a UNION query is based on the final SELECT.
You stated that you did not accept that.
 
As was explained more than once in this thread, the ORDER BY clause in a UNION query is based on the final SELECT.
You stated that you did not accept that.
the way i tried before did not accept it and i tried many times. this time with order by n and deleting the ";" accept it. i glad it is solved. if i could find mistake i wouldn't post here. i learned this time and i wont make this mistake twice.
 

Users who are viewing this thread

Back
Top Bottom