AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-207235

Shir K's questions

Martin Hope
Shir K
Asked: 2020-05-09 01:21:03 +0800 CST

使用 'case when count()=0' & 'where' 时结果不正确

  • 3

如果有人能帮我解决这个问题,我将不胜感激。case when count()=0使用&时我似乎无法得到正确的结果where


我使用 SQL Server,使用类似于“Facebook”的系统数据库。

我正在尝试编写一个显示电子邮件地址、标记状态和过去一个月发布的帖子数量的查询。标记状态应如下: 如果标记 1-2 个帖子 - 显示“few” 如果标记 3-5 个帖子 - 显示“medium” 如果标记 5+ 个帖子 - 显示“many” 如果根本不标记 - 显示“none” .

下面是我写的查询。我写的查询的问题是,由于 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
where datediff(day,P.DatePosted,getdate()) <= 30 --Because of this condition I would never get 'none'
group by U.Mail, U.Gender

数据举例:

所需的输入应该是: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
('[email protected]','k1000000','Kelly','Ka','1992-05-15','2016-09-04','F','Kelly','Kelly.jpg','546296100'),
('[email protected]','l1101111','Lilly','La','1999-04-03','2012-04-04','F','Lilly','Lilly.jpg','542448300'),
('[email protected]','n130131','Nelly','Na','1994-03-07','2020-04-13','F','Nelly','NellyNa.jpg','541234567');
('[email protected]','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','[email protected]'),
('','hii','','','2020-02-19','[email protected]');


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
('[email protected]','1','[email protected]'),
('[email protected]','1','[email protected]');
sql-server relational-theory
  • 2 个回答
  • 877 Views
Martin Hope
Shir K
Asked: 2020-05-05 08:48:04 +0800 CST

使用 4 个表的 JOIN 查询结果不正确

  • 4

如果有人可以帮助我完成下面的查询,那就太好了!这个查询非常具有挑战性,我尝试自己这样做,但无法显示正确的结果......

我使用类似于“Facebook”的系统的数据库。

查询应显示在过去一年中为每个帖子撰写评论的用户的电子邮件和姓名,这些帖子由他们的每个朋友发布。

意思是,我只想显示对所有朋友的所有帖子发表评论的人。


这是我的试验,但我收到了部分结果。我无法理解我做错了什么。

select distinct U.Mail, U.FirstName + ' ' + U.LastName as FullName 
from Users U
inner join FriendsList FL on U.Mail = FL.Mail1
inner join Post P on FL.Mail2 = P.UserMail
left outer join Comment C on P.ID = C.IDPost and P.UserMail <> C.Mail
where datediff(year, P.DatePosted, getdate()) <= 1
group by U.Mail, U.FirstName, U.LastName
having count(distinct P.ID) = count(distinct C.IDPost)

链接到 BAK 文件(包括测试数据): https ://file.io/SagvM3cx

表格图: 在此处输入图像描述


例子:

假设我和亚当和本是朋友。

⭐️ 第一种情况:

  • 亚当写了 1 篇文章,我发表了评论。
  • Ben 写了 1 篇文章,我没有发表评论。

然后,我的名字不应该显示在查询中。?

  • 亚当写了 1 篇文章,我发表了评论。
  • Ben 写了 1 个帖子,我发表了评论。

然后,我的名字应该显示在查询中。?

⭐️第二种情况:

  • 亚当写了 2 个帖子,我评论了第一个,但没有评论第二个。
  • Ben 写了 1 个帖子,我发表了评论。

然后,我的名字不应该显示在查询中。?

  • 亚当写了 2 篇文章,我都发表了评论。
  • Ben 写了 1 个帖子,我发表了评论。

然后,我的名字应该显示在查询中。?

⭐️ 第三种情况:

  • 亚当写了 2 篇文章,我都发表了评论。
  • Ben 写了 1 个帖子,我发表了评论。
  • 马克(不是我的朋友)写了 1 篇文章,我没有发表评论。

然后,我的名字应该显示在查询中。?


编辑:我添加了 8 个查询 - 4create table和 4 insert。根据这个例子,上面的查询应该只显示Kelly

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
('[email protected]','k1000000','Kelly','Ka','1992-05-15','2016-09-04','F','Kelly','Kelly.jpg','546296100'),
('[email protected]','l1101111','Lilly','La','1999-04-03','2012-04-04','F','Lilly','Lilly.jpg','542448300'),
('[email protected]','m120121','Moshe','Ma','1995-06-03','2011-04-02','M','Moshe','MosheMa.jpg','542840111'),
('[email protected]','n130131','Nelly','Na','1994-03-07','2020-04-13','F','Nelly','NellyNa.jpg','541234567');
('[email protected]','o140141','Owen','Oa','1992-02-02','2020-05-13','M','Owen','OwenOa.jpg','541234567');

create table FriendsList
(
Mail1 nvarchar (20) references Users(Mail) not null,
Mail2 nvarchar (20) references Users(Mail) not null,
DateAdding date check (DateAdding<=getDate()) not null,
primary key (Mail1,Mail2)
)

INSERT INTO FriendsList
VALUES
('[email protected]','[email protected]','2018-04-18'),
('[email protected]','[email protected]','2020-04-22'),
('[email protected]','[email protected]','2020-04-22'),
('[email protected]','[email protected]','2020-04-22'),
('[email protected]','[email protected]','2018-04-18');
('[email protected]','[email protected]','2020-04-22'),

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-04-18','[email protected]'),
('','hii','','','2020-05-19','[email protected]');

create table Comment
(
IDPost int references Post(ID) on delete cascade on update cascade not null,
SerialNumComment int check(SerialNumComment > 0) not null,
DateAndTimeComment Datetime check (DateAndTimeComment<=getDate()) not null,
Text nvarchar(200) not null,
Mail nvarchar (20) references Users(Mail) not null,
primary key (IDPost,SerialNumComment)
)

INSERT INTO Comment
VALUES
('1','1','2020-04-18','blabla','[email protected]'),
('2','1','2020-05-05','bhfk','[email protected]');
sql-server relational-division
  • 1 个回答
  • 64 Views
Martin Hope
Shir K
Asked: 2020-04-23 05:07:43 +0800 CST

如何在 CASE 中计算 NULL?

  • 1

NULL计算内部值的最佳方法是什么case?

使用连接表 - 我正在尝试计算列的值P.ID(来自连接表),并 通过 case.

例如:

  • 当计数为 0 - 显示“无”
  • 当计数 1-2 - 显示“很少”。
  • 当计数 3-5 - 显示'中
  • 当计数 5+ - 显示“很多”

但是,由于从连接中收到的一些值是空的 - 我无法计算它们。

我应该对查询进行哪些更改才能将每个都引用NULL为 0?

SELECT 
    U.Mail, 
    COUNT(P.ID) AS PostCount, 
    CASE
        WHEN COUNT(P.ID) = 0 THEN'none' --doesn't work
        WHEN COUNT(P.ID) <= 2 THEN 'few'
        WHEN COUNT(P.ID) <= 5 THEN 'medium'
        ELSE 'many'
    END AS PostCountCategory
FROM
    Users U 
LEFT JOIN 
    Tag T ON U.Mail = T.Mail
LEFT JOIN 
    Post P on T.IDPost = P.ID
GROUP BY U.Mail;
sql-server
  • 1 个回答
  • 535 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve