Advertisement
Inverth

oh

Jan 19th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.01 KB | None | 0 0
  1. select *
  2. from (
  3.     select
  4.         DP.port_id,
  5.         S.ship_id,
  6.         count(S.ship_id) as Cnt,
  7.         V.voyage_id
  8.     from       Captain_Ship_Voyage as CSV
  9.     inner join Voyage              as V   on V.voyage_id = CSV.voyage_id
  10.     inner join DestPoint           as DP  on DP.voyage_id = V.voyage_id
  11.     inner join Ship                as S   on S.ship_id = CSV.ship_id
  12.     group by Dp.port_id, S.[name], S.ship_id, V.voyage_id
  13. ) as tmptmt
  14. where
  15.     Cnt = (
  16.         select max(Cnt)
  17.         from (
  18.             select
  19.                 count(ISh.ship_id) as Cnt
  20.             from       Captain_Ship_Voyage as ICSV
  21.             inner join Voyage              as IV   on IV.voyage_id = ICSV.voyage_id
  22.             inner join DestPoint           as IDP  on IDP.voyage_id = IV.voyage_id
  23.             inner join Ship                as ISh  on ISh.ship_id = ICSV.ship_id
  24.             where
  25.                 IV.voyage_id = tmptmt.voyage_id
  26.             group by IDp.port_id, ISh.[name], ISh.ship_id
  27.         ) as tmpp
  28.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement