在Oracle SQL视图中使用sum可以在每行中添加列总计

时间:2017-05-15 17:25:36

标签: oracle view sum

我有一个简单的Oracle视图,包含3列团队,未成年人,成年人,如下所示: -

    --------------------------------------
       team      | minors | adults| total
    --------------------------------------
      volleyball    2        4       6
      football      6        3       9
      tennis        4        8       12  
    -----------------------------------------   

select语句返回如下行: -

    create view main_summary( team, minors, adults, sum(minors+adults)) ...
     and
     create view main_summary ( team, minors, adults, total)
  as 
     select all_teams.team_name as team , 
     case when age<18 then count(id) as minors,
     case when age>= 18 then count(id) as adults
     sum (  case when age<18 then count(id) + 
       case when age>= 18 then count(id) ) as total ...

我想在最后添加一个总列,使视图看起来像: -

[HttpGet]
        public async Task<IActionResult> ProcessQue()
        {
            List<string> reList = new List<string>();
            try
            {
                // Register a OnMessage callback
                queueClient.RegisterMessageHandler(
                    async (message, token) =>
                    {
                        // Process the message
                        reList.Add($"Received message: SequenceNumber:{message.SequenceNumber} Body:{message.GetBody<string>()}");

                        // Complete the message so that it is not received again.
                        // This can be done only if the queueClient is opened in ReceiveMode.PeekLock mode.
                        await queueClient.CompleteAsync(message.LockToken);
                    },
                    new RegisterHandlerOptions() {MaxConcurrentCalls = 1, AutoComplete = false});
            }
            catch (Exception exception)
            {
                Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
            }

            return Ok(reList);
        }

我试过跟随,但没有任何效果: -

PasswordChar

语法可能不正确,因为我没有直接从我的数据库中复制。但是,伪代码保持不变。请指导我如何实现这一目标。

1 个答案:

答案 0 :(得分:2)

我不知道为什么原始视图有效。我想你想要:

create view main_summary as 
     select t.team_name as team , 
            sum(case when age < 18 then 1 else 0 end) as minors,
            sum(case when age >= 18 then 1 else 0 end) as adults,
            count(*) as total
     from all_teams
     group by t.team_name;