F
F
Fyodor2015-07-07 12:13:40
SQL Server
Fyodor, 2015-07-07 12:13:40

How to complete the SQL query to exclude options?

SELECT
          a.*
        FROM(
            SELECT
                dbo.recipient.id,
                dbo.recipient.name,
                dbo.recipient.email,
                dbo.recipient.phone/*,
                dbo.reservation.humanid
                dbo.reservation.number,
                dbo.reservation.cdate*/

            FROM
                (SELECT
                   (SELECT top 1 dbo.human.id
                    FROM dbo.human
                     INNER JOIN dbo.people ON dbo.people.humanid = dbo.human.id
                     INNER JOIN dbo.recipient ON dbo.recipient.id = dbo.human.id
                    WHERE  dbo.people.reservationid = p.id AND (dbo.recipient.phone != '' OR  dbo.recipient.email != '')
                    ORDER BY dbo.human.born) AS huid,
                   p.id,
                   p.number
                 FROM dbo.reservation AS p
                 WHERE  (SELECT top 1 dbo.human.id FROM dbo.human
                         INNER JOIN dbo.people ON dbo.people.humanid = dbo.human.id
                         INNER JOIN dbo.recipient ON dbo.recipient.id = dbo.human.id
                        WHERE dbo.human.born IS NOT NULL AND dbo.people.reservationid = p.id
                         ) IS NOT NULL AND p.statusid = 1 
                  )
                AS huborn
                    INNER JOIN dbo.recipient ON dbo.recipient.id = huborn.huid
                    INNER JOIN dbo.human ON huborn.huid = dbo.human.id
                    INNER JOIN dbo.reservation ON dbo.reservation.id = huborn.id
                    INNER JOIN (SELECT MIN(dbo.subclaim.datebeg)AS dbeg,
                                      dbo.subclaim.reservationid FROM dbo.subclaim
                      GROUP BY dbo.subclaim.reservationid
                                )AS sub ON sub.reservationid = dbo.reservation.id

            WHERE

                 (dbo.human.human = 'ADL') AND (dbo.human.born IS NOT NULL)
          AND DateDiff(day, sub.dbeg, '2015-07-07')  < 548 
                    
                  
      ) AS a GROUP by a.id, a.name, a.phone, a.email

Based on one example, I created such a request, its task is to select people from the database who have been our clients for the last 548 days (one and a half years).
Now you need to select those who traveled with us more than a year and a half ago, but you need to exclude those who still bought in the last one and a half (i.e., a person regularly buys, for example).
I thought you could just get by with adding WHERE a.id NOT IN()
) AS a WHERE a.id NOT IN() GROUP by a.id, a.name, a.phone, a.email

and, accordingly, in IN inside to place the selection that is higher, however, it turned out that such a request, if it is executed, is extremely long. I suspect that this is due to the fact that by selecting 10 thousand records for each, it is probably still formed on request for NOT IN. maybe this data can somehow be remembered beforehand so that the request for NOT IN passes once?
Or maybe I'm wrong and the problem is something else. with SQL similar only I understand, before basically primitive SELECT operated.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2015-07-25
@res2001

Replace in with the appropriate join.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question