Avatar billede Slettet bruger
28. juni 2005 - 12:38 Der er 14 kommentarer og
2 løsninger

Order by expression

Jeg får en fejl når jeg prøver at køre min query:

The ORDER BY expression (IFF(Ucase(testing.[Product] ALike '%CALIBRATION%',1,2)) includes fields that are not selected by the query, Only those fields requested in the first query can be included in an ORDER BY expression.

Min query virker fint uden ORDER BY delen.. testing.[Product] er selected både før og efter min UNION ALL..

Mine SELECT og GROUP BY er ens før og efter UNION ALL..

Hvis jeg nøjes med at sortere alm (asc/desc) og ikke efter indhold (med IFF()), virker det fint..

Jeg har læst noget om en fejl og en løsning på denne, men jeg kan ikke rigtigt få noget ud af denne løsning (den er ikke uddybende nok) - http://www.kbalertz.com/kb_282322.aspx



SELECT SUM(Time_spent.hours) AS Time_spent, testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

FROM multisystem INNER JOIN (testing LEFT JOIN Time_spent ON testing.ID = Time_spent.id) ON multisystem.name = testing.[Test platform]

WHERE multisystem.member_1 = 'CPH_CRTU#2'  OR multisystem.member_2 =  'CPH_CRTU#2'  OR multisystem.member_3 =  'CPH_CRTU#2'  OR multisystem.member_4 =  'CPH_CRTU#2'  AND ucase(testing.[Status]) NOT LIKE ('%DONE%') AND ucase(testing.[Status]) NOT LIKE ('%WAITING%') AND ucase(testing.[Test platform]) NOT LIKE ('%REPORT%')

GROUP BY testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

UNION ALL

SELECT SUM(Time_spent.hours) AS Time_spent, testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

FROM testing LEFT JOIN Time_spent ON testing.ID = Time_spent.id

WHERE testing.[test platform] =  'CPH_CRTU#2'  AND ucase(testing.[Status]) NOT LIKE ('%DONE%') AND ucase(testing.[Status]) NOT LIKE ('%WAITING%') AND ucase(testing.[Test platform]) NOT LIKE ('%REPORT%')

GROUP BY testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

ORDER BY IIF(ucase(testing.[Product]) LIKE ('%CALIBRATION%'), 1, 2),IIF(ucase(testing.[Product]) LIKE ('%MAINTANANCE%'), 1, 2),IIF(len(testing.[Priority]) > 0, 1, 2) ,testing.[Priority] asc,IIF(len(testing.[TCC target date]) > 0, 1, 2) ,testing.[TCC target date] asc,IIF(len(testing.[PP Demand date]) > 0, 1, 2) ,testing.[PP Demand date] asc,IIF(len(testing.[Duration]) > 0, 1, 2) ,testing.[Duration] asc,IIF(len(testing.[Remaining]) > 0, 1, 2) ,testing.[Remaining] asc

/1..
Avatar billede Slettet bruger
28. juni 2005 - 13:15 #1
Har nu prøvet uden UNION ALL, hvor hvor hvert queryfragment fik ODER BY-delen på:

Hvis queryen ser sådan her ud

frag_1
UNION ALL
frag_2
ORDER BY

har jeg altså prøvet

frag_1
ORDER BY
-virker

frag_2
ORDER BY
-virker

Hvad kan problemer være?? fatter banjo af det her :P..
Avatar billede terry Ekspert
28. juni 2005 - 15:40 #2
are you saying that the second select WITH order by .... works?


SELECT SUM(Time_spent.hours) AS Time_spent, testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

FROM testing LEFT JOIN Time_spent ON testing.ID = Time_spent.id

WHERE testing.[test platform] =  'CPH_CRTU#2'  AND ucase(testing.[Status]) NOT LIKE ('%DONE%') AND ucase(testing.[Status]) NOT LIKE ('%WAITING%') AND ucase(testing.[Test platform]) NOT LIKE ('%REPORT%')

GROUP BY testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

ORDER BY IIF(ucase(testing.[Product]) LIKE ('%CALIBRATION%'), 1, 2),IIF(ucase(testing.[Product]) LIKE ('%MAINTANANCE%'), 1, 2),IIF(len(testing.[Priority]) > 0, 1, 2) ,testing.[Priority] asc,IIF(len(testing.[TCC target date]) > 0, 1, 2) ,testing.[TCC target date] asc,IIF(len(testing.[PP Demand date]) > 0, 1, 2) ,testing.[PP Demand date] asc,IIF(len(testing.[Duration]) > 0, 1, 2) ,testing.[Duration] asc,IIF(len(testing.[Remaining]) > 0, 1, 2) ,testing.[Remaining] asc
Avatar billede terry Ekspert
28. juni 2005 - 15:42 #3
If it does then try it on the first SELECT

You also need to make sure that the resulting column names in BOTH SELECTS are the same as in the ORDER BY fields!!!
Avatar billede terry Ekspert
28. juni 2005 - 15:44 #4
If you run the query without the order by so (that it works) then you can see the column names. These are the names you have to use when you sort
Avatar billede Slettet bruger
28. juni 2005 - 15:55 #5
Order by virker seperat på begge selects, men når jeg prøver at gøre det på dem begge samlet (ved hjælp af UNION ALL), virker det ikke.. Det var det jeg prøvede at vise i min første kommentar.. Jeg bruger den samme order by flere steder i mit system, hvor jeg selecter de samme kolonner..

Jeg synes, det er lidt mærkeligt, at det virker på 1. og 2, men ikke på dem samlet..
Avatar billede terry Ekspert
28. juni 2005 - 21:14 #6
could I see your database?


eksperten@NOSPAMsanthell.dk

remove NOSPAM
Avatar billede terry Ekspert
28. juni 2005 - 21:16 #7
Try changing your order by to the names of the columns you see in the query when you do NOT use order by

It might be something like

ORDER BY IIF(ucase([Product]) ....
in other words WITHOUT the table names
Avatar billede Slettet bruger
29. juni 2005 - 14:03 #8
Databasen er sendt via mail..

Det virkede ikke at udelade table names :(..
Avatar billede terry Ekspert
29. juni 2005 - 14:57 #9
Here is a solution which works although it s not maybe the most elegant

Make a query WITHOUT the order by

Now make another query where you select all fields from the first query and then add the order by.

If you are using this in ASP then you can always make the select on the query instead of the writing the whole SQL in ASP.
Avatar billede Slettet bruger
29. juni 2005 - 19:38 #10
Hvordan ville dette se ud i praksis??
Avatar billede terry Ekspert
29. juni 2005 - 20:09 #11
?
not sure what you mean there!

Do you know how to make a query in Access?

Save all of the SQL without the ORDER BY in a query. Lets say the query has the name myQuery

Now you would use

SELECT * FROM myQuery
ORDER BY IIF(ucase(testing.[Product]) LIKE ('%CALIBRATION%'), 1, 2),IIF(ucase(testing.[Product]) LIKE ('%MAINTANANCE%'), 1, 2),IIF(len(testing.[Priority]) > 0, 1, 2) ,testing.[Priority] asc,IIF(len(testing.[TCC target date]) > 0, 1, 2) ,testing.[TCC target date] asc,IIF(len(testing.[PP Demand date]) > 0, 1, 2) ,testing.[PP Demand date] asc,IIF(len(testing.[Duration]) > 0, 1, 2) ,testing.[Duration] asc,IIF(len(testing.[Remaining]) > 0, 1, 2) ,testing.[Remaining]
Avatar billede Slettet bruger
30. juni 2005 - 07:41 #12
Betyder det, at jeg skal eksekvere min sql: rs.open sql,... og derefter bruge rs-værdien som myQuery? - altså noget i retning af dette??

sql = "SELECT * FROM (...)"
rs.open sql,...

sql_with_orderpart = "SELECT * FROM " & rs & "
ORDER BY IIF(ucase(testing.[Product]) LIKE ('%CALIBRATION%'), 1, 2),IIF(ucase(testing.[Product]) LIKE ('%MAINTANANCE%'), 1, 2),IIF(len(testing.[Priority]) > 0, 1, 2) ,testing.[Priority] asc,IIF(len(testing.[TCC target date]) > 0, 1, 2) ,testing.[TCC target date] asc,IIF(len(testing.[PP Demand date]) > 0, 1, 2) ,testing.[PP Demand date] asc,IIF(len(testing.[Duration]) > 0, 1, 2) ,testing.[Duration] asc,IIF(len(testing.[Remaining]) > 0, 1, 2) ,testing.[Remaining]"
Avatar billede Slettet bruger
30. juni 2005 - 07:41 #13
Jeg taget til Afrika i 2 uger senere i dag, så det kan være jeg får lidt svært ved at svare lige med det sammen (skal først afsted kl. 2)
Avatar billede terry Ekspert
30. juni 2005 - 08:42 #14
In Access you can make your query and SAVE it with a name. A bit like a table. Then you can use the query in your ASP code as if it were a table


So if your SQL before was
"SELECT SUM(Time_spent.hours) AS Time_spent, testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

FROM multisystem INNER JOIN (testing LEFT JOIN Time_spent ON testing.ID = Time_spent.id) ON multisystem.name = testing.[Test platform]

WHERE multisystem.member_1 = 'CPH_CRTU#2'  OR multisystem.member_2 =  'CPH_CRTU#2'  OR multisystem.member_3 =  'CPH_CRTU#2'  OR multisystem.member_4 =  'CPH_CRTU#2'  AND ucase(testing.[Status]) NOT LIKE ('%DONE%') AND ucase(testing.[Status]) NOT LIKE ('%WAITING%') AND ucase(testing.[Test platform]) NOT LIKE ('%REPORT%')

GROUP BY testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

UNION ALL

SELECT SUM(Time_spent.hours) AS Time_spent, testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

FROM testing LEFT JOIN Time_spent ON testing.ID = Time_spent.id

WHERE testing.[test platform] =  'CPH_CRTU#2'  AND ucase(testing.[Status]) NOT LIKE ('%DONE%') AND ucase(testing.[Status]) NOT LIKE ('%WAITING%') AND ucase(testing.[Test platform]) NOT LIKE ('%REPORT%')

GROUP BY testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

ORDER BY IIF(ucase(testing.[Product]) LIKE ('%CALIBRATION%'), 1, 2),IIF(ucase(testing.[Product]) LIKE ('%MAINTANANCE%'), 1, 2),IIF(len(testing.[Priority]) > 0, 1, 2) ,testing.[Priority] asc,IIF(len(testing.[TCC target date]) > 0, 1, 2) ,testing.[TCC target date] asc,IIF(len(testing.[PP Demand date]) > 0, 1, 2) ,testing.[PP Demand date] asc,IIF(len(testing.[Duration]) > 0, 1, 2) ,testing.[Duration] asc,IIF(len(testing.[Remaining]) > 0, 1, 2) ,testing.[Remaining] asc"


it will now be

"SELECT * from myQuery ORDER BY
ORDER BY IIF(ucase([Product]) LIKE ('%CALIBRATION%'), 1, 2),IIF(ucase([Product]) LIKE ('%MAINTANANCE%'), 1, 2),IIF(len([Priority]) > 0, 1, 2) ,[Priority] asc,IIF(len([TCC target date]) > 0, 1, 2) ,[TCC target date] asc,IIF(len([PP Demand date]) > 0, 1, 2) ,[PP Demand date] asc,IIF(len([Duration]) > 0, 1, 2) ,[Duration] asc,IIF(len([Remaining]) > 0, 1, 2) ,[Remaining] asc"



So the query which is saved in Access with the name myQuery contains the SQL


"SELECT SUM(Time_spent.hours) AS Time_spent, testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]

FROM testing LEFT JOIN Time_spent ON testing.ID = Time_spent.id

WHERE testing.[test platform] =  'CPH_CRTU#2'  AND ucase(testing.[Status]) NOT LIKE ('%DONE%') AND ucase(testing.[Status]) NOT LIKE ('%WAITING%') AND ucase(testing.[Test platform]) NOT LIKE ('%REPORT%')

GROUP BY testing.[Product],testing.[TOM TR name],testing.[Test sheet links],testing.[TCC target date],testing.[PP Demand date],testing.[Duration],testing.[Remaining],testing.[Time spend],testing.[Last updated],testing.[Status],testing.[EUT available],testing.[Testing started],testing.[Done by],testing.[Comments & AP],testing.[PCM],testing.[Type],testing.[Stream & position],testing.[Test request description],testing.[Test Platform],testing.[Progress status],testing.[Priority],testing.[ID]"


So you still only use one recordset in your code



Have a good holiday in Afrika, hope the lions dont get you :o)
Avatar billede Slettet bruger
30. juni 2005 - 09:13 #15
ok.. det laver jeg så - selvom det ikke er så pænt :p..

Jeg har ikke lige tid til at tjekke, om det virker, men det regner jeg med, så jeg accepterer dit svar.. tak for hjælpen..
Avatar billede terry Ekspert
30. juni 2005 - 12:29 #16
thanks
post a comment if you still need help when you get back off your holidays
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester