Tsql 如何使用where子句遍历表的数据

Tsql 如何使用where子句遍历表的数据,tsql,loops,Tsql,Loops,我有以下数据库和查询: CREATE TABLE table1 (`group` int, `points` int) ; INSERT INTO table1 (`group`, `points`) VALUES (1, 115), (2, 125), (1, 105), (2, 000), (3, 005), (1, 020), (2, 005), (1, 010), (2, 005), (3,

我有以下数据库和查询:

CREATE TABLE table1
    (`group` int, `points` int)
;

INSERT INTO table1
    (`group`, `points`)
VALUES
    (1, 115),
    (2, 125),
    (1, 105),
    (2, 000),
    (3, 005),
    (1, 020),
    (2, 005),
    (1, 010),
    (2, 005),
    (3, 030),
    (2, 000),
    (2, 055),
    (2, 100),
    (1, 020),
    (3, 055),
    (3, 055),
    (1, 005),
    (1, 010),
    (2, 025),
    (1, 035),
    (2, 100),
    (1, 120),
    (3, 140),
    (3, 105),
    (1, 065),
    (3, 025),
    (4, 015),
    (1, 005),
    (2, 010),
    (1, 130),
    (4, 040),
    (1, 055),
    (4, 020),
    (4, 060),
    (3, 010),
    (3, 105),
    (4, 125),
    (3, 000),
    (2, 005),
    (2, 010),
    (1, 115)
;

CREATE TABLE soruce1
    (`group` int)
;

INSERT INTO soruce1
    (`group`)
VALUES
    (1),
    (2),
    (3),
    (4)
;

  select s1.`group`, SUM(t1.`points`) FROM table1 as t1
    inner join soruce1 as s1
      on s1.`group` = t1.`group`
  where s1.`group` = 1
  GROUP BY s1.`group`
  ORDER BY SUM(t1.`points`) ASC;
(SQL小提琴链接)

如何使用where子句循环source1中的所有值,而不必使用where循环,这样当它完成对组1的查询时,它将移动到组2,依此类推,直到表的末尾,select自然会在Insert中使用


这只是一个数据示例,source1有近5000个条目

注释掉WHERE行
——WHERE s1.group=1
,您将得到4行?这就是你的意思吗?

注释掉WHERE行
——WHERE s1.group=1
,你会得到4行?这就是您的意思吗?

您可以删除where子句,查询将聚合每个组的总和


您可以删除where子句,查询将聚合每个组的总和


facepalm和我一直在努力让它工作,当答案如此简单的时候,查询是由我团队中的其他人完成的,我不得不编辑它,这样搜索就不会被硬编码,谢谢facepalm,我一直在努力让它工作,当答案如此简单的时候,这个查询是由我团队中的其他人完成的,我必须对它进行编辑,这样搜索就不会被硬编码,谢谢,虽然原始查询更复杂,但它做的事情完全相同,我不敢相信答案这么简单,一定是性能问题,虽然原始查询更复杂,但它做的是完全相同的事情,我不敢相信答案如此简单,这一定是我遇到的性能问题
 select s1.[group], SUM(t1.points) FROM table1 as t1
    inner join soruce1 as s1
      on s1.[group] = t1.[group]
  --where s1.[group] = 1
  GROUP BY s1.[group]
  ORDER BY SUM(t1.points) ASC;