title: Postgres comparison same rows,count and match from 3 defferent tables
body: <p>i have 3 tables </p>
  
  <pre><code>CREATE TABLE product (
          productid int,
          name varchar(80),
          safetystocklevel int,
          reorderpoint int,
          standardcost int,
          listprice numeric,
          productcategoryid numeric
          );
  
  CREATE TABLE salesorderdetail (
          salesorderid int,
          salesorderdetailid int,
          orderqty int,
          productid int,
          unitprice numeric,
          unitpricediscount numeric,
          linetotal numeric
          );
  
  CREATE TABLE salesorderheader (
          salesorderid int,
          orderdate date,
          duedate date,
          shipdate date,
          onlineorderflag int,
          customerid int,
          creditcardid int,
          subtotal numeric,
          taxamt  numeric,
          freight numeric,
          totaldue numeric
          );
  </code></pre>
  
  <p>i want to know the clients who made an order witch contains at least 3 products from different categories</p>
  
  <p>i think i should do something like : </p>
  
  <pre><code>SELECT saleorderid,productid FROM salesorderdetail WHERE lag(productid)!= productid AND lead(productid)!= productid AND lag(productid)!=lead(productid))
  
  INTERSECT
  
  (SELECT productid,productcategoryid FROM product WHERE lag(productcategoryid)!=productcategoryid AND lead(productcategoryid)!=productcategoryid AND lead(productcategoryid)!=lag(productcategoryid))
  </code></pre>