DATE IN SQL 上的 RANK() 函数
RANK() Function on DATE IN SQL
我为此使用 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
不要按 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 分组。按名称分组会不小心将两件事组合在一起,并且架构中没有强制执行唯一的国家/地区名称(它们应该是)。
最后,显式连接确实让查询更容易理解和维护。
我为此使用 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
不要按 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 分组。按名称分组会不小心将两件事组合在一起,并且架构中没有强制执行唯一的国家/地区名称(它们应该是)。
最后,显式连接确实让查询更容易理解和维护。