Pivot both ticket table and result table vertically; join those pivoted tables on round id and the one of the six numbers resulting out of pivoting vertically, and count.
Here goes:
WITH
-- some input data ...
ticket(roundid , playerid , num1 , num2 , num3 , num4 , num5 , num6) AS (
SELECT 1,1, 5,13,22,27,42,49
UNION ALL SELECT 2,1, 5,13,22,27,42,49
UNION ALL SELECT 3,1, 5,13,22,27,42,49
UNION ALL SELECT 1,2, 6,14,23,28,44,48
UNION ALL SELECT 2,2, 6,14,23,28,44,48
UNION ALL SELECT 3,2, 6,14,23,28,44,48
UNION ALL SELECT 1,3, 7,15,24,29,43,47
UNION ALL SELECT 2,3, 7,15,24,29,43,47
UNION ALL SELECT 3,3, 7,15,24,29,43,47
)
,
-- some input data ...
lotteryresult(roundid , num1 , num2 , num3 , num4 , num5 , num6) AS (
SELECT 1, 5, 23, 29,34,36,40
UNION ALL SELECT 2, 1, 12, 16,23,31,48
UNION ALL SELECT 3, 4, 12, 13,19,31,46
)
,
-- real query starts here, replace comma above with "WITH"
-- need a series of integers ...
i(i) AS (
SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
)
,
unpivot_tk AS (
SELECT
roundid
, playerid
, CASE i
WHEN 1 THEN num1
WHEN 2 THEN num2
WHEN 3 THEN num3
WHEN 4 THEN num4
WHEN 5 THEN num5
WHEN 6 THEN num6
END AS num
FROM ticket CROSS JOIN i
)
,
unpivot_res AS (
SELECT
roundid
, CASE i
WHEN 1 THEN num1
WHEN 2 THEN num2
WHEN 3 THEN num3
WHEN 4 THEN num4
WHEN 5 THEN num5
WHEN 6 THEN num6
END AS num
FROM lotteryresult CROSS JOIN i
)
SELECT
tk.roundid
, tk.playerid
, COUNT(*) AS hits
FROM unpivot_tk tk
JOIN unpivot_res USING(roundid,num)
GROUP BY
tk.roundid
, tk.playerid
ORDER BY
tk.roundid
, tk.playerid
-- out roundid | playerid | hits
-- out ---------+----------+------
-- out 1 | 1 | 1
-- out 1 | 2 | 1
-- out 1 | 3 | 1
-- out 2 | 2 | 2
-- out 3 | 1 | 1
ticket) and 3 rounds forlottery resultto your question, so I don' need to type any results from my side?. I have a solution, but I'm too lazy to create test data ...wherein a select, if you want multiple conditions, useANDandOR.