| I am trying to select records from a database to determine qualifying order over 3 rounds of competition. I *thought* I had it working but the results are not always correct. I need to find the lowest number in the field IQUARTER from a list of runs from many different entries listed by the field RACESESSIONS, but I need only the lowest for each contestant. If there is a tie then the highest Miles Per Hour from the field I1320MPH breaks the tie, if there is an additional tie then the first one is placed above the other, this is determined by the TIMESTAMP field. The structure of the database that matters is this: timestamp (datetime) RaceSession (character) CarNumber (character) iQuarter (numeric 10,3) *Most Important* i1320MPH (numeric 10,3) the following records are an example, I CAN post an example with actual data and a small PRG file to test if necessary. (timestamp) , (racesession) , (carnumber), (iquarter) , (i1320mph) 10/22/2005 09:30, TOP FUEL Q1, 3, 4.599, 321.50 10/22/2005 09:30, TOP FUEL Q1, 1, 4.573, 324.21 10/22/2005 09:31, TOP FUEL Q1, 6, 4.509, 329.63 10/22/2005 09:31, TOP FUEL Q1, 2, 4.509, 328.55 10/22/2005 09:32, TOP FUEL Q1, 9, 4.563, 325.15 10/22/2005 09:32, TOP FUEL Q1, 8, 4.600, 309.45 10/22/2005 09:33, TOP FUEL Q1, 7, 4.598, 316.33 10/22/2005 09:33, TOP FUEL Q1, 5, 4.523, 327.54 *** 10/22/2005 2:30, TOP FUEL Q2, 3, 4.579, 321.50 10/22/2005 2:30, TOP FUEL Q2, 1, 4.543, 324.21 10/22/2005 2:31, TOP FUEL Q2, 6, 4.499, 333.63 10/22/2005 2:31, TOP FUEL Q2, 2, 4.489, 334.55 10/22/2005 2:32, TOP FUEL Q2, 9, 4.513, 325.15 10/22/2005 2:32, TOP FUEL Q2, 8, 4.500, 309.45 10/22/2005 2:33, TOP FUEL Q2, 7, 4.588, 316.33 10/22/2005 2:33, TOP FUEL Q2, 5, 4.502, 327.54 ** 10/23/2005 09:30, TOP FUEL Q3, 3, 4.759, 321.50 10/23/2005 09:30, TOP FUEL Q, 1, 4.633, 324.21 10/23/2005 09:31, TOP FUEL Q3, 6, 4.59, 329.63 10/23/2005 09:31, TOP FUEL Q3, 2, 4.579, 328.55 10/23/2005 09:32, TOP FUEL Q3, 9, 4.593, 325.15 10/23/2005 09:32, TOP FUEL Q3, 8, 4.700, 309.45 10/23/2005 09:33, TOP FUEL Q3, 7, 4.908, 316.33 10/23/2005 09:33, TOP FUEL Q3, 5, 4.623, 327.54 *** end of data example The Proper Results from the data above would be the following qualifying positions: Position * CarNumber * iQuarter #1 * 2 * 4.489 #2 * 6 * 4.499 #3 * 8 * 4.500 #1 * 5 * 4.502 #1 * 9 * 4.513 #1 * 1 * 4.543 #1 * 3 * 4.579 #1 * 7 * 4.581 What I do first is construct a select statement using an "IN" clause that works great, it selects all the cars in the first 3 sessions and the list of data appears as above with each session grouped together. This select looks like this: local lcAnswer ** construct the IN clause for the where part of the select statement For example the variable lcAnswer may look like this: lcAnswer=["TOP FUEL Q1","TOP FUEL Q2","TOP FUEL Q3"] lcAnswer="("+ (lcAnswer) + ")" SELECT * ; FROM icard ; WHERE RACESESSION IN &lcAnswer ; .AND. iquarter <> 0 ; ORDER BY iquarter, i1320mph, SESSION ; INTO CURSOR csrAnswer This cursor works fine, and displays the info as in the example perfectly. Next I need to find a way to select out the top 32 drivers in the order of the lowest iQuarter then i1320mph. I use this command currently which gives inconsistant results. SELECT TOP 32 csrAnswer.ioverunder, csrAnswer.carnumber, csrAnswer.TIMESTAMP, csrAnswer.RACESESSION, ; csrAnswer.carclass, csrAnswer.drivername, ; csrAnswer.ilane, csrAnswer.idialin, csrAnswer.ireaction, ; csrAnswer.i60foot, csrAnswer.i330foot, csrAnswer.i660foot, ; csrAnswer.i660mph, csrAnswer.i1000foot, csrAnswer.iquarter, ; csrAnswer.i1320mph, csrAnswer.imargin, csrAnswer.iwin ; FROM csrAnswer ; GROUP BY csrAnswer.carnumber ; ORDER BY csrAnswer.iquarter, csrAnswer.i1320mph ; INTO CURSOR csrQualifyingTop This works fine if there is only one session, as soon as I include the other sessions in the original select then every pass they make appears and the order is not correct. I am certain it can be done with one select statement but 2 is fine to verify the correct information. Any ideas Don Don Higgins |

Select SQL Problem
YoK
Sorry for the late reply (I was on a vacation). Is this the one you're looking for
Create Cursor sessions (sessionid c(10))
Insert into sessions values ('TOP FUEL Q1')
Insert into sessions values ('TOP FUEL Q2')
Insert into sessions values ('TOP FUEL Q3')
SELECT top 32 * ;
FROM icard t1 ;
WHERE SESSION IN (select sessionid from sessions) and ;
t1.iQuarter = ;
(select Min(iQuarter) from iCard t2 ;
where t1.Carnumber = t2.CarNumber and t2.iQuarter > 0) ;
ORDER BY IQUARTER, I1320MPH desc, timestamp ;
INTO CURSOR csrAnswer
Browse field carnumber, drivername, iquarter, i1320mph, session, timestamp
PS: This code has a problem though. If the same carnumber has more than one entry with the same lowest iQuarter all would be included (note that it's an indirect grouping). Also I'm not sure about if it is carnumber or drivername you're finding lowest iQuarter, then highest mph. For example in sample data driver Kalitta appears in carnumber 2 and 4. I accepted carnumber as significant.
Blanker
CarNumber * DriverName * iQuarter * i1320mph
1 * Schumacher * 4.516 * 325.530 Miles Per Hour
507 * Troxel * 4.521 * 328.780
91 * Dixon * 4.524 * 325.530
552 * Fuller * 4.547 * 326.080
7 * McClenathan * 4.553 * 324.440
2 * Kalita * 4.570 * 323.970
743 * Lucas * 4.573 * 321.19
71 * Beckman * 4.581 * 315.340
5 * Grubnic * 4.634 * 318.990
4 * Kalita (Scott) * 4.649 * 321.580
1301 * Foley * 4.653 * 292.650
3 * Bernstein * 4.680 * 321.120
8 * Herbert * 4.681 * 289.630
104 * Milican * 4.690 * 314.020
141 * Lagana * 4.736 * 308.280
709 * Reehl * 4.757 * 310.840
241 * Smith * 4.823 * 249.120
72 * Strasburg * 4.984 * 222.470
and so on and so on
It must be the lowest of the sessions by driver, so only one driver with his lowest iquarter will be in the final table.
Larryk01
Here is the code for the sample program I put together. After that I will post a comma delimited chuck of the database also.
PARAMETERS showcode
CLEAR
ON ERROR
CLOSE DATABASES
CLOSE ALL
IF PCOUNT() = 0
showcode = .F.
ENDIF
IF VARTYPE(showcode) <> "L"
showcode = .f.
ENDIF
IF showcode
SET STEP ON
ELSE
SET STEP OFF
ENDIF
LOCAL nSelectedCount, lcAnswer, nCnt
nSelectedCount = 3
lcAnswer = ""
USE icard
DIMENSION gettext[3]
gettext(1)="TOP FUEL Q1"
gettext(2)="TOP FUEL Q2"
gettext(3)="TOP FUEL Q3"
nCnt=0
FOR nCnt = 1 TO 3
IF nSelectedCount = 1
lcAnswer = '"'+ALLTRIM(gettext(nCnt)) +'"'
**+ IIF(nSelectedCount = 1,""," .or. session = ")
ELSE
IF EMPTY(lcAnswer)
lcAnswer ='"'+ALLTRIM(gettext(nCnt)) +'"'
ELSE
lcAnswer = lcAnswer + "," +'"'+ALLTRIM(gettext(nCnt)) +'"'
ENDIF
ENDIF
ENDFOR
lcAnswer="("+ (lcAnswer) + ")"
SELECT * ;
FROM icard ;
WHERE SESSION IN &lcAnswer ;
.AND. iquarter <> 0 ;
ORDER BY IQUARTER, I1320MPH ;
INTO CURSOR csrAnswer
**IN &lcAnswer ;
Browse field carnumber, drivername, iquarter, i1320mph, session
SELECT TOP 250 csrAnswer.ioverunder, csrAnswer.carnumber, csrAnswer.TIMESTAMP, csrAnswer.SESSION, ;
csrAnswer.carclass, csrAnswer.drivername, ;
csrAnswer.ilane, csrAnswer.idialin, csrAnswer.ireaction, ;
csrAnswer.i60foot, csrAnswer.i330foot, csrAnswer.i660foot, ;
csrAnswer.i660mph, csrAnswer.i1000foot, csrAnswer.iquarter, ;
csrAnswer.i1320mph, csrAnswer.imargin, csrAnswer.iwin ;
FROM csrAnswer ;
GROUP BY csrAnswer.carnumber ;
ORDER BY csrAnswer.iquarter ;
INTO CURSOR csrqualifyingbottom
Browse field carnumber, drivername, iquarter, i1320mph, session
RETURN
Here is the csv file contents:
runnumber,timestamp,session,carclass,carnumber,drivername,ilane,ibump,idialin,ireaction,i60foot,i330foot,i660foot,i660mph,i1000foot,i1000mph,iquarter,i1320mph,imargin,ioverunder,iwin
105,10/21/2005 13:41:19,"TOP FUEL Q1","TF","709","Reehl","L",0.000,0.00,0.128,0.894,2.249,3.222,258.620,4.044,0.000,4.757,310.840,0.000,0.000," "
107,10/21/2005 13:42:00,"TOP FUEL Q1","TF","1301","Foley","L",0.000,0.00,0.135,0.875,2.204,3.137,271.080,3.929,0.000,4.653,292.650,0.000,0.000," "
106,10/21/2005 13:42:00,"TOP FUEL Q1","TF","524","Hartley","R",0.000,0.00,0.116,0.947,2.376,3.955,133.530,5.794,0.000,7.625,116.510,0.000,0.000," "
109,10/21/2005 13:45:17,"TOP FUEL Q1","TF","72","Strasburg","L",0.000,0.00,0.126,0.896,2.234,3.207,260.560,4.062,0.000,4.984,222.470,0.000,0.000," "
108,10/21/2005 13:45:17,"TOP FUEL Q1","TF","141","Lagana Jr","R",0.000,0.00,0.148,0.891,2.274,3.772,139.930,5.631,0.000,7.787,92.820,0.000,0.000," "
111,10/21/2005 13:48:19,"TOP FUEL Q1","TF","104","Millican","L",0.000,0.00,0.081,0.869,2.324,3.804,142.260,5.623,0.000,7.714,96.110,0.000,0.000," "
110,10/21/2005 13:48:19,"TOP FUEL Q1","TF","507","Troxel","R",0.000,0.00,0.073,0.876,2.196,3.136,269.130,3.917,0.000,4.590,328.140,0.000,0.000," "
113,10/21/2005 13:51:45,"TOP FUEL Q1","TF","71","Beckman","L",0.000,0.00,0.122,2.395,7.274,10.747,61.990,10.335,0.000,21.428,33.080,0.000,0.000," "
112,10/21/2005 13:51:45,"TOP FUEL Q1","TF","13","Baca","R",0.000,0.00,0.136,0.899,2.276,3.250,258.470,4.144,0.000,5.108,214.140,0.000,0.000," "
115,10/21/2005 13:54:27,"TOP FUEL Q1","TF","241","Smith","L",0.000,0.00,0.108,0.877,2.208,3.170,261.670,3.997,0.000,4.823,249.120,0.000,0.000," "
114,10/21/2005 13:54:27,"TOP FUEL Q1","TF","10","Weis","R",0.000,0.00,0.140,0.880,2.247,3.630,153.280,5.265,0.000,7.039,115.030,0.000,0.000," "
117,10/21/2005 14:01:45,"TOP FUEL Q1","TF","552","Fuller","L",0.000,0.00,0.090,0.872,2.180,0.000,272.720,3.889,0.000,4.572,323.270,0.000,0.000," "
116,10/21/2005 14:01:45,"TOP FUEL Q1","TF","7","McClenathan","R",0.000,0.00,0.203,0.887,2.214,0.000,268.170,3.945,0.000,4.629,324.590,0.000,0.000," "
119,10/21/2005 14:05:27,"TOP FUEL Q1","TF","4","Kalitta","L",0.000,0.00,0.157,0.881,2.200,3.151,264.030,3.955,0.000,4.649,321.580,0.000,0.000," "
118,10/21/2005 14:05:27,"TOP FUEL Q1","TF","3","Bernstein","R",0.000,0.00,0.124,0.898,2.238,3.187,265.220,3.987,0.000,4.680,321.120,0.000,0.000," "
121,10/21/2005 14:08:42,"TOP FUEL Q1","TF","743","Lucas","L",0.000,0.00,0.081,0.873,2.184,3.103,273.660,3.886,0.000,4.580,317.190,0.000,0.000," "
120,10/21/2005 14:08:42,"TOP FUEL Q1","TF","8","Herbert","R",0.000,0.00,0.078,0.877,2.200,3.146,264.960,3.954,0.000,4.681,289.630,0.000,0.000," "
123,10/21/2005 14:11:49,"TOP FUEL Q1","TF","5","Grubnic","L",0.000,0.00,0.102,0.870,2.223,3.157,271.240,3.943,0.000,4.634,318.990,0.000,0.000," "
122,10/21/2005 14:11:49,"TOP FUEL Q1","TF","2","Kalitta","R",0.000,0.00,0.078,0.911,2.244,3.181,267.270,3.974,0.000,4.660,322.580,0.000,0.000," "
125,10/21/2005 14:21:40,"TOP FUEL Q1","TF","91","Dixon","L",0.000,0.00,0.056,0.868,2.181,3.117,269.510,3.907,0.000,4.593,323.660,0.000,0.000," "
124,10/21/2005 14:21:40,"TOP FUEL Q1","TF","1","Schumacher","R",0.000,0.00,0.090,0.863,2.169,3.102,269.830,3.887,0.000,4.570,324.750,0.000,0.000," "
15,10/21/2005 17:49:47,"TOP FUEL Q2","TF","765","Stutz","L",0.000,0.00,0.113,0.949,2.419,3.555,218.870,4.528,0.000,5.388,250.600,0.000,0.000," "
17,10/21/2005 17:52:51,"TOP FUEL Q2","TF","141","Lagana Jr","L",0.000,0.00,0.131,0.892,2.230,3.195,259.710,4.017,0.000,4.736,308.280,0.000,0.000," "
16,10/21/2005 17:52:51,"TOP FUEL Q2","TF","71","Beckman","R",0.000,0.00,0.089,0.864,2.182,3.101,273.830,3.884,0.000,4.581,315.340,0.000,0.000," "
19,10/21/2005 17:56:01,"TOP FUEL Q2","TF","524","Hartley","L",0.000,0.00,0.136,0.896,2.288,3.762,141.640,5.515,0.000,7.306,119.770,0.000,0.000," "
18,10/21/2005 17:56:01,"TOP FUEL Q2","TF","104","Millican","R",0.000,0.00,0.070,0.890,2.232,3.175,266.000,3.982,0.000,4.690,314.020,0.000,0.000," "
21,10/21/2005 17:59:19,"TOP FUEL Q2","TF","10","Weis","L",0.000,0.00,0.000,0.887,2.234,3.208,259.510,4.084,0.000,5.048,211.860,0.000,0.000," "
20,10/21/2005 17:59:19,"TOP FUEL Q2","TF","72","Strasburg","R",0.000,0.00,0.000,0.883,2.595,5.002,85.090,7.849,0.000,10.653,73.840,0.000,0.000," "
23,10/21/2005 18:03:00,"TOP FUEL Q2","TF","700","Chrisman","L",0.000,0.00,0.169,0.906,0.000,0.000,0.000,0.000,0.000,5.102,279.270,0.000,0.000," "
22,10/21/2005 18:03:00,"TOP FUEL Q2","TF","241","Smith","R",0.000,0.00,0.092,0.898,0.000,0.000,0.000,0.000,0.000,5.013,251.020,0.000,0.000," "
25,10/21/2005 18:07:57,"TOP FUEL Q2","TF","13","Baca","L",0.000,0.00,0.181,0.895,0.000,3.203,0.000,0.000,0.000,0.000,0.000,0.000,0.000," "
24,10/21/2005 18:07:57,"TOP FUEL Q2","TF","709","Reehl","R",0.000,0.00,0.179,0.909,0.000,3.286,0.000,0.000,0.000,0.000,0.000,0.000,0.000," "
27,10/21/2005 18:11:02,"TOP FUEL Q2","TF","8","Herbert","L",0.000,0.00,0.000,0.868,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000," "
26,10/21/2005 18:11:02,"TOP FUEL Q2","TF","1301","Foley","R",0.000,0.00,0.000,0.856,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000," "
29,10/21/2005 18:13:40,"TOP FUEL Q2","TF","3","Bernstein","L",0.000,0.00,0.000,0.875,2.193,3.118,272.610,0.000,0.000,0.000,0.000,0.000,0.000," "
28,10/21/2005 18:13:40,"TOP FUEL Q2","TF","4","Kalitta","R",0.000,0.00,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000," "
31,10/21/2005 18:31:32,"TOP FUEL Q2","TF","2","Kalitta","L",0.000,0.00,0.110,0.882,2.185,3.105,272.010,3.888,0.000,4.570,323.970,0.000,0.000," "
30,10/21/2005 18:31:32,"TOP FUEL Q2","TF","5","Grubnic","R",0.000,0.00,0.085,1.903,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.000," "
33,10/21/2005 18:34:22,"TOP FUEL Q2","TF","7","McClenathan","L",0.000,0.00,0.102,0.857,0.000,3.090,272.390,3.872,0.000,4.553,324.440,0.000,0.000," "
32,10/21/2005 18:34:22,"TOP FUEL Q2","TF","91","Dixon","R",0.000,0.00,0.101,0.856,0.000,3.061,273.160,3.842,0.000,4.524,325.530,0.000,0.000," "
35,10/21/2005 18:38:20,"TOP FUEL Q2","TF","507","Troxel","L",0.000,0.00,0.099,0.865,2.165,3.080,275.730,3.850,0.000,4.521,328.780,0.000,0.000," "
34,10/21/2005 18:38:20,"TOP FUEL Q2","TF","743","Lucas","R",0.000,0.00,0.067,0.864,2.161,3.091,270.210,3.883,0.000,4.573,321.190,0.000,0.000," "
37,10/21/2005 18:42:21,"TOP FUEL Q2","TF","1","Schumacher","L",0.000,0.00,0.096,0.850,2.134,3.055,272.610,3.835,0.000,4.516,325.530,0.000,0.000," "
36,10/21/2005 18:42:21,"TOP FUEL Q2","TF","552","Fuller","R",0.000,0.00,0.099,0.866,2.162,3.090,271.620,3.869,0.000,4.547,326.080,0.000,0.000," "
Here is the create dbf command:
CREATE table icard( runnumber N(10), ;
TIMESTAMP T(8), ;
SESSION C(40), ;
carclass C(20), ;
carnumber C(10), ;
drivername C(40), ;
ilane C(5), ;
idialin N(10,3), ;
ireaction N(10,3), ;
i60foot N(10,3), ;
i330foot N(10,3), ;
i660foot N(10,3), ;
i660mph N(10,3), ;
i1000foot N(10,3), ;
iquarter N(10,3), ;
i1320mph N(10,3), ;
ioverunder N(10,3), ;
imargin N(10,3), ;
iwin C(1))
Roman Benko.
Don,
I found it a little bit hard to understand your need (and couldn't really). For example it's obscure to me where those #1,2,3,1,1 ... (position) come from and what is the importance of timestamp column.
"I CAN post an example with actual data and a small PRG file to test if necessary."
Would you please do it along with the expected results.
You can do a select like this to find the min (max) in a group:
select * from table1 t1 where t1.iQuarter = ;
( select min(iQuarter) from table1 t2 where t1.carnumber = t2.carnumber)
Note that this is kind of grouping without a group by clause (yours for example wouldn't work under VFP9 or SQL server) selecting only the rows with the min iQuarter within each same car number. But again as I said I couldn't really understand the question.
Slowpoison
select * from iCard t1 ;
where t1.iQuarter = ;
(select min(iQuarter) from iCard t2 where t1.carnumber = t2.carnumber)
It works like:
scan iCard (t1)
-for each carnumber encountered
--select min(iQuarter) from iCard (t2)
where (t1) iCard.carnumber = iCard.carnumber (t2)
--if current (t1) iCard.iQuarter matches the found min(iQuarter)
--add it to the result
PS: I guessed you use a picklist for building IN clause and I suggest not to build that as a string but rather create a temporary cursor to join as shown in code. IN approach has limits and open to headaches. ie:
IN ("...", "...")
would fail as soon as user chooses too many sessions.
Gary Harpin
There is no other file that has information, so I am not sure what icard t1 and t2 is in your code.
Kapil Kumar
It looks like you're doing the SQL I sent (t1, t2) in multiple steps.
CccF
You should use group by for the first select.
Select racesession, iquarter, min(str(iquarter,10,3)+(1000-i1320mph)) as min_iquarter from icard ;
group by racesession ;
order by racesession,min_iquarter
I used 1000-i1320mph because you wanted it to be the maximum.
Hope this will help.
Vedat
Indraneel
This is how I solved it but I was hoping a simpler approch would be available.
LOCAL lnDriverCount, lcCarNumber
SELECT DIST carnumber ;
FROM csrAnswer ;
ORDER BY carnumber ;
INTO ARRAY carArray
lnDriverCount = _TALLY
CREATE CURSOR csrQualifying ( TIMESTAMP T(8), ;
SESSION C(40), ;
carclass C(20), ;
carnumber C(10), ;
drivername C(40), ;
iquarter N(10,3), ;
i1320mph N(10,3) )
FOR i=1 TO lnDriverCount
)
lcCarNumber = ALLTRIM(carArray
SELECT TIMESTAMP, SESSION, carclass, carnumber, ;
drivername, MIN(iquarter), MAX(i1320mph) ;
FROM csrAnswer ;
WHERE ALLTRIM(carnumber) == lcCarNumber ;
INTO ARRAY QualifyingPositionArray
SELECT csrQualifying
APPEND FROM ARRAY QualifyingPositionArray
ENDFOR
SELECT * ;
FROM csrQualifying ;
ORDER BY iquarter, i1320mph ;
INTO CURSOR csrQualifyingBottom
** NOW delete any records in dbf top so new records will be added
IF USED("TOP")
SELECT TOP
ELSE
SELECT 0
USE TOP
ENDIF
ZAP
**** now append cursor into dbf so I can see the qualifying positions
APPEND FROM DBF("csrQualifyingBottom")
**BROWSE
RETURN