根据两个表中的值合并SQL中的行。

huangapple go评论65阅读模式
英文:

Merge rows in SQL based on values in 2 tables

问题

我有一张包含旅行详情的表格。详情以分布式方式保存。我需要根据出发地和目的地合并行。我的出发地是A,最终目的地是D,我需要将所有3行合并为1行,时间和距离求和。这是一个示例。

表格 #1: 旅行详情

CarID Source Destination Distance Time Date
1 A P 10 1 2022年1月1日
1 P R 20 2 2022年1月1日
1 R D 30 3 2022年1月1日
2 S A 20 1 2022年1月1日
2 A F 10 2 2022年1月1日
2 F G 30 3 2022年1月1日
2 S A 10 1 2022年1月2日

表格 #2: 旅行计划

CarID Source Destination Date
1 A D 2022年1月1日
2 S G 2022年1月1日
2 S A 2022年1月2日

所需输出:

CarID Source Destination Distance Time Date
1 A D 60 6 2022年1月1日
2 S G 60 6 2022年1月1日
2 S A 60 6 2022年1月2日
英文:

I have a table with travel details. Details are getting saved in distributed manner. I need to merge the rows based on Source and Destination. My Source is A and Final Destination is D, I need to merge all the 3 rows into 1 with sum of time and distance. Here is an example.

Table #1: trip details

CarID Source Destination Distance Time Date
1 A P 10 1 1 Jan 2022
1 P R 20 2 1 Jan 2022
1 R D 30 3 1 Jan 2022
2 S A 20 1 1 Jan 2022
2 A F 10 2 1 Jan 2022
2 F G 30 3 1 Jan 2022
2 S A 10 1 2 Jan 2022

Table #2: TravelPlan

CarID Source Destination Date
1 A D 1 Jan 2022
2 S G 1 Jan 2022
2 S A 2 Jan 2022

Output needed:

CarID Source Destination Distance Time Date
1 A D 60 6 1 Jan 2022
2 S G 60 6 1 Jan 2022
2 S A 60 6 2 Jan 2022

I tried using concatenate but not able to do based on conditions. Not sure how to combine rows of one table based on values of another. 'Between' is also not giving me desired output.

答案1

得分: 0

使用您的示例数据来构建DDL和DML(对于这种问题非常有用):

DECLARE @TripDetails TABLE (CarID INT, Source NVARCHAR(20), Destination NVARCHAR(20), Distance DECIMAL(5,2), Time DECIMAL(5,2), Date DATE)
INSERT INTO @TripDetails (CarID, Source, Destination, Distance, Time, Date) VALUES
(1, 'A', 'P', 10, 1, '1 Jan 2022'),
(1, 'P', 'R', 20, 2, '1 Jan 2022'),
(1, 'R', 'D', 30, 3, '1 Jan 2022'),
(2, 'S', 'A', 20, 1, '1 Jan 2022'),
(2, 'A', 'F', 10, 2, '1 Jan 2022'),
(2, 'F', 'G', 30, 3, '1 Jan 2022'),
(2, 'S', 'A', 10, 1, '2 Jan 2022')

DECLARE @TripPlan TABLE (CarID INT, Source NVARCHAR(20), Destination NVARCHAR(20), Date DATE)
INSERT INTO @TripPlan (CarID, Source, Destination, Date) VALUES
(1, 'A', 'D', '1 Jan 2022'),
(2, 'S', 'G', '1 Jan 2022'),
(2, 'S', 'A', '2 Jan 2022')

这随后变成了一个相当简单的JOIN和GROUP BY操作。

SELECT tp.CarID, tp.Source, tp.Destination, tp.Date, SUM(t.Distance) AS Distance, SUM(t.Time) AS Time
  FROM @TripPlan tp
    INNER JOIN @TripDetails t
	  ON tp.CarID = t.CarID
	  AND tp.Date = t.Date
 GROUP BY tp.CarID, tp.Source, tp.Destination, tp.Date
CarID	Source	Destination	Date		Distance	Time
--------------------------------------------------------		
1		A		D			2022-01-01	60.00		6.00
2		S		A			2022-01-02	10.00		1.00
2		S		G			2022-01-01	60.00		6.00

为了偏离问题一点:
我更改了Distance和Time的明显数据类型,因为我可以看到这两个值都需要表示为小数。在示例数据中没有指示这些列的单位。
在列名称中详细说明单位是个好主意,这样基本上可以自行记录。如果我们记录时间以分钟为单位,请在列名称中说明:TimeMinutes,如果我们记录距离以千米为单位,请说明:DistanceKM。

英文:

Using your example data to construct DDL and DML (which is really useful for questions like this):

DECLARE @TripDetails TABLE (CarID INT, Source NVARCHAR(20), Destination NVARCHAR(20), Distance DECIMAL(5,2), Time DECIMAL(5,2), Date DATE)
INSERT INTO @TripDetails (CarID, Source, Destination, Distance, Time, Date) VALUES
(1, 'A', 'P', 10, 1, '1 Jan 2022'),
(1, 'P', 'R', 20, 2, '1 Jan 2022'),
(1, 'R', 'D', 30, 3, '1 Jan 2022'),
(2, 'S', 'A', 20, 1, '1 Jan 2022'),
(2, 'A', 'F', 10, 2, '1 Jan 2022'),
(2, 'F', 'G', 30, 3, '1 Jan 2022'),
(2, 'S', 'A', 10, 1, '2 Jan 2022')

DECLARE @TripPlan TABLE (CarID INT, Source NVARCHAR(20), Destination NVARCHAR(20), Date DATE)
INSERT INTO @TripPlan (CarID, Source, Destination, Date) VALUES
(1, 'A', 'D', '1 Jan 2022'),
(2, 'S', 'G', '1 Jan 2022'),
(2, 'S', 'A', '2 Jan 2022')

This then becomes a fairly straight forward JOIN and GROUP BY operation.

SELECT tp.CarID, tp.Source, tp.Destination, tp.Date, SUM(t.Distance) AS Distance, SUM(t.Time) AS Time
  FROM @TripPlan tp
    INNER JOIN @TripDetails t
	  ON tp.CarID = t.CarID
	  AND tp.Date = t.Date
 GROUP BY tp.CarID, tp.Source, tp.Destination, tp.Date
CarID	Source	Destination	Date		Distance	Time
--------------------------------------------------------		
1		A		D			2022-01-01	60.00		6.00
2		S		A			2022-01-02	10.00		1.00
2		S		G			2022-01-01	60.00		6.00

To deviate from the question a little:
I changed from the obvious data types for both Distance and Time as I could see both values needing to be expressed as decimals. There is no indication in the example data as to what the units for these columns is.
Detailing the units in your column names is a good idea, it's pretty much self documenting that way. If we're recording Time in minutes, say so in the column name: TimeMinutes, if we're recording distance in kilometers: DistanceKM.

huangapple
  • 本文由 发表于 2023年1月9日 16:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定