SELECT '' AS '#'
-- ,lo.createtime AS '查询日期'
,lo.l1_s AS 'hot'
  ,h.hotel_id
  --   ,lo.checkin,lo.checkout,lo.rooms
  ,h.hotel_name,h.address 
  ,h.country_code
  ,(SELECT c.name FROM city AS c WHERE h.city_id=c.id AND c.lgc=2) AS city
  ,CASE hi.update_flag
    WHEN 99
      THEN '已下架'
    ELSE ''
    END AS current_state
FROM (
  SELECT l1.hotel_id
    ,sum(1) AS l1_s
  FROM (
    SELECT '' AS '#'
      --   ,date(x.createtime)
      ,DATE_FORMAT(x.createtime, '%Y-%m-%d %H:%i') AS createtime
      ,JSON_UNQUOTE(JSON_EXTRACT(x.request_data, '$.hotel_id')) AS 'hotel_id'
      ,JSON_UNQUOTE(JSON_EXTRACT(x.request_data, '$.checkin')) AS 'checkin'
      --     ,JSON_UNQUOTE(JSON_EXTRACT(x.request_data, '$.checkout')) AS 'checkout'
      ,JSON_UNQUOTE(JSON_EXTRACT(x.request_data, '$.rooms')) AS 'rooms'
    --     ,COUNT(1)  AS cnt
    --     ,SUM(1) OVER (PARTITION BY JSON_UNQUOTE(JSON_EXTRACT(x.request_data, '$.hotel_id'))) AS id_total
    --     ,x.*
    FROM hotel_hub.request_logs x
    WHERE `path` = '/availability'
    GROUP BY JSON_UNQUOTE(JSON_EXTRACT(x.request_data, '$.hotel_id'))
      ,JSON_UNQUOTE(JSON_EXTRACT(x.request_data, '$.checkin'))
      ,JSON_UNQUOTE(JSON_EXTRACT(x.request_data, '$.rooms'))
      --     ,date(x.createtime)
      ,DATE_FORMAT(x.createtime, '%Y-%m-%d %H:%i')
    ) AS l1
  GROUP BY l1.hotel_id
  ) AS lo
INNER JOIN hotelinfo AS h ON h.hotel_id = lo.hotel_id
INNER JOIN heytrip_ids AS hi ON hi.hotel_id = h.hotel_id
ORDER BY country_code,lo.l1_s DESC













