查找表中只有Balls和Runs列的总运行次数

时间:2018-06-13 15:40:57

标签: sql sql-server oracle

表 - 板球:


Balls Runs
1     6
2     0
3     1
4     3
5     2
6     1
1     0
2     2
3     6
4     1
5     4
6     1
.
.continue n no of times..

现在输出应该是:

Over Runs
1    13  ( sum of first 6 balls which makes a over)
2    14 

按照上面的例子继续这个..

任何人都可以帮我在Oracle或SQL Server中获取此功能吗?

4 个答案:

答案 0 :(得分:1)

对于您问题中的结构(即仅BallsRuns列),这是不可能的。 SQL数据库中的表没有自然顺序,因此无法保证您将以任何特定顺序获取数据。您需要有一个列,允许您根据需要对行进行分组。例如,假设您有一个Over列:

Over Balls Runs
1    1     6
1    2     0
1    3     1
1    4     3
1    5     2
1    6     1
2    1     0
2    2     2
2    3     6
2    4     1
2    5     4
2    6     1

您可以使用简单的分组查询,例如:

SELEC "Over", SUM(Runs) AS Runs
FROM Cricket
GROUP BY "Over"

答案 1 :(得分:1)

就像我在评论中说的那样,你不能这样做。作为一个演示:

USE Sandbox;
GO

CREATE TABLE Cricket (Ball int, Runs int);
INSERT INTO Cricket
VALUES (1,6),
       (2,0),
       (3,1),
       (4,3),
       (5,2),
       (6,1),
       (1,0),
       (2,2),
       (3,6),
       (4,1),
       (5,4),
       (6,1);
GO
WITH CTE AS (
    SELECT Ball, Runs,
           (ROW_NUMBER() OVER (ORDER BY NEWID())-1) / 6 AS [Over]--NEWID as your table has nothing to identify an order
    FROM Cricket)
SELECT [Over],
       SUM(Runs) AS Runs --This won't be correct
FROM CTE
GROUP BY [Over];

GO
DROP TABLE Cricket;
GO

你需要有人知道球的顺序,所以,我添加了IDENTITY栏:

--Fix the problem, add an identity

CREATE TABLE Cricket (ID int IDENTITY(1,1), Ball int, Runs int);
INSERT INTO Cricket (Ball, Runs)
VALUES (1,6),
       (2,0),
       (3,1),
       (4,3),
       (5,2),
       (6,1),
       (1,0),
       (2,2),
       (3,6),
       (4,1),
       (5,4),
       (6,1);
GO
WITH CTE AS (
    SELECT Ball, Runs,
           (ROW_NUMBER() OVER (ORDER BY ID)-1) / 6 AS [Over]
    FROM Cricket)
SELECT [Over],
       SUM(Runs) AS Runs --Hazaar!
FROM CTE
GROUP BY [Over];

GO
DROP TABLE Cricket;

现在你得到了正确的结果。但是,根据您现在拥有的数据,答案是不可能

答案 2 :(得分:0)

您确实需要第3列来跟踪或包含行的唯一顺序ID。

可以在没有Oracle的情况下完成它但是你依赖于数据库总是以相同的顺序读取行(虽然这通常是正确的但并非总是如此 - 特别是在并行系统上或者如果表有行运动已启用)。一旦以不同的顺序读取行,那么结果将被搞砸 - 所以添加第三列而不是依赖可能发生变化的事物。

您可以使用ROW_NUMBER()分析函数和ROWNUM伪列来模拟sequnce值:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE table_name ( Balls, Runs ) AS
  SELECT 1,     6 FROM DUAL UNION ALL
  SELECT 2,     0 FROM DUAL UNION ALL
  SELECT 3,     1 FROM DUAL UNION ALL
  SELECT 4,     3 FROM DUAL UNION ALL
  SELECT 5,     2 FROM DUAL UNION ALL
  SELECT 6,     1 FROM DUAL UNION ALL
  SELECT 1,     0 FROM DUAL UNION ALL
  SELECT 2,     2 FROM DUAL UNION ALL
  SELECT 3,     6 FROM DUAL UNION ALL
  SELECT 4,     1 FROM DUAL UNION ALL
  SELECT 5,     4 FROM DUAL UNION ALL
  SELECT 6,     1 FROM DUAL;

查询1

SELECT overs,
       SUM( runs ) AS runs
FROM   (
  SELECT t.*,
         ROW_NUMBER() OVER ( PARTITION BY Balls ORDER BY ROWNUM ) AS overs
  FROM   table_name t
)
GROUP BY overs

<强> Results

| OVERS | RUNS |
|-------|------|
|     1 |   13 |
|     2 |   14 |

答案 3 :(得分:-1)

SELECT超时,SUM(运行)AS运行 来自(   SELECT t。,ROW_NUMBER()OVER(按Mod ORDER BY球划分)AS OVERS   FROM(SELECT t。,mod(balls,6)mod from CRICKET t)t

)按分数分组;