I have a table from Poland Lotto Multi Multi 20/80 drawings history. So, each row contain 20 columns with numbers ranging from 1 to 80, no repeating numbers inside the row.
There are a few draws where 80 and 77 pair appears together in the last draws (check image link) but on the SQL result it seems that there isn't any 80 or even 77 number, the biggest being just 63 on nr2 column (check image link for details). I am trying this piece of SQL query code and it seems that somehow it doesn't "catch" the all columns and it check at the maximum column number 10 (where the maximum number is 63 in this case) and it doesn't go further to the column number 20 (where the maximum possible number stored is 80).
This is the full SQL query code I used:
CREATE TABLE IF NOT EXISTS unpivot AS
SELECT *
FROM (
SELECT DrawId, N1 as n_value FROM draws union all
SELECT DrawId, N2 as n_value FROM draws union all
SELECT DrawId, N3 as n_value FROM draws union all
SELECT DrawId, N4 as n_value FROM draws union all
SELECT DrawId, N5 as n_value FROM draws union all
SELECT DrawId, N6 as n_value FROM draws union all
SELECT DrawId, N7 as n_value FROM draws union all
SELECT DrawId, N8 as n_value FROM draws union all
SELECT DrawId, N9 as n_value FROM draws union all
SELECT DrawId, N10 as n_value FROM draws union all
SELECT DrawId, N11 as n_value FROM draws union all
SELECT DrawId, N12 as n_value FROM draws union all
SELECT DrawId, N13 as n_value FROM draws union all
SELECT DrawId, N14 as n_value FROM draws union all
SELECT DrawId, N15 as n_value FROM draws union all
SELECT DrawId, N16 as n_value FROM draws union all
SELECT DrawId, N17 as n_value FROM draws union all
SELECT DrawId, N18 as n_value FROM draws union all
SELECT DrawId, N19 as n_value FROM draws union all
SELECT DrawId, N20 as n_value FROM draws
) as T;
SELECT nr1, nr2, count(*) as total
FROM
(
SELECT up1.n_value as nr1, up2.n_value as nr2
FROM unpivot up1
JOIN unpivot up2
ON up1.DrawId = up2.DrawId
AND up1.n_value < up2.n_value
) T
GROUP BY nr1, nr2
ORDER BY nr2 desc;
-- LIMIT 300;
I suspect the problem is:
CREATE TABLE IF NOT EXISTS unpivot AS . . .
That is, you have an older version of the table and the new version is not being created.
Instead, do:
DROP TABLE IF EXISTS unpivot;
Then:
CREATE TABLE unpivot AS . . .