如何在SQL Server中使用Joins编写具有排名功能的此特定的Co相关子查询?

时间:2018-12-10 07:21:39

标签: sql sql-server tsql join

假设您有下表中的城市,酒店,价格。您需要编写代码才能找到每个城市中最便宜的酒店,并且要比同一城市中价格最高的酒店便宜%。仅使用联接。只能加入!

以下是示例:请仅在TEST_DB中运行。

create table citycheap 
(
     city varchar(100), 
     Hotel varchar(100), 
     prici money
)

insert into citycheap 
values ('Poway', 'Ramada Inn', 100),  ('Poway', 'Elks Oaks', 70),
       ('Poway', 'Days Inn', 85),
       ('Long Beach', 'Days Inn', 95), ('Long Beach', 'Motel 8', 65),
       ('Long Beach', 'Hampton Inn', 105),
       ('San Diego', 'Motel 6', 55), ('San Diego', 'Beach Inn', 115),
       ('San Diego', 'Days Inn', 85)

select * 
from citycheap

enter image description here

3 个答案:

答案 0 :(得分:2)

您根本不需要加入:

WITH DataSource AS 
(
    select city
          ,Hotel
          ,prici
          ,ROW_NUMBER() OVER (PARTITION BY city ORDER BY prici ASC) AS rowID
          ,MAX(prici) OVER (PARTITION BY city) AS total_price
    from citycheap
)
SELECT city
      ,hotel
      ,prici as LowPrice
      ,CAST((total_price - prici) * 100.0 / total_price AS DECIMAL(9,2)) as [% Cheapter]
FROM DataSource
WHERE rowID = 1;

enter image description here


WITH DataSource AS
(
    SELECT city
          ,MIN(prici) as min_price
          ,MAX(prici) as max_price
          ,CAST((MAX(prici) - MIN(prici)) * 100.0 / MAX(prici) AS DECIMAL(9,2)) as [% Cheapter]
    FROM citycheap
    GROUP BY  city
)
SELECT CH.city
      ,CH.Hotel
      ,CH.prici  as LowPrice
      ,DS.[% Cheapter]
FROM citycheap CH
INNER JOIN DataSource DS
    ON CH.prici = DS.[min_price]

答案 1 :(得分:1)

这将起作用:

myPojo

示例输出:

select * 
from 
    (select 
         city, Hotel, prici as min_prici,
         rank() over (partition by city order by prici asc) rank, 
         100-((min(prici) over (partition by city) / max(prici) over (partition by city)) * 100) as percentcheaper
     from 
         d061_citycheap) 
where 
    rank = 1;

答案 2 :(得分:1)

这是“仅联接”版本。我本人更喜欢CTE /窗口聚合版本:

declare @citycheap table(city varchar(100), Hotel varchar(100), prici money)
insert into @citycheap  (city,Hotel,prici) values
('Poway', 'Ramada Inn', 100), 
('Poway', 'Elks Oaks', 70),
('Poway', 'Days Inn', 85),
('Long Beach', 'Days Inn', 95),
('Long Beach', 'Motel 8', 65),
('Long Beach', 'Hampton Inn', 105),
('San Diego', 'Motel 6', 55),
('San Diego', 'Beach Inn', 115),
('San Diego', 'Days Inn', 85)

select
    low.city,
    low.Hotel,
    low.prici,
    100 * (high.prici - low.prici) / high.prici as PercentCheaper
from
        @citycheap low
            left join
        @citycheap low_anti
            on
                low.city = low_anti.city and
                low.prici > low_anti.prici
    inner join
        @citycheap high
            left join
        @citycheap high_anti
            on
                high.city = high_anti.city and
                high.prici < high_anti.prici
    on
        high.city = low.city
where
    high_anti.Hotel is null and
    low_anti.Hotel is null

您可以希望看到我如何构建lowhigh的对称性。 left联接和where子句的组合(确保联接不成功)意味着每一个都是各自城市中的最低价或最高价。

然后,我们简单地将lowhigh连接在一起。结果:

city          Hotel       prici    PercentCheaper
------------- ----------- -------- ----------------
Poway         Elks Oaks   70.00    30.00
Long Beach    Motel 8     65.00    38.0952
San Diego     Motel 6     55.00    52.1739

还请注意我如何将表变量用于示例数据而不是永久表。这样,我就可以在任何数据库中制作此文件,而不必担心会留下任何残留物。