SHARE
TWEET

Untitled

a guest Feb 23rd, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH a AS (SELECT ba.Name, c.ContractStartDate, c.ContractEndDate, ba.BusinessActivityStartDate
  2. FROM ShopOwner sh JOIN Contract c ON sh.ShopOwnerID = c.ShopOwnerID
  3.                 JOIN Shop s ON s.ShopID = c.ShopID
  4.                 JOIN BusinessActivity ba ON
  5.                 ba.ShopID = s.ShopID AND ba.ContractStartDate = c.ContractStartDate
  6.                 WHERE sh.ShopOwnerID = 11),
  7.      b AS (SELECT ba.Name, c.ContractStartDate, c.ContractEndDate, MAX(ba.BusinessActivityStartDate) mxy
  8. FROM ShopOwner sh JOIN Contract c ON sh.ShopOwnerID = c.ShopOwnerID
  9.                 JOIN Shop s ON s.ShopID = c.ShopID
  10.                 JOIN BusinessActivity ba ON
  11.                 ba.ShopID = s.ShopID AND ba.ContractStartDate = c.ContractStartDate
  12.                 WHERE sh.ShopOwnerID = 11
  13.                 GROUP BY 2)
  14. SELECT a.Name, a.ContractStartDate, a.ContractEndDate, a.BusinessActivityStartDate FROM a JOIN b
  15. ON  a.ContractStartDate = b.ContractStartDate
  16. AND a.ContractEndDate = b.ContractEndDate
  17. AND a.BusinessActivityStartDate = b.mxy;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top