Tải bản đầy đủ - 0 (trang)
Hack 46. Get All Combinations of Data

Hack 46. Get All Combinations of Data

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

CreateaSelectquerywiththetwotables,anddesignatethe

singlefieldfromeachtableforoutput.Figure5-29showsthe

querydesign.Notethatthelackofarelationlinebetweenthe

tablesisintentional.



Figure5-29.ASelectqueryofunrelatedtables



Alittletip-taponacalculatorshows96combinationsofperson

andactivity.Runningthequeryreturnsthe96records,as

showninFigure5-30.



Figure5-30.Returningthecombinedrecords



Thequeryresultscanbecopied,exported,andsoon.Thisisa

fastandeasywaytogetallcombinationsofdata.Goingone

stepfurther,athirdtableisaddedtothequery.Thisnewtable

containspartsoftheday,intworecords:morningand

afternoon.Runningthequeryreturnstheexpected192records,

whichistheproductof12x8x2.Figure5-31showstheresult.



Figure5-31.Returningcombinationsonthree

unrelatedtables



Althoughitisn'tefficienttohandledatainthisunrelatedway,at

leastwithregardtodatabasework,asetofcombinationssuch

asthismakesforusefulreports,checklists,andsoon.



Hack47.Don'tLetNullsRuinDataSummaries



Whennullsaremixedinwithvaliddata,incorrectresults

canoccur.Herearesomeguidelinestotamethebeast.

WhenyouaredealingwithvaluesinAccess,youmightbe

temptedtothinkthatablankfieldissimplyablankfield.

However,thereisadifferencebetweenablankfieldthatisfilled

inwithanemptystringandablankfieldthatisnull.For

example,whenyouarelookingatnumberfields,thereisa

differencebetweenafieldwitha0valueandafieldwithanull

value.Thishackhelpsyouworkwiththesenonvaluevalues.

Thefirstfrustratingthingaboutnullsisthatifyouwritealine

suchasthis,everylinewillshowupasNotBlank,evenifyou

havenullvalues:





IIF([Amount]=Null,"Blank","NotBlank")



ThisoccursbecauseinaBooleanexpression,anyitem

comparedtoNullreturnsFalse.

Thereisaneasywaytodealwiththis,usingafunction

availableinAccesscalledISNULL.ThisfunctionreturnsaBoolean

andallowsyoutoperformyourtest.Hereishowtorewritethe

previousexample:





IIF(ISNULL([Amount],"Blank","NotBlank")



Thatclinchesit.Now,anyencounterednullisconvertedto



Blank.



5.9.1.NullsinNumberFields

Let'sassumeyouhaveatablewithafieldcalledAmount.You

aretryingtodeterminetheaverageofthatfield(assumealso

thattheaveragedoesn'tneedtobeweighted).Ifyouwritea

querythatattemptstodeterminetheaveragevalue,theSQL

mightlooklikethis:







SELECTAvg(tbl_Amount.Amount)ASAvgOfAmount

FROMtbl_Amount;



Thisgivesyoutheaverageamountofthevaluesinthatfield.

However,ifyouhavenullsforanyofthevalues,thequerywill

ignorethem.So,ifyourvaluesare8,null,8,null,8,null,the

averageis8.Ifyourvaluesare8,0,8,0,8,0,theaverageis4.

Dependingonthepurposeofthequery,youmightwanttosee

4insteadof8.

Ifyouwanttosubstitute0fornull,youcantrytodoitwiththe

ISNULLfunctionbywritingalinesuchasthis:





IIF(ISNULL([Amount]),0,[Amount])



Thereisamucheasierway,though.TheNZfunctionavailablein

Accessrequirestwoparameters:oneforthevalueandthe

otherforthevalueifitisnull.Youcanusethisforbothnumber

andstringfunctions.HereiswhattheSQLofthequerylooks

likeusingtheNZfunction:





SELECTAvg(NZ([Amount],0))ASAverageofAmount



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

Hack 46. Get All Combinations of Data

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

×