I am running an SQL query which self-joins the same table 24 times in order to "look up" particular rows of the table according to 24 different criteria, so that I can use all those values in a calculation. While the performance is perfectly fine (the table is indexed and the join criteria are restrictive), I can't help but feel there is a code smell here.
Is there a better way of doing lookups in SQL?
(Apologies for not including an example; I hope I have phrased the question in a general fashion).
Edit: attempting an example anyway:
CREATE TABLE key (
pk1 int,
pk2 int,
pk3 int,
PRIMARY KEY (pk1, pk2, pk3)
);
CREATE TABLE values (
pk1 int,
pk2 int,
pk3 int,
pk4 int,
pk5 int,
value int,
PRIMARY KEY (pk1, pk2, pk3, pk4, pk5)
);
SELECT k.pk1, k.pk2, k.pk3,
v1.value + v2.value - v3.value * (v4.value / v5.value) + ... + v24.value as result
FROM key k
LEFT JOIN values v1
on v1.pk1=k.pk1
and v1.pk2=k.pk2
and v1.pk3=k.pk3
and v1.pk4=100
and v1.pk5=200
LEFT JOIN values v2
on v2.pk1=k.pk1
and v2.pk2=k.pk2
and v2.pk3=k.pk3
and v2.pk4=400
and v2.pk5=800
...
LEFT JOIN values v24
on v24.pk1=k.pk1
and v24.pk2=k.pk2
and v24.pk3=k.pk3
and v24.pk4=900
and v24.pk5=700;
Edit 2: The reason for this structure is that the values table represents (mathematically speaking) a function of 5 variables, with pre-computed return values stored in the table for a variety of parameters.