我有以下查询:
SELECT
InspectionID, Distance, Continuous, structural_grade
FROM
Conditions
WHERE
Continuous LIKE ("S%") OR Continuous LIKE ("F%")
ORDER BY
InspectionId ASC, Distance ASC
哪个输出:
262 60.80 S01 3
262 73.10 F01 3
262 82.60 S02 2
262 140.30 F02 2
263 30.80 S01 2
263 46.60 F01 2
380 4.60 S01 3
380 8.50 F01 3
380 9.80 S02 4
380 28.20 F02 4
380 77.70 S03 4
380 97.70 F03 4
订购是正确的。但是,我仍然坚持下一步该做什么。
每个" S"有一个匹配的" F"使用相同的编号Ex S01,F01。我需要访问每个" F"的距离字段。和减去" S"与它的距离。
例如,F01的距离为73.10,S01为60.80,因此距离为12.3。我需要为每个记录执行此操作,并在" S"之后编号。和" F"可以真的很高但总会匹配。恩。 S999 - F999
答案 0 :(得分:1)
使用Mid()
功能从Continuous
字段中提取数字:Mid("S01", 2)
产生“01”。然后在加入 F 和 S 行时使用这些数字。
使用Access 2010中的示例数据,以下查询为我提供了此结果集:
SELECT
f.InspectionID,
f.Continuous,
s.Continuous,
f.Distance AS F_distance,
s.Distance AS S_distance,
f.Distance - s.Distance AS F_minus_S
FROM
Conditions AS f
INNER JOIN
(
SELECT
InspectionID,
Continuous,
Distance,
Mid([Continuous],2) AS digits_only
FROM Conditions
WHERE Continuous ALike 'S%'
) AS s
ON
f.InspectionID = s.InspectionID
AND Mid(f.[Continuous],2) = s.digits_only
WHERE f.Continuous ALike 'F%'
ORDER BY f.InspectionId ASC, f.Distance ASC;
我在最终查询中包含了几个我怀疑你想要的列。但我认为它们在开发和测试过程中非常有用。
请注意Access查询设计器会抱怨它无法在设计视图中表示该连接条件。如果在 SQL View 中设置连接,则可以在没有Access抱怨的情况下运行查询。
答案 1 :(得分:0)
SELECT a.continuous AS 'S Continuous',
a.distance AS 'S Distance',
b.continuous AS 'F Continuous',
b.distance AS 'F Distance',
abs(a.distance - b.distance) AS 'Difference'
FROM Conditions a
INNER JOIN Conditions b
ON a.InspectionID = b.InspectionID
AND substring(a.continuous,2,len(a.continuous)-1) = substring(b.continuous,2,len(b.continuous)-1)
WHERE a.Continuous LIKE 'S%'
AND b.Continuous LIKE 'F%'
ORDER BY a.InspectionId ASC, a.Distance ASC