计算行中非空字段的数量

发布于 2024-12-24 15:46:02 字数 810 浏览 1 评论 0原文

可能的重复:
计算 SQL 中一行中的 Null 列 < /p>

I我已经深入研究过这个问题的答案,但找不到有效或合适的答案,而且我是 SQL 新手。

我有一个包含活动团队的表格,其中包括 team_member_1team_member_2team_member_3team_member_4< /code>,team_member_5。每个团队都存储该团队成员的姓名,每个团队都有不同数量的成员(3 到 5 人之间)。

我正在尝试通过计算该行/团队中有多少列/字段NOT NULL来计算团队中的人数。

如果我的头脑会这样想:

Select Count NOT NULL team_member_1, team_member_2, team_member_3,  
                      team_member_4, team_member_5 
from Teams 
where team_id = 5

当然这是行不通的。然后我想将这个数字乘以 20 英镑(因为赛事成本为每人 20 英镑),得到每个团队所欠的金额,这将在网站上显示出来。希望一切都有道理。

Possible Duplicate:
Count the Null columns in a row in SQL

I've had a dig around for answers to this, but can't find either a working or suitable answer and I'm a novice with SQL.

I've got a table containing teams for an event, and amongst other columns I have team_member_1, team_member_2, team_member_3, team_member_4, team_member_5. Each one stores the name of that team memember, each team has a different amount of members (between 3 and 5).

I'm trying count the number of people in a team by counting how many columns/ fields within that row/team are NOT NULL.

If my head if would go something like:

Select Count NOT NULL team_member_1, team_member_2, team_member_3,  
                      team_member_4, team_member_5 
from Teams 
where team_id = 5

Of course this doesn't work. I then want to times this figure by £20 (as the event cost is £20 per head), to give me the amount each team owes, and that will be echo'd out on the website. Hope all makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

栖竹 2024-12-31 15:46:02

如果您的服务器产品支持布尔数据类型,并在适当的上下文中将其隐式转换为整数(例如MySQL),那么您可以尝试以下模式:

SELECT
  (team_member_1 IS NOT NULL) +
  (team_member_2 IS NOT NULL) +
  (team_member_3 IS NOT NULL) +
  (team_member_4 IS NOT NULL) +
  (team_member_5 IS NOT NULL) AS MemberCount
FROM Teams
WHERE team_id = 5

If your server product supports the boolean data type and implicitly converts it to integer when in a proper context, like MySQL does, for instance, then you could try the following pattern:

SELECT
  (team_member_1 IS NOT NULL) +
  (team_member_2 IS NOT NULL) +
  (team_member_3 IS NOT NULL) +
  (team_member_4 IS NOT NULL) +
  (team_member_5 IS NOT NULL) AS MemberCount
FROM Teams
WHERE team_id = 5
梦纸 2024-12-31 15:46:02

您应该有一个关系表 TeamMember ->团队关系为 1:n \ n:n。

然后在查询中连接两个表,按团队分组并对团队成员进行计数。

select t.Id, count(tm.Id)
from teams t, teams_members tm
where t.Id = tm.Team_Id
group by t.Id

You should have a relation table TeamMember -> Team with relation of 1:n \ n:n.

Then in your query join the two tables,Group by the team and count the team members.

select t.Id, count(tm.Id)
from teams t, teams_members tm
where t.Id = tm.Team_Id
group by t.Id
猫九 2024-12-31 15:46:02

恕我直言,这是一个非常糟糕的设计:)

首先,我将标准化数据库。如果这样做了,关于团队成员数量的问题就是一个简单的 SELECT COUNT(*) ...

为团队制作一张表格,一张为成员,第三张为您“连接”的表格团队成员。

Imho, that's a really bad design :)

First of all, I would normalize the db. If that is done, your question about the number of team members is a simple SELECT COUNT(*) ...

Make a table for the teams, one for the member and a third where you "concat" the team member with a team.

与之呼应 2024-12-31 15:46:02

解决问题的关键是使用特定的 if 函数

对于 Oracle,它可能是 DECODE

SELECT 
 DECODE(COL1, null, 0, 1) +
 DECODE(COL2, null, 0, 1) +
 DECODE(COL3, null, 0, 1) +
 DECODE(COL4, null, 0, 1) AS Result
FROM YOUR_TABLE

对于 MySQL,它可以是 IF

SELECT 
 IF(COL1 is null, 0, 1) +
 IF(COL2 is null, 0, 1) +
 IF(COL3 is null, 0, 1) +
 IF(COL4 is null, 0, 1) AS Result
FROM YOUR_TABLE

如果您使用的是 SQL Server,则应该使用 CASE 语句

SELECT
    CASE COL1 WHEN IS NULL THEN 0 ELSE 1 END + 
    CASE COL2 WHEN IS NULL THEN 0 ELSE 1 END +
    CASE COL3 WHEN IS NULL THEN 0 ELSE 1 END 
FROM YOUR_TABLE

The key to solve your problem is to use specific if function

For Oracle it could be DECODE.

SELECT 
 DECODE(COL1, null, 0, 1) +
 DECODE(COL2, null, 0, 1) +
 DECODE(COL3, null, 0, 1) +
 DECODE(COL4, null, 0, 1) AS Result
FROM YOUR_TABLE

For MySQL it could be IF.

SELECT 
 IF(COL1 is null, 0, 1) +
 IF(COL2 is null, 0, 1) +
 IF(COL3 is null, 0, 1) +
 IF(COL4 is null, 0, 1) AS Result
FROM YOUR_TABLE

I you are using the SQL Server, you should use CASE statement

SELECT
    CASE COL1 WHEN IS NULL THEN 0 ELSE 1 END + 
    CASE COL2 WHEN IS NULL THEN 0 ELSE 1 END +
    CASE COL3 WHEN IS NULL THEN 0 ELSE 1 END 
FROM YOUR_TABLE
遗心遗梦遗幸福 2024-12-31 15:46:02

正如其他人指出的那样,这是一个糟糕的设计,您应该尝试标准化。但是,我会尝试这样的操作:

Select Count(*) from teams where
 (team_id = 5) and
 (team_member_1 is not null or
  team_member_2 is not null or
  team_member_3 is not null or
  team_member_4 is not null or
  team_member_5 is not null)

您至少应该通过创建团队表和成员表并将成员引用到团队来标准化为第一个范式。

As others have pointed out, this is a bad design and you should try to normalize. However, I'd try something like this:

Select Count(*) from teams where
 (team_id = 5) and
 (team_member_1 is not null or
  team_member_2 is not null or
  team_member_3 is not null or
  team_member_4 is not null or
  team_member_5 is not null)

You should at least normalize to the first normal form by creating a Team table and a Member table and reference Member to Team.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文