dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

Ackk...All Fileds with MIN - sqlserver-datamining

This is a discussion on Ackk...All Fileds with MIN - sqlserver-datamining ; I am trying to construct a SQL statement to return records with a minimum value in a field. I've been reading on how to use the MIN syntax but cannot figure out how to return all fields in my results. ...


Home > Database Forum > Data Warehousing > sqlserver-datamining > Ackk...All Fileds with MIN

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 10-18-2008, 09:47 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Ackk...All Fileds with MIN

I am trying to construct a SQL statement to return records with a minimum
value in a field. I've been reading on how to use the MIN syntax but cannot
figure out how to return all fields in my results. The table looks like so:

TripId RouteId RouteName RouteDist RouteDesc RouteNumStops
RouteIsMain
200 1 A1 100 A.ES.34
3 F
200 2 A4 110 A.ES.36
3 T
200 3 D5 200 A.ES.37
2 F
200 4 E7 152 A.ES.10
3 F
350 1 Z4 440 Z.ES.34
3 T
350 2 SS 425 Q.ES.11
3 F
350 3 D0 495 Q.ES.44
2 F
350 4 WW 425 S.ES.10 3
F

I know my TripId and im trying to return a record with all fields with a
minimum RouteDist so am doing something like:

SELECT TripId,RouteId,RouteName, MIN(RouteDist),
RouteDesc,RouteNumStops,RouteIsMain
FROM tblTrips
WHERE TripId=200
GROUP BY TripId

I am anticipating this for TripId=200
200 1 A1 100 A.ES.34
3 F
and this for TripId=350
350 2 SS 425 Q.ES.11
3 F
350 4 WW 425 S.ES.10 3
F

But I get an error about not including the other fields as part of the group
or an aggregate function.is there a way to get all fields as a return result
with the criteria that a filed is at a minimum value? i just cant figure out
how to do it with one statement. i can do it in two statements but it seems
there should be a more straightforward way to do it.

tia for any suggestions
AGP


Reply With Quote
  #2  
Old 10-18-2008, 10:56 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Ackk...All Fileds with MIN

Try this -- it uses a subquery in the WHERE clause:

SELECT TripId,RouteId,RouteName, RouteDist,
RouteDesc,RouteNumStops,RouteIsMain
FROM tblTrips
WHERE TripId=200 And
RouteDist =
(SELECT Min(T.RouteDist)
FROM tblTrips AS T
WHERE T.TripID = tblTrips.TripID)

--

Ken Snell



"AGP" wrote in message
news:INlKk.5207$be.5017@nlpi061.nbdc.sbc.com...
>I am trying to construct a SQL statement to return records with a minimum
>value in a field. I've been reading on how to use the MIN syntax but cannot
>figure out how to return all fields in my results. The table looks like so:
>
> TripId RouteId RouteName RouteDist RouteDesc RouteNumStops
> RouteIsMain
> 200 1 A1 100 A.ES.34 3
> F
> 200 2 A4 110 A.ES.36 3
> T
> 200 3 D5 200 A.ES.37 2
> F
> 200 4 E7 152 A.ES.10 3
> F
> 350 1 Z4 440 Z.ES.34 3
> T
> 350 2 SS 425 Q.ES.11 3
> F
> 350 3 D0 495 Q.ES.44 2
> F
> 350 4 WW 425 S.ES.10
> 3 F
>
> I know my TripId and im trying to return a record with all fields with a
> minimum RouteDist so am doing something like:
>
> SELECT TripId,RouteId,RouteName, MIN(RouteDist),
> RouteDesc,RouteNumStops,RouteIsMain
> FROM tblTrips
> WHERE TripId=200
> GROUP BY TripId
>
> I am anticipating this for TripId=200
> 200 1 A1 100 A.ES.34 3
> F
> and this for TripId=350
> 350 2 SS 425 Q.ES.11 3
> F
> 350 4 WW 425 S.ES.10
> 3 F
>
> But I get an error about not including the other fields as part of the
> group or an aggregate function.is there a way to get all fields as a
> return result with the criteria that a filed is at a minimum value? i just
> cant figure out how to do it with one statement. i can do it in two
> statements but it seems there should be a more straightforward way to do
> it.
>
> tia for any suggestions
> AGP
>



Reply With Quote
  #3  
Old 10-18-2008, 01:13 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Ackk...All Fileds with MIN

That seems to work. I think i misunderstood the MIN syntax but your subquery
makes sense.
Thanks.

AGP

"Ken Snell (MVP)" wrote in message
news:%23MfBDITMJHA.4540@TK2MSFTNGP05.phx.gbl...
> Try this -- it uses a subquery in the WHERE clause:
>
> SELECT TripId,RouteId,RouteName, RouteDist,
> RouteDesc,RouteNumStops,RouteIsMain
> FROM tblTrips
> WHERE TripId=200 And
> RouteDist =
> (SELECT Min(T.RouteDist)
> FROM tblTrips AS T
> WHERE T.TripID = tblTrips.TripID)
>
> --
>
> Ken Snell
>
>
>
> "AGP" wrote in message
> news:INlKk.5207$be.5017@nlpi061.nbdc.sbc.com...
>>I am trying to construct a SQL statement to return records with a minimum
>>value in a field. I've been reading on how to use the MIN syntax but
>>cannot figure out how to return all fields in my results. The table looks
>>like so:
>>
>> TripId RouteId RouteName RouteDist RouteDesc RouteNumStops
>> RouteIsMain
>> 200 1 A1 100 A.ES.34 3 F
>> 200 2 A4 110 A.ES.36 3 T
>> 200 3 D5 200 A.ES.37 2 F
>> 200 4 E7 152 A.ES.10 3 F
>> 350 1 Z4 440 Z.ES.34 3 T
>> 350 2 SS 425 Q.ES.11 3 F
>> 350 3 D0 495 Q.ES.44 2 F
>> 350 4 WW 425 S.ES.10 3 F
>>
>> I know my TripId and im trying to return a record with all fields with a
>> minimum RouteDist so am doing something like:
>>
>> SELECT TripId,RouteId,RouteName, MIN(RouteDist),
>> RouteDesc,RouteNumStops,RouteIsMain
>> FROM tblTrips
>> WHERE TripId=200
>> GROUP BY TripId
>>
>> I am anticipating this for TripId=200
>> 200 1 A1 100 A.ES.34 3 F
>> and this for TripId=350
>> 350 2 SS 425 Q.ES.11 3 F
>> 350 4 WW 425 S.ES.10 3 F
>>
>> But I get an error about not including the other fields as part of the
>> group or an aggregate function.is there a way to get all fields as a
>> return result with the criteria that a filed is at a minimum value? i
>> just cant figure out how to do it with one statement. i can do it in two
>> statements but it seems there should be a more straightforward way to do
>> it.
>>
>> tia for any suggestions
>> AGP
>>

>
>



Reply With Quote
  #4  
Old 10-19-2008, 11:50 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Ackk...All Fileds with MIN


"AGP" schrieb im Newsbeitrag
news:INlKk.5207$be.5017@nlpi061.nbdc.sbc.com...
>I am trying to construct a SQL statement to return records with a minimum
>value in a field. I've been reading on how to use the MIN syntax but cannot
>figure out how to return all fields in my results. The table looks like so:
>
> TripId RouteId RouteName RouteDist RouteDesc RouteNumStops
> RouteIsMain
> 200 1 A1 100 A.ES.34 3
> F
> 200 2 A4 110 A.ES.36 3
> T
> 200 3 D5 200 A.ES.37 2
> F
> 200 4 E7 152 A.ES.10 3
> F
> 350 1 Z4 440 Z.ES.34 3
> T
> 350 2 SS 425 Q.ES.11 3
> F
> 350 3 D0 495 Q.ES.44 2
> F
> 350 4 WW 425 S.ES.10
> 3 F
>
> I know my TripId and im trying to return a record with all fields with a
> minimum RouteDist so am doing something like:
>
> SELECT TripId,RouteId,RouteName, MIN(RouteDist),
> RouteDesc,RouteNumStops,RouteIsMain
> FROM tblTrips
> WHERE TripId=200
> GROUP BY TripId
>
> I am anticipating this for TripId=200
> 200 1 A1 100 A.ES.34 3
> F
> and this for TripId=350
> 350 2 SS 425 Q.ES.11 3
> F
> 350 4 WW 425 S.ES.10
> 3 F
>
> But I get an error about not including the other fields as part of the
> group or an aggregate function.is there a way to get all fields as a
> return result with the criteria that a filed is at a minimum value? i just
> cant figure out how to do it with one statement. i can do it in two
> statements but it seems there should be a more straightforward way to do
> it.
>
> tia for any suggestions
> AGP
>


Reply With Quote
  #5  
Old 11-01-2008, 07:41 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: Ackk...All Fileds with MIN

In article <93E3F4D1-3D4E-4736-A682-6B21D7840D4C@microsoft.com>, "Eduard Schuller" wrote:
>
>"AGP" schrieb im Newsbeitrag
>news:INlKk.5207$be.5017@nlpi061.nbdc.sbc.com...
>>I am trying to construct a SQL statement to return records with a minimum
>>value in a field. I've been reading on how to use the MIN syntax but cannot
>>figure out how to return all fields in my results. The table looks like so:
>>
>> TripId RouteId RouteName RouteDist RouteDesc RouteNumStops
>> RouteIsMain
>> 200 1 A1 100 A.ES.34 3
>> F
>> 200 2 A4 110 A.ES.36 3
>> T
>> 200 3 D5 200 A.ES.37 2
>> F
>> 200 4 E7 152 A.ES.10 3
>> F
>> 350 1 Z4 440 Z.ES.34 3
>> T
>> 350 2 SS 425 Q.ES.11 3
>> F
>> 350 3 D0 495 Q.ES.44 2
>> F
>> 350 4 WW 425 S.ES.10
>> 3 F
>>
>> I know my TripId and im trying to return a record with all fields with a
>> minimum RouteDist so am doing something like:
>>
>> SELECT TripId,RouteId,RouteName, MIN(RouteDist),
>> RouteDesc,RouteNumStops,RouteIsMain
>> FROM tblTrips
>> WHERE TripId=200
>> GROUP BY TripId
>>
>> I am anticipating this for TripId=200
>> 200 1 A1 100 A.ES.34 3
>> F
>> and this for TripId=350
>> 350 2 SS 425 Q.ES.11 3
>> F
>> 350 4 WW 425 S.ES.10
>> 3 F
>>
>> But I get an error about not including the other fields as part of the
>> group or an aggregate function.is there a way to get all fields as a
>> return result with the criteria that a filed is at a minimum value? i just
>> cant figure out how to do it with one statement. i can do it in two
>> statements but it seems there should be a more straightforward way to do
>> it.
>>
>> tia for any suggestions
>> AGP
>>



SELECT TripId,RouteId,RouteName, RouteDist,RouteDesc,RouteNumStops,RouteIsMain
FROM tblTrips
WHERE TripId=200
AND RouteDist = (SELECT MIN(RouteDist) FROM tblTrips WHERE TripID = 200)

Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 05:13 AM.