CHSI MSSQL 101 Training Materials

download CHSI MSSQL 101 Training Materials

of 18

Transcript of CHSI MSSQL 101 Training Materials

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    1/46

    CHSI SQLAn ongoing manual to the ins and outs of T-SQL inregards to the CHSI database structure. 

    CHSI TechnologiesDatabase Worksho

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    2/46

     Table of ContentsIntroduction.....................................................................................................................................!

    Some Terms.................................................................................................................................... !

     Tool Setu....................................................................................................................................... "

    Queries............................................................................................................................................#

    S$L$CT......................................................................................................................................... # T%&........................................................................................................................................... '

    Constant Columns.....................................................................................................................'

    (ILT$)I*+ ,the WH$)$ clause.................................................................................................... '

    %ther Comarison %erators.................................................................................................... '

    Combining comarisons...........................................................................................................

    &erformance.............................................................................................................................

    LI/$..............................................................................................................................................

    Wildcards.................................................................................................................................. 0

    Calculated (ields.......................................................................................................................... 0

    Concatenated (ield................................................................................................................... 0

    C%*CAT....................................................................................................................................... 1

    2ath......................................................................................................................................... 1

    Aggregate (unctions ,Counting3 summari4ing3 etc..................................................................... 1

    Counting...................................................................................................................................... 1

    Summing......................................................................................................................................1

    2a53 2in3 A6+............................................................................................................................ 78

    +rouing.................................................................................................................................... 78

    CAS$............................................................................................................................................. 77

    Combining Tables.......................................................................................................................... 79

     :%I*S.......................................................................................................................................... 79

    Sub Queries............................................................................................................................... 7!

    DISTI*CT....................................................................................................................................... 7"

    ;*I%*........................................................................................................................................... 7#

    Con

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    3/46

     Table 6ariable ;sage............................................................................................................... 98

    2$)+$ outut......................................................................................................................... 98

    D$L$T$...................................................................................................................................... 97

    %)D$) >?......................................................................................................................................9!

    ;*I%* %rdering......................................................................................................................... 9"

    ;sing (unctions.......................................................................................................................... 9#

    Sorting Te5t as *umbers............................................................................................................9'

    ;sing Stored &rocedures............................................................................................................... 9'

    $5licitl= Secif= &arameters..................................................................................................... 9

    Quer=ing against a stored rocedure.........................................................................................90

    Correlated Sub Queries................................................................................................................. 90

    Al=.......................................................................................................................................... 91

    C)%SS A&&L?.......................................................................................................................... 91

    %;T$) A&&L?.......................................................................................................................... !8

    Summari4ing Data........................................................................................................................ !8WITH C;>$.................................................................................................................................!8

    WITH )%LL;&............................................................................................................................. !7

    *umbering )o@s........................................................................................................................... !9

    )%W*;2>$)........................................................................................................................... !9

    )A*/,....................................................................................................................................... !!

    CT$................................................................................................................................................!'

    )ecursi

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    4/46

    Introduction This guide is a general guide to using T-SQL3 emhasi4ing areas that are alicable @ith @orkingagainst CHSI SQL Ser

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    5/46

    An alias renames an obect in a uer=3 either to make it easier to refer to something some@hereelse3 to control outut3 or to allo@ for in

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    6/46

    • Click %/ 

    QueriesS$L$CT The foundation of most userEs interaction @ith SQL. S$L$CT allo@s =ou to secif= the columns=ou @ant to form the basis of a resultset.

    At its simlest3 =ou siml= secif= S$L$CT Column73 Column93 N ()%2 Table73 something like

     

     This uer= @ill retrie

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    7/46

    $

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    8/46

    Notice

    •  The Clients is missing its schema name3 so SQL ser

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    9/46

     

    &erformance• 2ore columns slo@ do@n the uer=.

    • 2ore comlicated WH$)$ Glters also slo@ do@n the uer=.

    • )estraining WH$)$ Glters to onl= inde5ed columns can drasticall= imro

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    10/46

    Concatenated (ield

    C%*CAT

    2ath

    Aggregate (unctions ,Counting3 summari4ing3 etc

    Counting Three otions for counting.

    • C%;*T,F returns a count of all ro@s from the resultset.

    C%;*T ,Column*ame returns a count of all non-null

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    11/46

    Summing6er= simle3 siml= use S;2, around the column =ou @ant to sum. The function accets distinctto onl= sum distinct

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    12/46

    +rouing;suall= =ou @onEt @ant to do a simle aggregate function3 =ouEll @ant to get results for eacharent record or b= something arbitrar= like month. Siml= add +)%;& >? for e

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    13/46

    CAS$2an= times3 =ou @ill @ant to select diJerent

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    14/46

    Combining TablesA roerl= built database @ill store data in multile tables3 either for erformance or for theurose of not dulicating data. ?ou @ill almost al@a=s @ant to combine these multile tablestogether to generate a Kcomlete resultset.

     ?ou ha

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    15/46

    An e5amle of an inner oin3 matching e

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    16/46

    Here @e generate a sum of all credits and debits3 in some cases3 the erformance canouterform grouing the arent uer=.

    DISTI*CT%ften3 @hen oining3 =ouEll end u @ith dulicate ro@s. In those cases3 =ou can use S$L$CTDISTI*CT to remo

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    17/46

    Here is a legitimate use for DISTI*CT.

    In this uer=3 @e get a list of all olic=eriods for each client from the Accts)ec

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    18/46

    Here @e make sure @e get all clients @ith all of their olic=eriods from both the Accts)ec

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    19/46

    Con

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    20/46

    source

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    21/46

    Here @e insert a contact3 resumabl= from some batch rocess3 but @e insert the currenttimeBdate using the getdate, function3 then @e select the inserted contact to see the ne@ id andtimestam using the scoeidentit=, function.

    ;&DAT$When =ou need to change a

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    22/46

    Here @e make sure the grousec is inserted if it does not e5ist3 or udate it if it does.

    Inserting Cascading )ecordsIn some cases3 =ouEll @ant to insert multile records along @ith a child

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    23/46

    Here @e retend @e ha

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    24/46

     This is a fairl= comlicated uer=. We insert an= contacts that donEt alread= e5ist ,@ith anassumtion that emails should be uniue into the contacts table3 grabbing their ne@ contactIDsand inserting them into a tem table. We then merge the tem table on the original table3 andinsert that into the clientcontacts table.

     ?ou could do the same thing for deletes3 deleting all of the child records for a gi

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    25/46

    Here @e delete the test contact @e inserted in the rior e5amle. Careful though3 I left strandedclientcontactsR

    Abo

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    26/46

    %)D$) >?

    If =ou do not tell the database ho@ to order the resultset3 it @ill decide for =ou ho@ the resultsetshould be ordered. If the data is for end user consumtion3 =ouEll usuall= @ant to order the dataintentionall=. To do this3 =ou siml= end =our uer= @ith %)D$) >?3 then a list of columns =ou@ant the resultset ordered b=. >= default3 the data @ill be sorted ascending3 but =ou cane5licitl= deGne the direction of the sort b= aending ASC or D$SC to the column. (or e5amle3%)D$) >? Column7 ASC3 Column9 D$SC. ?ou can refer to columns that e5ist in the ()%23 or b=the aliased columns referenced in the S$L$CT.

    In the e5amle belo@3 @e can see that @hen selecting all records3 the itemDates are inseemingl= random order.

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    27/46

    Here3 @e take the same data3 but sort it b= ClientID3 then b= ItemDate.

    ;*I%* %rderingWhen =ou %)D$) a ;*I%*3 %)D$) >? @orks a bit diJerentl=. ?ou can onl= %)D$) using thecolumns in the resultset. The e5amle belo@ sho@s ordering b= a calculated column

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    28/46

    ,&olic=&eriod3 also note that the second resultset does not secif= the column name for @hat isrendered as the &olic=&eriod in the Grst resultset.

    ;sing (unctionsSorting is generall= incredibl= simle3 but ust as =ou can use functions e

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    29/46

    Aboelo@ @e see the roblem.

    And here @e G5 it. We use a CAS$ statement to make sure the column is numeric3 then if so3 @econ

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    30/46

    ;sing Stored &roceduresA stored rocedure is an entire set of e5ecutable SQL. To control ho@ the SQL functions3arameters are deGned @hen the stored rocedure is created3 its u to the @riter of the storedrocedure to deGne @hat haens based on the arameter.

     ?ou can e5ecute a stored rocedure b= assing the arameter in the order it @as seciGed. (ore5amle3 lets look at a stored rocedure3 Claims.ClaimClass+et

    $5anding the arameters3 @e can see it e5ects a string named YCo

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    31/46

     The abo

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    32/46

    In the abo

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    33/46

    e5amles. The other reason =ou donEt usuall= @ant a subuer= in the S$L$CT3 is that =ou canonl= retrie

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    34/46

    Summari4ing Data+enerating some numbers for a reort is eas= @ith $5cel or Cr=stal. >ut sometimes =ou @ant todo it ust $When =ou C;>$ the data3 =ou ask SQL to gi$ to the uer=3 @e see ho@ man= scheduleitems at each client and location3 ho@ man= schedule items for each client3 and ho@ man=schedule items for each location3 as @ell as ho@ man= schedule items total.

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    35/46

    WITH )%LL;&If =ou care more about the grand totals3 =ou can use WITH )%LL;&. >elo@3 @e can see ho@man= schedules e5ist at each client3 at each location3 and ho@ man= total.

     This is all @ell and good3 but @e can do better3 making it a bit easier to read the data b= using+)%;&I*+. +)%;&I*+ tells =ou @hat time of ro@ a gi

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    36/46

     The imortant bits here3 is that =ou need to check both columns to see @hether itEs a +rand Total3 or @hether its ust a subtotal line.

    *umbering )o@s)%W*;2>$)Sometimes3 =ou @ant to generate a ne@ number for each ro@ based on certain criteria.)%W*;2>$), @ill generate a ne@ number starting o

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    37/46

    )A*/,In the reSI+3 there are a lot of ties3 =et theK&a=ment%rder column continues to count u. If @e @ere tr=ing to re@ard clients based ontheir totalaid3 it @ouldnEt be

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    38/46

    Abo

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    39/46

     ?ou ma= @ant to kno@ @ho the \9 rank is3 \!3 etc3 regardless of ho@ man= clients might tie.D$*S$)A*/, erforms this function.

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    40/46

    CT$At its most basic3 a CT$ ,Common Table $5ression is siml= a resultset created b= a re

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    41/46

    Deduing ;sing CT$s%ccasionall=3 =ou ma= Gnd that =ou ha

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    42/46

    (or that3 a CT$ can be a simle solution to the roblem. When =ou interact @ith a CT$3 it actuall=aJects the base tables. We combine the CT$ @ith the )%W*;2>$) function to generateuniue numbers er ro@3 then @e delete e

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    43/46

    In the abo

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    44/46

    man= records are returned3 but =ou canEt ass in a arameter to a uer= as the T%& number ofro@s. >elo@3 @e construct the uer= d=namicall=.

    In the abo

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    45/46

    We create a arameter called YD=namicQuer=3 ust like before3 but instead of @riting a comleteuer=3 @e @rite onl= art of the uer=3 stoing @here @e @ant the d=namic columns to begenerated.

     Then @e S$L$CT the d=namic ortion. A S$L$CT statement is rendered ro@ b= ro@3 not all atonce3 so @e can take ad

  • 8/20/2019 CHSI MSSQL 101 Training Materials

    46/46

    >= S$L$CTing =our uer=3 =ou can diagnose @hat @ent @rong3 or hel