I have a query which calculates distances between faults on a roll of material but I only want to show the minimum distance and not all distances between each fault. Can anyone help please. My query is below
select batch, roll, Faults as Quantity,
isnull((faultstart - (select top 1 isnull(faultend,0)
from faultTbl t2
where t2.batch = t1.batch
and t2.roll = t1.roll
and t2.faultstart < t1.faultstart
order by faultstart desc)),0) as Distance
from rollTbl r1 LEFT outer Join faultTbl t1
ON r1.batch = t1.batch
and r1.roll = t1.roll
ORDER BY batch, Roll, Distance
The query produces the data below and will produce a 0 distance on the first fault as there is no other fault to calculate a distance from. This works fine if there is only 1 fault but I need to remove this for Rolls that have more than 1 fault. I also want to remove all other distances apart from the lowest number.
Batch Roll Quantity Distance
878731365 1 0 0.0
878731365 2 0 0.0
878731365 3 0 0.0
878731365 4 0 0.0
878731365 5 5 0.0
878731365 5 5 0.5
878731365 5 5 3.0
878731365 5 5 3.5
878731365 5 5 5.0
878731365 6 1 0.0
878731365 7 0 0.0
878731365 8 2 0.0
878731365 8 2 4.5
878731365 9 0 0.0
878731365 10 1 0.0
This is how I want my data to look with only the 0.5 distance for Roll 5 as this is the lowest distance.
Batch Roll Quantity Distance
878731365 1 0 0.0
878731365 2 0 0.0
878731365 3 0 0.0
878731365 4 0 0.0
878731365 5 5 0.5
878731365 6 1 0.0
878731365 7 0 0.0
878731365 8 2 4.5
878731365 9 0 0.0
878731365 10 1 0.0
Thanks in advance for you help.
select batch, roll, Faults as Quantity,
isnull((faultstart - (select top 1 isnull(faultend,0)
from faultTbl t2
where t2.batch = t1.batch
and t2.roll = t1.roll
and t2.faultstart < t1.faultstart
order by faultstart desc)),0) as Distance
from rollTbl r1 LEFT outer Join faultTbl t1
ON r1.batch = t1.batch
and r1.roll = t1.roll
ORDER BY batch, Roll, Distance
The query produces the data below and will produce a 0 distance on the first fault as there is no other fault to calculate a distance from. This works fine if there is only 1 fault but I need to remove this for Rolls that have more than 1 fault. I also want to remove all other distances apart from the lowest number.
Batch Roll Quantity Distance
878731365 1 0 0.0
878731365 2 0 0.0
878731365 3 0 0.0
878731365 4 0 0.0
878731365 5 5 0.0
878731365 5 5 0.5
878731365 5 5 3.0
878731365 5 5 3.5
878731365 5 5 5.0
878731365 6 1 0.0
878731365 7 0 0.0
878731365 8 2 0.0
878731365 8 2 4.5
878731365 9 0 0.0
878731365 10 1 0.0
This is how I want my data to look with only the 0.5 distance for Roll 5 as this is the lowest distance.
Batch Roll Quantity Distance
878731365 1 0 0.0
878731365 2 0 0.0
878731365 3 0 0.0
878731365 4 0 0.0
878731365 5 5 0.5
878731365 6 1 0.0
878731365 7 0 0.0
878731365 8 2 4.5
878731365 9 0 0.0
878731365 10 1 0.0
Thanks in advance for you help.