Tải bản đầy đủ - 0 (trang)
Hack 40. Return a Sample of Records

Hack 40. Return a Sample of Records

Tải bản đầy đủ - 0trang

TheToppredicateallowsyoutoisolaterecordsfromthetopofa

dataset.Ifyouwanttogetrecordsfromthebottom,firstapply

areversesort(i.e.,descendinginsteadofascending).Either

way,youwillcontinuouslygetthesamesetofrecordseach

timeyourunthequery.Laterinthishack,we'lldiscussa

methodforgettingatruerandomsample.

Figure5-1showsaqueryinDesignmodeinwhichthequery

propertysheetisusedtoindicatethenumberofrecordsto

return.TheTopValuespropertyhasafewvaluesfromwhichto

select.Youcanuseoneofthese,oryoucanenteryourown.



Figure5-1.SelectingavaluefortheToppredicate



With25astheselectedcounttoreturn,thequeryreturns,no

surprise,25recordsfromthetopofthedataset,asshownin

Figure5-2.



Figure5-2.Returningthedesignatednumberof

records



It'sinterestingtoseetheSQLtheAccessquerygridgenerates.

SwitchingtoSQLview,hereiswhatyousee:







SELECTTOP25Occurrences.Reading

FROMOccurrences;



TheToppredicatesitsjustaftertheSelectstatementand

specifiesthenumberofrecordstoreturn.

Toreturnapercentageofrecords,simplyaddthewordPercent

totheSQLstatement,afterthenumber:







SELECTTOP25PERCENTOccurrences.Reading

FROMOccurrences;



Toindicatepercentwhenusingthequerydesigner,addthe

percentsign(%)totheTopValuesproperty,asshowninFigure

5-3.



Figure5-3.Indicatingtoreturnapercentageof

records



5.2.2.HackingtheHack

TheToppredicateisgreatforgrabbingahandfulofrecords,but

itwillalwaysgrabthesamerecords.Evenwhennosortis

placedonthesourcedata,therecordsstillsitintheorderin

whichtheywereplacedinthetable.

ReturningarandomsetofrecordsrequiresusingtheRnd

function.Youapplythisasasort.Normally,asortisn'twhat

youwanttousetoreturnanunbiaseddataset,butsortingona

randomvaluemakesthisamootpoint.Tomakethiswork,alter

theSQLstatementtolooklikethis:











SELECTTOP25Occurrences.Reading

FROMOccurrences

ORDERBYRND([Reading]);



EnterthenameofthefieldastheargumentfortheRndfunction.

Eachtimethequeryruns,arandomselectionofrecordsis

returned.



Hack41.CreateBulletproofInsertOperations



Preventfailedappendoperationssothatalltherecords

makeitintothetable.

YouusetheSQLInsertstatementtoappendrecordstoatable.

Althoughthisusuallyworksgreat,itispronetoissuesthatcan

makeitbomb.Thishackshowstwothingsyoucandoto

validatedatabeforehandingitofftoanInsertoperation.Before

wediscussthesevalidationmethods,let'screateasimpletable,

asshowninFigure5-4.



Figure5-4.Atablethatacceptsnamesandages



Thetablehastwofields:



Patient

Meanttotakejustthefirstname;thelengthissetto10.



Age

Theageofthepatient.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 40. Return a Sample of Records

Tải bản đầy đủ ngay(0 tr)

×