简体   繁体   English

如何在SQL查询中返回0值?

[英]How to return 0 value in SQL query?

How many (sum) goals were scored by each footballer in a given tournament in ascending order. 在给定的锦标赛中,每个足球运动员按升序计分进球数(总和)。 For those footballers with 0 goals, we should return name of the footballer with value 0. Point being, footballers with 0 goals should also be part of output. 对于那些进球数为0的足球运动员,我们应返回值0的足球运动员的名称。要点是,进球数为0的足球运动员也应作为输出的一部分。

Footballer name | Goal  | Tournament
Messi           |    3  | La liga
Ronaldo         |    5  | UEFA
Suarez          |    2  | La liga

Output should be for 'La Liga' output should be: 输出应为“西甲”,输出应为:

Ronaldo  0
Suarez   2
Messi    3

Try this 尝试这个

select footballer, sum(goals)
from (
   select footballer, CASE
   WHEN tournament = 'LA_LIGA' THEN goals
     else 0 
   END 
   from tableA) AS tabA
group by footballer

Hope it help. 希望对您有所帮助。

I'm not sure what's the structure of your database, I think normally the score is rewarded to a team instead of individuals. 我不确定您的数据库的结构是什么,我认为通常分数会奖励给团队而不是个人。 Anyway, here is just the concept: 无论如何,这只是概念:

Tables: 表格:

  1. Players = {playerID, name, age, gender, height, etc..} 玩家= {玩家ID,姓名,年龄,性别,身高等。}

  2. Games = {gameID, playerID, score...} 游戏= {游戏ID,玩家ID,得分...}

  3. Tournaments = {tournamentID, gameID, tournamentName...} 锦标赛= {锦标赛ID,游戏ID,锦标赛名称...}

      select p.name, IFNULL(g.score,0), t.tournamentName from players p left join Games g on p.playerID = g.playerID left join Tournaments t on g.GameID = t.GameID where t.tournamentName = 'La liga' 

Solution: My point is you may need to use IFNULL(..) and set the default value to 0. 解决方案:我的意思是您可能需要使用IFNULL(..)并将默认值设置为0。

try this.... 尝试这个....

Select FootballerName, 
sum(CASE WHEN tournament = 'LA LIGA' THEN Goal  else 0 END) as Goal 
from #Matches group by FootballerName

want to get all goals by players in all tournament 想要在所有锦标赛中获得玩家的所有进球

Select FootballerName, sum(Goal) as Goal 
from #Matches group by FootballerName

want to know goals touranmentwise 想明智地了解目标

Select FootballerName, sum(Goal) as Goal,Tournament 
from #Matches group by FootballerName,Tournament

Unless you have 3 tables (players, tournaments, and goals) you'll need to create 3 sets using subselects and join them together. 除非您有3个表(玩家,锦标赛和目标),否则您将需要使用子选择创建3套并将它们连接在一起。 You need the cross-product of all tournaments and all footballers. 您需要所有比赛和所有足球运动员的交叉产品。

SELECT footballer_name, tournament FROM
    (SELECT DISTINCT footballer_name FROM your_table) AS players
  JOIN
    (SELECT DISTINCT tournament FROM your_table) AS tournaments

The above should get you one record per unique pairing of footballer and tournament. 以上内容可为您将足球运动员和锦标赛的每对独特配对创造一条记录。 See if you can get this working in your database and then finish it off with a final join for scores 看看是否可以在数据库中使用它,然后最后加入分数以结束它

Source Of Information: Pivot query help us to generate an interactive table that quickly combines and compares large amounts of data. 信息源:数据透视查询可帮助我们生成一个交互式表格,该表格可以快速组合和比较大量数据。 We can rotate its rows and columns to see different summaries of the source data, and we can display the details for areas of interest at a glance. 我们可以旋转其行和列以查看源数据的不同摘要,并且可以一目了然显示感兴趣区域的详细信息。 It also help us to generate Multidimensional reporting. 它还有助于我们生成多维报告。

Create Table script 创建表脚本

Create table Matches(FootballerName varchar(100),Goal int ,Tournament varchar(100))

Sql Server Insert Records script SQL Server插入记录脚本

insert into Matches(FootballerName,Goal,Tournament)
select 'Messi ', 3 ,'La liga' union
select 'Ronaldo ', 5 ,'UEFA' union
select 'Surez ', 2 ,'La liga'

MySql Server script MySQL服务器脚本

   Create table Matches(FootballerName varchar(100),Goal int ,Tournament varchar(100));
insert into Matches(FootballerName,Goal,Tournament)
select 'Messi ', 3 ,'La liga' union
select 'Ronaldo ', 5 ,'UEFA' union
select 'Surez ', 2 ,'La liga';

MYSQL Version MYSQL版本

Select FootBallerName,
Sum(CASE WHEN Tournament= 'La liga' THEN goal ELSE 0 END) AS 'La liga',
Sum(CASE WHEN Tournament= 'UEFA' THEN goal ELSE 0 END) AS 'UEFA'from Matches 
group by FootBallerName order by FootBallerName;

SQL Server Query using Pivot --- for Only [La liga] 使用Pivot的SQL Server查询---仅适用于[La liga]

SELECT FootballerName,isnull([La liga],0) as'La liga'
FROM (
    SELECT 
        FootballerName  ,isnull(goal,0) as'goal', 
        Tournament
    FROM Matches
) as s
PIVOT
(
    SUM(goal)
    FOR Tournament IN ([La liga],[UEFA])
)AS pvt order by [La liga]

SQL Server Query using Pivot --- Only [La liga] and [UEFA] 使用Pivot的SQL Server查询---仅[La liga]和[UEFA]

SELECT FootballerName,isnull([La liga],0) as'La liga',isnull([UEFA],0) as'UEFA'
FROM (
    SELECT 
        FootballerName  ,isnull(goal,0) as'goal', 
        Tournament
    FROM Matches
) as s
PIVOT
(
    SUM(goal)
    FOR Tournament IN ([La liga],[UEFA])
)AS pvt order by [La liga],[UEFA]

You can read more [ https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx] 您可以阅读更多[ https://technet.microsoft.com/zh-cn/library/ms177410(v=sql.105).aspx]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM