0

我为此使用 PopSQL。

请考虑以下几点:我正在尝试使用 PayDate 对每个国家/地区进行排名。对于每个 PayDate,关联的国家/地区不止一个。我的愿望是用特定的 PayDate 或其计数对每个国家/地区进行分组,并相应地对它们进行排名。

CREATE TABLE Country
(
    CountryID INT PRIMARY KEY,
    CountryName VARCHAR(40)
);

CREATE TABLE State
(
    StateID INT PRIMARY KEY,
    StateName VARCHAR(40),
    CountryID INT,
    FOREIGN KEY(CountryID) REFERENCES Country(CountryID)
);

CREATE TABLE City
(
    CityID INT PRIMARY KEY,
    CityName VARCHAR(40),
    StateID INT,
    FOREIGN KEY(StateID) REFERENCES State(StateID)
);

CREATE TABLE Rooms 
(
    RoomID INT PRIMARY KEY,
    RoomTypeID INT,
    RoomBandID INT,
    RoomFacilityID INT,
    CityID INT,
    Floor INT,
    AddionalNotes VARCHAR(255),
    FOREIGN KEY(RoomTypeID) REFERENCES RoomType(RoomTypeID),
    FOREIGN KEY(RoomBandID) REFERENCES RoomBand(RoomBandID),
    FOREIGN KEY(RoomFacilityID) REFERENCES RoomFacility(RoomFacilityID),
    FOREIGN KEY(CityID) REFERENCES City(CityID)
);

CREATE TABLE DT_Date
(
    DateID INT NOT NULL,
    FullDate Datetime NOT NULL, 
    DateMonth INT NOT NULL, 
    Quarter INT NOT NULL,
    DateYear year NOT NULL, 
    PRIMARY KEY(DateID)
);

CREATE TABLE Customer
(
    CustomerID INT PRIMARY KEY,
    CustomerForename VARCHAR(20),
    CustomerSurname VARCHAR(20),
    CustomerDOB DATetime,
    CustomerHomePhone INT,
    CustomerMobilePhone INT,
    CustomerWorkPhone INT,
    CustomerEmail VARCHAR(40),
    CityID INT,
    FOREIGN KEY (CityID) REFERENCES City(CityID)

);

CREATE TABLE Payments
(
    PaymentID INT PRIMARY KEY, 
    PaymentComment VARCHAR(255), 
    PaymentsMethodID INT, 
    PayDate Datetime,
    RoomID INT,
    DateID INT,
    CustomerID INT,
    Price INT,
    PaymentAmount INT,
    FOREIGN KEY(RoomID) REFERENCES Rooms(RoomID),
    FOREIGN KEY(PaymentsMethodID) REFERENCES PaymentsMethod(PaymentsMethodID),
    FOREIGN KEY(DateID) REFERENCES DT_Date(DateID),
    FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID)
);

还请看看我的最后一次尝试:

select 
    rank() over (PARTITION BY CountryName order by count(PayDate)),
    CountryName, PayDate, count(Paydate)
from 
    City, Payments, Rooms, Customer, State, Country, DT_Date
where
    Payments.PayDate >= "2010-00-00 00:00:00" 
    and Payments.CustomerID = Customer.CustomerID
    and State.CountryID = Country.CountryID
    and City.StateID = State.StateID
    and Customer.CityID = City.CityID
    and Payments.DateID = DT_Date.DateID
group by 
    CountryName, PayDate;

查询有效。但是,结果不正确,因为英国有两个 PayDate,美国有一个 PayDate。以下是显示的结果:

Rank CountryName         PayDate       count(Paydate)
1       UK        2015-12-31 00:00:00    4
1       UK        2014-06-10 00:00:00    4
1       USA       2011-11-25 00:00:00    4

预期输出:

Rank CountryName        
1       UK        

2       USA       

4

1 回答 1

0

不要按 PayDate 分组,只能按 CountryName 分组。这意味着您不能将 PayDate 包含在结果中,但无论如何您都不想这样做。

还要从您的分区中删除 CountryName。您的排名仅按 PayDate 计数排序。

并且要使最大的计数排在第一位,请使用降序。

select
  rank() over (order by count(PayDate) desc ) as "rank",
  CountryName
from City , Payments,Rooms,Customer,State,Country,DT_Date
WHERE Payments.PayDate >= '2010-00-00 00:00:00'
ANd Payments.CustomerID = Customer.CustomerID
And State.CountryID = Country.CountryID
AND City.StateID = State.StateID
And Customer.CityID = City.CityID
and Payments.DateID = DT_Date.DateID
GROUP by CountryName

这是一个简化的演示

一些旁注。

尽管它可能适用于您的 SQL 实现,但它2010-00-00 00:00:00是一个狡猾的日期时间。使用2010-01-01 00:00:00或更好地使用您的数据库的等价物year(Payments.PayDate) >= 2010

不要按 CountryName 分组,按 Country.CountryId 分组。按名称分组会导致意外地将两个事物组合在一起,并且模式中不强制执行唯一的国家/地区名称(它们应该是)。

最后,显式连接确实更容易理解和维护查询。

于 2020-04-03T20:16:18.810 回答