Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH a AS (SELECT ba.Name, c.ContractStartDate, c.ContractEndDate, ba.BusinessActivityStartDate
- FROM ShopOwner sh JOIN Contract c ON sh.ShopOwnerID = c.ShopOwnerID
- JOIN Shop s ON s.ShopID = c.ShopID
- JOIN BusinessActivity ba ON
- ba.ShopID = s.ShopID AND ba.ContractStartDate = c.ContractStartDate
- WHERE sh.ShopOwnerID = 11),
- b AS (SELECT ba.Name, c.ContractStartDate, c.ContractEndDate, MAX(ba.BusinessActivityStartDate) mxy
- FROM ShopOwner sh JOIN Contract c ON sh.ShopOwnerID = c.ShopOwnerID
- JOIN Shop s ON s.ShopID = c.ShopID
- JOIN BusinessActivity ba ON
- ba.ShopID = s.ShopID AND ba.ContractStartDate = c.ContractStartDate
- WHERE sh.ShopOwnerID = 11
- GROUP BY 2)
- SELECT a.Name, a.ContractStartDate, a.ContractEndDate, a.BusinessActivityStartDate FROM a JOIN b
- ON a.ContractStartDate = b.ContractStartDate
- AND a.ContractEndDate = b.ContractEndDate
- AND a.BusinessActivityStartDate = b.mxy;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement