I have this statement:
SELECT *
FROM sgtn
WHERE sgtn_kun_id IN (SELECT DISTINCT kun_id
FROM sgtn, kun
WHERE kun.kun_e_mail IN (LONG LIST OF EMAILS)
AND sgtn_kun_id = kun_id)
AND sgtn_strasse IN (SELECT sgtn.sgtn_strasse
FROM sgtn
WHERE sgtn_kun_id IN (SELECT DISTINCT kun_id
FROM sgtn, kun
WHERE kun.kun_e_mail IN (LONG LIST OF EMAILS)
AND sgtn_kun_id = kun_id)
GROUP BY sgtn.sgtn_strasse
HAVING COUNT(sgtn_strasse) > 2);
LONG LIST OF EMAILS is i.e.:
‘abc@domain.com’,
‘def@domain.com’,
.
.
.
‘xyz@domain.com’
As you can see I repeat some part of subquerys in this query.
I’m wondering if and how can I replace LONG LIST OF EMAILS. It occurs in my statement twice. Would it be possible to edit this query, so that the mentioned LONG LIST OF EMAILS occurs once?
First, you don’t need to use
it is enough
the rows having multiple sgtn_strasse and email in your list can be detected with