Ich habe dieses Stück SQL-Code, der die row_number auf einige Werte in einer Tabelle __working Basis findet, die in einer Lookup-Tabelle verbindet __Evalrow_number mit Wert Partition Ändern
;WITH r
AS (
select
w.uid,
t.TypeId,
--weight
ROW_NUMBER() OVER (PARTITION BY w.uid ORDER BY DIFFERENCE(t.val1, w.val1) + DIFFERENCE(t.val2, w.val2) + DIFFERENCE(t.val3, w.val3) + DIFFERENCE(t.val4, w.val4) DESC) as Score
,w.account
from __Working w
join __eval t on w.val1 like t.val1 and IsNull(w.val4, '') like t.val4 and IsNull(w.val2, '') like t.val2 and IsNull(w.val3, '') like t.val3
)
select * from r where r.account = 1 and score = 1
dies liefert eine TypeId = 1
aber wenn ich es wie dies
;WITH r
AS (
select
w.uid,
t.TypeId,
--weight
ROW_NUMBER() OVER (PARTITION BY w.uid ORDER BY DIFFERENCE(t.val1, w.val1) + DIFFERENCE(t.val2, w.val2) + DIFFERENCE(t.val3, w.val3) + DIFFERENCE(t.val4, w.val4) DESC) as Score
,w.account
from __Working w
join __eval t on w.val1 like t.val1 and IsNull(w.val4, '') like t.val4 and IsNull(w.val2, '') like t.val2 and IsNull(w.val3, '') like t.val3
where r.account = 1
)
select * from r where r.account = 1 and score = 1
schreiben gibt es TypeId = 2. ich würde erwarten, dass, wenn ich mehrere UIDs über verschiedene Konten in __working hatte, aber ich weiß nicht. Was fehlt mir hier?
Warum Filter auf R.account im CTE und wieder int er wählen? Filterung innerhalb des CTE wird Ihre ROW_NUMBER() ändern – scsimon
Gordon antwortete richtig. Das einzige, was zu beachten ist, ist, dass DIFFERENCE (t.val1, NULL) NULL wäre. Und NULL + 1 ist NULL. Wenn also nur 1 Ihrer Spalten in den DIFFERENCE-Funktionen null sein kann, wird Ihre gesamte Reihenfolge zu NULL. Ich würde vermuten, dass Sie einige NULL-Handhabung tun wollen, aber nicht sicher von Ihrem Anwendungsfall – Matt
yeah, val1 ist ein nicht-Nullable-Feld, so dass die Nullprüfung nicht benötigt, aber danke – nikolifish