Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.03 KB | None | 0 0
  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;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement