【问题标题】:Can you COUNT() the result of a CASE statement that is aliased "AS" something你能 COUNT() 别名为“AS”的 CASE 语句的结果吗
【发布时间】:2020-02-06 19:22:37
【问题描述】:

我想弄清楚我是否可以计算我的 CASE 别名函数导致的 Wins 和 Loses 列。我已经包含了我的表格、插入、选择语句和输出图像。在图像中,您将看到由 CASE 创建的“赢家”和“输家”列。我希望以某种方式 COUNT() 那些在团队名称上有 where 子句的人。

我的目标是显示每支球队的输赢数字。所以在这种情况下,列应该显示 TeamName、Wins、Loses。在它们下面按顺序应该是 Sharks, 2, 0。

表格:

create table team (
  teamID        number(2,0) not null,
  teamAbb       varchar2(3),
  teamName      varchar2(20),
  location      varchar2(20),
  division      varchar2(20),
  conference    varchar2(20),
  constraint pk_team primary key (teamID)
);

create table game(
 gameID     number(2,0) not null,
 gameDate   date,
 homeTeam   number(2,0),
 homeScore  number(2,0),
 awayTeam   number(2,0),
 awayScore  number(2,0),
 constraint pk_game primary key (gameID),
 constraint fk_homeTeam foreign key (homeTeam) references team (teamID),
 constraint fk_awayTeam foreign key (awayTeam) references team (teamID)
);

插入:

DECLARE

BEGIN

insert into team values(10, 'ANA', 'Ducks', 'Anaheim', 'Pacific', 'Western');
insert into team values(11, 'ARI', 'Coyotes', 'Arizona', 'Pacific', 'Western');
insert into team values(12, 'BOS', 'Bruins ', 'Boston', 'Atlantic', 'Eastern');
insert into team values(13, 'BUF', 'Sabers ', 'Buffalo', 'Atlantic', 'Eastern');
insert into team values(14, 'CGY', 'Flames ', 'Calgary', 'Pacific', 'Western');
insert into team values(15, 'CAR', 'Hurricanes', 'Carolina', 'Metropolitan', 'Eastern');
insert into team values(16, 'CHI', 'Blackhawks', 'Chicago', 'Central', 'Western');
insert into team values(17, 'COL', 'Avalanche', 'Colorado', 'Central', 'Western');
insert into team values(18, 'CBJ', 'Blue Jackets', 'Columbus', 'Metropolitan', 'Eastern');
insert into team values(19, 'DAL', 'Stars', 'Dallas', 'Central', 'Western');
insert into team values(20, 'DET', 'Red Wings', 'Detroit', 'Atlantic', 'Eastern');
insert into team values(21, 'EDM', 'Oilers', 'Edmonton', 'Pacific', 'Western');
insert into team values(22, 'FLA', 'Panthers', 'Florida', 'Atlantic', 'Eastern');
insert into team values(23, 'LAK', 'Kings', 'Los Angeles', 'Pacific', 'Western');
insert into team values(24, 'MIN', 'Wild', 'Minnesota', 'Central', 'Western');
insert into team values(25, 'MTL', 'Canadiens', 'Montreal', 'Atlantic', 'Eastern');
insert into team values(26, 'NSH', 'Predators', 'Nashville', 'Central', 'Western');
insert into team values(27, 'NJD', 'Devils', 'New Jersey', 'Metropolitan', 'Eastern');
insert into team values(28, 'NYI', 'Islanders', 'New York', 'Metropolitan', 'Eastern');
insert into team values(29, 'NYR', 'Rangers', 'New York ', 'Metropolitan', 'Eastern');
insert into team values(30, 'OTT', 'Senators', 'Ottawa', 'Atlantic', 'Eastern');
insert into team values(31, 'PHI', 'Flyers', 'Philadelphia', 'Metropolitan', 'Eastern');
insert into team values(32, 'PIT', 'Penguins', 'Pittsburgh', 'Metropolitan', 'Eastern');
insert into team values(33, 'SJS', 'Sharks', 'San Jose', 'Pacific', 'Western');
insert into team values(34, 'STL', 'Blues', 'St. Louis', 'Central', 'Western');
insert into team values(35, 'TBL', 'Lightning', 'Tampa Bay', 'Atlantic', 'Eastern');
insert into team values(36, 'TOR', 'Maple Leafs', 'Toronto', 'Atlantic', 'Eastern');
insert into team values(37, 'VAN', 'Canucks', 'Vancouver', 'Pacific', 'Western');
insert into team values(38, 'VGK', 'Golden Knights', 'Vegas', 'Pacific', 'Western');
insert into team values(39, 'WSH', 'Capitals', 'Washington', 'Metropolitan', 'Eastern');
insert into team values(40, 'WPG', 'Jets', 'Winnipeg', 'Central', 'Western');

COMMIT;

END;

DECLARE

BEGIN

insert into game values (1, to_date('01-11-2020','mm-dd-yyyy'), 33, 2, 20, 1);
insert into game values (2, to_date('01-13-2020','mm-dd-yyyy'), 11, 5, 33, 6);
insert into game values (3, to_date('03-04-2020','mm-dd-yyyy'), 20, null, 13, null);

COMMIT;

END;

我的选择语句:

 SELECT gameDate,
 t1.teamName AS homeTeam, g.homeScore, 
 t2.teamName AS awayTeam, g.awayScore, 
  CASE WHEN homescore > awayscore THEN t1.teamname
  WHEN awayscore > homescore THEN t2.teamname
  ELSE null END AS winner,
  CASE WHEN homescore < awayscore THEN t1.teamname
  WHEN awayscore < homescore THEN t2.teamname
  ELSE null END AS loser
FROM game g
 INNER JOIN team t1 
 ON t1.teamID = g.homeTeam
 INNER JOIN team t2 
 ON t2.teamID = g.awayTeam;

【问题讨论】:

  • “鲨鱼”从何而来?
  • 您的预期结果是什么。
  • @Littlefoot 抱歉,我添加了不正确的插入,我已修复它。
  • @AnkitBajpai 我希望能够计算团队表中每个团队的赢/输金额。我希望通过添加 COUNT(winner WHERE Winner = 'Sharks') 之类的东西来做到这一点。但我不确定是否可以在由 AS 别名创建的列上使用 COUNT()。最后,我想显示一个输出,在列中列出每个团队,在他们的名字旁边的列中列出他们的每一个胜利和失败记录。
  • @Littlefoot,鲨鱼来自圣何塞。 (很多年前也有洛杉矶鲨鱼队。)

标签: sql oracle


【解决方案1】:

如果我们循环查询你已经写的,剩下的就是获取赢家和输家并计算他们的赢/输。

SQL> with your_query as (
  2  SELECT gameDate,
  3   t1.teamName AS homeTeam, g.homeScore,
  4   t2.teamName AS awayTeam, g.awayScore,
  5    CASE WHEN homescore > awayscore THEN t1.teamname
  6    WHEN awayscore > homescore THEN t2.teamname
  7    ELSE null END AS winner,
  8    CASE WHEN homescore < awayscore THEN t1.teamname
  9    WHEN awayscore < homescore THEN t2.teamname
 10    ELSE null END AS loser
 11  FROM game g
 12   INNER JOIN team t1
 13   ON t1.teamID = g.homeTeam
 14   INNER JOIN team t2
 15   ON t2.teamID = g.awayTeam
 16   )
 17  -- fetch winners and count their wins
 18   select
 19     winner,
 20     count(*) cnt_win,
 21     0 cnt_lose
 22    from your_query
 23    where winner is not null
 24    group by winner
 25  union all
 26  -- fetch losers and count their loses
 27    select loser,
 28      0 cnt_win,
 29      count(*) cnt_lose
 30    from your_query
 31    where loser is not null
 32    group by loser;

WINNER                  CNT_WIN   CNT_LOSE
-------------------- ---------- ----------
Sharks                        2          0
Coyotes                       0          1
Red Wings                     0          1

SQL>

【讨论】:

    【解决方案2】:

    我会选择所有赢家和所有输家,并即时计算他们的输赢比赛。然后带队并加入两个计数。

    with winners as
    (
      select
       case when homescore > awayscore then hometeam else awayteam end as teamid,
       count(*) as total
      from game
      where homescore <> awayscore
      group by case when homescore > awayscore then hometeam else awayteam end
    )
    , losers as
    (
      select
        case when homescore < awayscore then hometeam else awayteam end as teamid,
        count(*) as total
      from game
      where homescore <> awayscore
      group by case when homescore < awayscore then hometeam else awayteam end
    )
    select
      t.name,
      coalesce(w.total, 0) as won_games,
      coalesce(l.total, 0) as lost_games
    from team t
    left join winners w on w.teamid = t.teamid
    left join losers l on l.teamid = t.teamid
    order by t.name;
    

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多