patrussell
Technical User
Using this query I am very close to getting the results I want but cannot find where I am going wrong. I will post the initial query with results and then explain what I am looking for.
gives this result
CoilID Min Tower Hgt Grade Gauge Width
--------------- ------------- ----------- ----------- -----------
1035n743 49 304 62 39
1045n033 38 304 46 49
1045n034 34 304 46 49
10514c158 34 430 73 49
10534c191 32 430 73 49
10554n036a 49 304 58 48
1065n516 61 304 57 38
12034n084 48 304 46 49
12064n441 50 305 49 49
12114n784 63 201 48 31
12114n785 66 201 48 31
12144n280 73 201 48 31
12144n296 71 201 48 31
12174n347 35 304 34 49
12204n894 54 304 46 37
12224n198 36 304 57 49
12234n502 63 201 48 31
12524c504 48 430 73 49
8424n114 46 301 90 49
8424n115 46 301 90 49
This is great but I need two other columns to get my desired data. I need to add a [Time Stamp] and [Footage] column but each time I try to add one or the other I no longer have one row per CoilID.
This is the query as I am trying it
This give the following results (snipped for space)
CoilID Min Tower Hgt Grade Gauge Width Footage Time Stamp
--------------- ------------- ----------- ----------- ----------- ----------- ------------------------------------------------------
1035n743 96 304 62 39 1 2005-01-18 04:17:22.687
1035n743 96 304 62 39 11 2005-01-18 04:17:39.780
1035n743 96 304 62 39 21 2005-01-18 04:17:56.920
1035n743 96 304 62 39 31 2005-01-18 04:18:17.297
1035n743 96 304 62 39 42 2005-01-18 04:18:37.203
1035n743 96 304 62 39 52 2005-01-18 04:19:00.797
1035n743 95 304 62 39 62 2005-01-18 04:19:21.217
1035n743 96 304 62 39 73 2005-01-18 04:19:45.077
1035n743 96 304 62 39 83 2005-01-18 04:20:05.420
1035n743 96 304 62 39 93 2005-01-18 04:20:29.407
1035n743 96 304 62 39 103 2005-01-18 04:20:49.890
.
.
.
I only need the [Time Stamp] and [Footage] for the minimum tower height, not every record. If there are multiple records at the same minimum the earliest timestamp should decide which to use.
Thanks for any help you can give...
Pat Russell
Code:
SELECT b.CoilID, MIN(a.[Entry Tower]) as [Min Tower Hgt], AVG(a.Grade) as [Grade],
AVG(a.Gauge) as [Gauge],AVG(a.Width) as [Width]
FROM dbo.BA_ALL_COILS a LEFT OUTER JOIN
dbo.Coil_List b ON a.CoilID = b.CoilID
Where a.[Entry Tower] is not null
Group BY b.CoilID
Order By b.CoilID
gives this result
CoilID Min Tower Hgt Grade Gauge Width
--------------- ------------- ----------- ----------- -----------
1035n743 49 304 62 39
1045n033 38 304 46 49
1045n034 34 304 46 49
10514c158 34 430 73 49
10534c191 32 430 73 49
10554n036a 49 304 58 48
1065n516 61 304 57 38
12034n084 48 304 46 49
12064n441 50 305 49 49
12114n784 63 201 48 31
12114n785 66 201 48 31
12144n280 73 201 48 31
12144n296 71 201 48 31
12174n347 35 304 34 49
12204n894 54 304 46 37
12224n198 36 304 57 49
12234n502 63 201 48 31
12524c504 48 430 73 49
8424n114 46 301 90 49
8424n115 46 301 90 49
This is great but I need two other columns to get my desired data. I need to add a [Time Stamp] and [Footage] column but each time I try to add one or the other I no longer have one row per CoilID.
This is the query as I am trying it
Code:
SELECT b.CoilID, MIN(a.[Entry Tower]) AS [Min Tower Hgt], AVG(a.Grade) AS Grade,
AVG(a.Gauge) AS Gauge, AVG(a.Width) AS Width, a.Footage, a.[Time Stamp]
FROM dbo.BA_ALL_COILS a LEFT OUTER JOIN
dbo.Coil_List b ON a.CoilID = b.CoilID
WHERE (a.[Entry Tower] IS NOT NULL)
GROUP BY b.CoilID, a.Footage, a.[Time Stamp]
ORDER BY b.CoilID
This give the following results (snipped for space)
CoilID Min Tower Hgt Grade Gauge Width Footage Time Stamp
--------------- ------------- ----------- ----------- ----------- ----------- ------------------------------------------------------
1035n743 96 304 62 39 1 2005-01-18 04:17:22.687
1035n743 96 304 62 39 11 2005-01-18 04:17:39.780
1035n743 96 304 62 39 21 2005-01-18 04:17:56.920
1035n743 96 304 62 39 31 2005-01-18 04:18:17.297
1035n743 96 304 62 39 42 2005-01-18 04:18:37.203
1035n743 96 304 62 39 52 2005-01-18 04:19:00.797
1035n743 95 304 62 39 62 2005-01-18 04:19:21.217
1035n743 96 304 62 39 73 2005-01-18 04:19:45.077
1035n743 96 304 62 39 83 2005-01-18 04:20:05.420
1035n743 96 304 62 39 93 2005-01-18 04:20:29.407
1035n743 96 304 62 39 103 2005-01-18 04:20:49.890
.
.
.
I only need the [Time Stamp] and [Footage] for the minimum tower height, not every record. If there are multiple records at the same minimum the earliest timestamp should decide which to use.
Thanks for any help you can give...
Pat Russell