Avatar billede eksperten1 Nybegynder
25. november 2008 - 12:25 Der er 4 kommentarer

Forkert sortering ved FULL OUTER JOIN

Jeg joiner nogle tabeller, heraf en FULL OUTER JOIN - for at finde de records som IKKE er repræsenteret i en given tabel.

MEN ak ak - hvis min OUTER JOIN er med, så får jeg ikke den sortering som jeg havde forventet...

Mit query er således :
SELECT TOP 1 a.docid, a.defname, a.status, a.blobdata, c.groupname, c.enterlimit, c.pretext, c.posttext FROM documents AS a INNER JOIN web_defname AS b ON a.defname = b.defname INNER JOIN web_groups AS c ON b.groupid = c.groupid LEFT OUTER JOIN web_timelock AS d ON a.docid = d.docid WHERE a.status = 4 AND (d.id IS NULL OR(d.done = 0 AND DATEDIFF(minute, d.ts, GETDATE()) > 5) ) ORDER BY c.rank, a.createtime;

Konceptet er at jeg opretter en record i tabellen web_timelock, med brugernavn samt docid og et timestamp. Så når brugerne efterspørger den næste fil til behandling, så skal der returneres den næste i rækken - sorteret efter c.rank's værdi - men istedet får jeg en "vilkårlig" i rækkefølgen - jeg sætter vilkårlig i anførselstegn fordi den er ikke vilkårlig, jeg kan bare ikke finde ud af hvilken nøgle den sorteres efter...

Her er et resultat for top 100
2455918    N_SCH_HP      4    <Binary data>    Schenker    8    NULL    00017100
2455919    N_SCH_HP      4    <Binary data>    Schenker    8    NULL    00017100
2455921    N_SCH_HP      4    <Binary data>    Schenker    8    NULL    00017100
2455922    N_SCH_HP      4    <Binary data>    Schenker    8    NULL    00017100
2455315    N_LEM          4    <Binary data>    Leman    20        NULL
2455567    N_LEM          4    <Binary data>    Leman    20        NULL
2454026    N_LMG_11      4    <Binary data>    LMG    8    NULL    00070060
2454056    N_LMG_11      4    <Binary data>    LMG    8    NULL    00070060
2454194    N_LMG_11      4    <Binary data>    LMG    8    NULL    00070060
2456551    N_LMG_11      4    <Binary data>    LMG    8    NULL    00070060
2156527    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2156591    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2156672    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2156706    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2156959    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157129    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157180    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157203    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157272    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157367    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157601    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157611    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157663    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157667    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2157782    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2158179    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2158413    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2158530    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2158605    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2158606    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2159028    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2159058    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2159081    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2159354    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2159356    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2159543    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2159813    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160049    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160056    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160066    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160079    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160154    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160478    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160516    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160524    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160533    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160683    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160897    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2160966    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161080    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161186    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161223    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161407    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161587    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161751    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161811    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161939    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161948    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2161969    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2162002    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2162058    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2162460    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2162870    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2162899    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2162909    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2162936    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2163032    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2163656    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2163736    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2163867    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2163870    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2163921    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164134    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164155    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164273    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164737    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164739    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164740    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164746    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164800    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164809    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164826    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164828    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164849    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164856    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164857    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164870    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164896    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164899    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164905    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2164921    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2165313    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2165383    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2165457    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2165652    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2165781    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2165936    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2166145    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2166175    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
2166266    O_TRG_11      4    <Binary data>    TRG    NULL    NULL    NULL
Avatar billede eksperten1 Nybegynder
25. november 2008 - 12:28 #1
Det skal nævnes at der pt. er 208 af typen Schenker, 10 af typen LEMAN, 26 af typen LMG og mere end 70.000 af typen TRG

Alligevel så hvis web_timelock er tom, så får jeg ovenstående resultat. Efterhånden som web_timelock bliver udfyldt med matchende docid + done=1 så kommer vi bare ned i den ovenstående omgang data - der kommer altså ikke "nye" schenker ind i rækkefølgen.

Anyone ????
Avatar billede ldanielsen Nybegynder
27. november 2008 - 15:51 #2
Det er svært at gennemskue, for du har jo ikke udskrevet de koloner du sorterer efter. Mit gæt er at der bliver sorteret korrekt efter dem, udfra at ingenting (dvs. fx NULL) kommer først.

Du snakker om et FULL OUTER JOIN, men det er ikke tilstede i den SELECT du viser ... ?
Avatar billede eksperten1 Nybegynder
27. november 2008 - 20:48 #3
Jamen det er da mig der er helt fra den !!!

ldanielsen > You are the winner - forkert query, forkerte data - det viste sig efter en nærmere granskning af resultatet at det VAR korrekt - men den kilde jeg sammenlignede med var forkert :-O

Jeg undskylder mange gange
Avatar billede ldanielsen Nybegynder
13. december 2008 - 00:54 #4
Du tager vel point'ene selv, ikke? Du fik jo ikke noget svar ...
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

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



Seneste spørgsmål Seneste aktivitet
I går 20:46 opkaldside Af hagbartm i Mobiltelefoner
I går 16:05 win 10 vil ikke boote Af bb69 i Windows
I går 11:20 Lenovo x390 Af tobberjas i PC
I går 10:14 Alder i Excel Af Nanarsi i Excel
I går 09:00 Flere linier på faneblad Af Peder Lund Nielsen i Excel