Shi*_*r K 3 sql-server relational-theory
如果有人能帮我解决这个问题,我将不胜感激。使用case when count()=0
&时我似乎无法得到正确的结果where
我使用 SQL Server,使用类似于“Facebook”的系统数据库。
我正在尝试编写一个查询,显示电子邮件地址、标记状态和过去一个月发布的帖子数量。标记状态应如下: 如果标记 1-2 个帖子-显示“很少” 如果标记 3-5 个帖子-显示“中等” 如果标记 5+ 个帖子-显示“许多” 如果根本不标记-显示“无” .
下面是我写的查询。我写的查询的问题是,由于 WHERE 中的条件,我永远不会收到“none”。
如何更改查询以显示正确的结果?
select U.Mail, count(P.ID) as PostCount,
case
when count(P.ID) = 0 then 'none'
when count(P.ID) <= 2 then 'few'
when count(P.ID) <= 5 then 'medium'
else 'many'
end PostCountCategory
from Users U
left join Tagging T on U.Mail = T.Mail
left join Post P on T.IDPost = P.ID
where datediff(day,P.DatePosted,getdate()) <= 30 --Because of this condition I would never get 'none'
group by U.Mail, U.Gender
Run Code Online (Sandbox Code Playgroud)
数据例如:
所需的输入应该是:Kelly-'none',Lilly-'none',Nelly-'few',Owen-'none'。
create table Users
(
Mail nvarchar (20) primary key check(Mail like '_%@_%._%' and (Mail like '%[0-9]%' Or Mail like '%[a-z]%'Or Mail like '%[A-Z]%')),
Password nvarchar (8) check (Password like '%[0-9]%' and Password like '%[az]%' and len(password) <= 8) not null,
FirstName nvarchar (20) not null,
LastName nvarchar (20) not null,
BirthDate date check (datediff(year,BirthDate,getdate())>=18) not null,
JoinDate date check (JoinDate<=getdate()) not null,
Gender nchar(1) check(Gender = 'F' or Gender = 'M' or Gender = 'O'),
NickName nvarchar(20),
Photo nvarchar(20),
Phone bigint check (Phone like '%[0-9]%' and len(Phone) <= 10) not null
)
INSERT INTO Users
VALUES
('Kelly@gmail.com','k1000000','Kelly','Ka','1992-05-15','2016-09-04','F','Kelly','Kelly.jpg','546296100'),
('Lilly@gmail.com','l1101111','Lilly','La','1999-04-03','2012-04-04','F','Lilly','Lilly.jpg','542448300'),
('Nelly@gmail.com','n130131','Nelly','Na','1994-03-07','2020-04-13','F','Nelly','NellyNa.jpg','541234567');
('Owen@gmail.com','o140141','Owen','Oa','1992-02-02','2020-05-13','M','Owen','OwenOa.jpg','541234567');
create table Post
(
ID int identity(1,1) primary key,
Photo nvarchar(20),
Text nvarchar(200),
Location nvarchar(50),
Video int,
DatePosted date check (datediff(month,DatePosted,getdate())<=3) not null,
UserMail nvarchar (20) references Users(Mail) on delete cascade on update
cascade not null
)
INSERT INTO Post
VALUES
('','my name is nellu','','','2020-05-08','Nelly@gmail.com'),
('','hii','','','2020-02-19','Lilly@gmail.com');
create table Tagging
(
Mail nvarchar (20) references Users(Mail) not null,
IDPost int references Post(ID) not null,
TagMail nvarchar(20) references Users (Mail) not null,
primary key (Mail,IDPost);
)
INSERT INTO Tagging
VALUES
('Nelly@gmail.com','1','Kelly@gmail.com'),
('Nelly@gmail.com','1','Owen@gmail.com');
Run Code Online (Sandbox Code Playgroud)
只需将 where 条件移动到连接部分:
select U.Mail, count(P.ID) as PostCount,
case
when count(P.ID) = 0 then 'none'
when count(P.ID) <= 2 then 'few'
when count(P.ID) <= 5 then 'medium'
else 'many'
end PostCountCategory
from Users U
left join Tagging T on U.Mail = T.Mail
left join Post P on T.IDPost = P.ID AND datediff(day,P.DatePosted,getdate()) <= 30
group by U.Mail, U.Gender
Run Code Online (Sandbox Code Playgroud)
您的原始条件也不允许 SQL Server 使用索引查找。最好以这种方式重写它:
P.DatePosted >= dateadd(day, -30, cast(getdate() as date))
Run Code Online (Sandbox Code Playgroud)