Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在sql server中对同一id的多行表进行迭代?_Sql Server_Loops_Iterator - Fatal编程技术网

Sql server 如何在sql server中对同一id的多行表进行迭代?

Sql server 如何在sql server中对同一id的多行表进行迭代?,sql-server,loops,iterator,Sql Server,Loops,Iterator,我有一个名为城市的表和一个名为路线的表。 它们之间的关系是多对多的,因此我在它们之间使用了一个名为map的表。 路由表包含路由的名称 city表格包含城市的坐标。 地图表格包含每条路线的所有城市及其在路线中的顺序: CREATE TABLE city ( ID int IDENTITY(1,1) PRIMARY KEY not null , name varchar(50) UNIQUE, populationOfCity int not null, cent

我有一个名为
城市
的表和一个名为
路线
的表。 它们之间的关系是多对多的,因此我在它们之间使用了一个名为
map
的表。
路由
表包含路由的名称
city
表格包含城市的坐标。
地图
表格包含每条路线的所有城市及其在路线中的顺序:

CREATE TABLE city
(
   ID int IDENTITY(1,1) PRIMARY KEY not null  , 
    name varchar(50) UNIQUE,
    populationOfCity int  not null,
    center char (10) not null,
    gover_id  int null,
    cordinates geography null,
);

CREATE TABLE routes_
(
    ID int IDENTITY(1,1) PRIMARY KEY not null,
    name varchar (50) not null,
);

CREATE TABLE map
(
    ID int IDENTITY(1,1) PRIMARY KEY not null  , 
    orderOfcity int not null,
    city_id int not null,
    route_id int not null,
    FOREIGN KEY( city_id )REFERENCES city (id),
    FOREIGN KEY( route_id )REFERENCES routes_ (id)
);
我需要找到最长的路线。 我写这个算法是为了找到它,但实际上我对sql server的语法一无所知,所以我需要一些帮助来编写它

  • 我使用geography类型通过
    stdDistance
    查找两个城市之间的距离
  • 我想根据路线id对地图表进行分组,以便对每条路线的城市进行分组
  • 或城市等级
    对地图表进行排序,以计算距离
  • 在对地图表进行分组和排序后,在地图表上进行迭代,并循环查找每条路线的距离,而路线id相同
  • 在数组中保存每个根的距离,然后找到最大值
  • 我开始写这篇文章,但我不知道如何完成循环语法

     SELECT map.route_id FROM map group by map.route_id order by MIN(map.orderOfcity)  
    
      declare @i int 
      declare @numOfRows int 
      set @i=1
      set @numOfRows=(SELECT COUNT (*) from map )
      while @i<@numOfRows
    
    插入路线:

    INSERT INTO routes_(name) VALUES ('Al_Bab___Afrin');
    INSERT INTO routes_(name) VALUES ('As Safirah___Azaz');
    INSERT INTO routes_(name) VALUES ('Damascus___Latakia');
    INSERT INTO routes_(name) VALUES ('Tartous___Damascus');
    
    插入地图:

    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (1,7,1);
    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (2,9,1);
    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (3,8,1);
    
    ----As Safirah___Azaz
    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (1,9,2);
    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (2,8,2);
    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (3,10,2);
    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (4,11,2);
    
    
    
    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (1,6,4);
    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (2,4,4);
    INSERT INTO map(orderOfcity,city_id,route_id) VALUES (3,6,4);
    
    迭代必须在我按路线id分组并按城市顺序排序后在地图表上,因此我知道每条路线的城市,然后我应该计算每个组的距离,为此,我应该在地图表上迭代,并根据相同的路线id计算城市之间的距离 如果在路线中有此示例: 1) 阿尔·巴布·阿夫林 2) 作为萨菲拉·阿扎兹 在地图上,其城市包括: 阿尔·巴布、萨菲拉、阿夫林 地图中的这些城市的路线为_id=1 所以我应该计算它们之间的距离 然后移动到mab中的下一个路线id,即2,并对其城市进行相同的计算。

    您可以使用来获取给定路线中的下一个
    坐标。然后,您可以使用between
    cordinates
    和next cordinates查找路线中两个城市之间的距离。 使用CTE或派生查询按Route.ID对
    进行分组
    ,并进行
    求和(cordinate的距离)

    像这样的

    查询

    ;WITH CTE AS 
    (
    SELECT R.ID,
    cordinates.STDistance(LEAD(cordinates)OVER(PARTITION BY r.id ORDER BY orderOfcity ASC)) as Distance
    FROM city c
    INNER JOIN map m on c.id = m.city_id
    INNER JOIN routes_ r on r.id = m.route_id
    )
    SELECT TOP 1 ID,SUM(Distance)
    FROM CTE 
    GROUP BY ID
    ORDER BY SUM(Distance) DESC
    
    输出

    2   247811.559896733
    

    尝试使用映射表中连续行的连接:

    SELECT R.Name as RouteName,
            SUM(C.cordinates.STDistance(C2.cordinates)) As Distance
    FROM routes_ R
    INNER JOIN Map M
        ON M.route_id = R.Id
    INNER JOIN City C
        ON M.city_id = C.ID
    INNER JOIN Map M2
        ON M2.route_id = R.Id AND M2.orderOfcity = M.orderOfcity + 1
    INNER JOIN City C2
        ON C2.Id = M2.city_id
    GROUP BY R.Name
    
    这使得:

    RouteName          Distance
    ---------          --------------------------
    Al_Bab___Afrin     103547.016615796
    As Safirah___Azaz  247811.559896733
    Tartous___Damascus 162303.402745115
    
    要仅检索最长的路线,请使用:

    SELECT TOP 1 R.Name as RouteName,
            SUM(C.cordinates.STDistance(C2.cordinates)) As Distance
    FROM routes_ R
    INNER JOIN Map M
        ON M.route_id = R.Id
    INNER JOIN City C
        ON M.city_id = C.ID
    INNER JOIN Map M2
        ON M2.route_id = R.Id AND M2.orderOfcity = M.orderOfcity + 1
    INNER JOIN City C2
        ON C2.Id = M2.city_id
    GROUP BY R.Name
    ORDER BY SUM(C.cordinates.STDistance(C2.cordinates)) DESC
    

    发布您想要的样本输出和一些样本数据,以便我们工作with@ZoharPeled我们都要求相同的样本数据和要求output@mohan111您编写了示例输出,如果我不清楚,我猜niha也不清楚。我编辑并添加了插入的数据,这有用吗?
    SELECT TOP 1 R.Name as RouteName,
            SUM(C.cordinates.STDistance(C2.cordinates)) As Distance
    FROM routes_ R
    INNER JOIN Map M
        ON M.route_id = R.Id
    INNER JOIN City C
        ON M.city_id = C.ID
    INNER JOIN Map M2
        ON M2.route_id = R.Id AND M2.orderOfcity = M.orderOfcity + 1
    INNER JOIN City C2
        ON C2.Id = M2.city_id
    GROUP BY R.Name
    ORDER BY SUM(C.cordinates.STDistance(C2.cordinates)) DESC