Lead and Lag in SQL -


my table has following columns: product_id, customer_id, start_date, end_date, product, product_use. goal find product customer using @ beginning of each month end of each month? ideas how in sql? thanks.

product_id   cust_id    start_dt    end_dt 8398d           8678    12/18/2013  3/10/2014 270b9           8678    2/14/2014   3/16/2014 c0867           8678    3/25/2014   4/1/2014 2f9d2           8678    4/3/2014    5/7/2014 9ae65           8678    4/25/2014   5/6/2014 

expected results

month    product_begining_of_month     product_end_of_month  december        8398d                  8398d january         8398d                  8398d february        8398d                  8398d , 270b9   march           8398d , 270b9        c0867 april           2f9d2                  2f9d2 , 9ae65 

you select ids, , first , last dates result set; use outer table query these results select productid using inner table results of dates , customerid..

  select custid, (select product_id customertable ctbl2   (custid=inntbl.custid ,  (transcationdate=inntbl.lastmostdate)) lastmostproduct, (select product_id customertable ctbl3    (custid=inntbl.custid_ , (transcationdate=inntbl.firstdate)) firstproduct           (select custid, max(transcationdate) lastmostdate,     min(transcationdate) firstdate customertable ctbl    group custid) inntbl  

Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -