queries. open the query table. 2 what am i looking for?

54
QUERIES

Upload: matthew-cannon

Post on 23-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • QUERIES
  • Slide 2
  • OPEN THE QUERY TABLE. 2 What am I looking for?
  • Slide 3
  • WHAT AM I LOOKING FOR? 1. What data element is that? muname 2. The data element resides in which table? mapunit 3
  • Slide 4
  • CREATING THE QUERY 4 Type in FROM Select Table Type in the WHERE clause Identify a comparison operator Identify the condition
  • Slide 5
  • WHAT DATA TYPE IS THE DATA ELEMENT? a variable character code 5
  • Slide 6
  • VARIABLE CHARACTER CODE?? What comparison operator will meet the conditions? 6
  • Slide 7
  • EXERCISE 1 Problem: Create a query SQL that will load the local database with the map unit name for the harney silt loam, 0 to 1 percent slopes mapunit. Send result to: [email protected] [email protected] Subject = Exercise 1 7
  • Slide 8
  • USING THE ? PARAMETER FROM mapunit WHERE muname MATCHES harney silt loam, 0 to 1 percent slopes 8
  • Slide 9
  • EXERCISE 2 Problem: Create a query SQL that will select by component name that are greater than 80 percent composition. Send result to: [email protected] [email protected] Subject = Exercise 2 9
  • Slide 10
  • EXERCISE 2 - USING >, < FROM component WHERE comppct_r >= 80 and compname matches ? 10
  • Slide 11
  • EXERCISE 3 CHOICE FIELD Problem: Create a query SQL that will allow the user to select the Component, Major Component flag, and Comp Kind by choice. Send result to: [email protected] [email protected] Subject = Exercise 3 11
  • Slide 12
  • EXERCISE 3 FROM component WHERE compname = ? AND majcompflag = ? AND compkind = ? 12
  • Slide 13
  • IN PARAMETER The IN parameter is used to identify multiple choice conditions. For example: FROM component WHERE compname = ? and majcompflag = ? and compkind IN (?) 13
  • Slide 14
  • IN PARAMETER The IN parameter is used to identify multiple string conditions. For example: FROM component WHERE compname IN (harney, farnum, albion, crete) AND mapcompflag = 1 and compkind IN (?) 14
  • Slide 15
  • IN PARAMETER The IN parameter is used to identify multiple string conditions. For example: FROM component WHERE compname IN (?) 15
  • Slide 16
  • EXERCISE 4 IN OPERATOR Problem: Create a query SQL that will allow the user to select the Component, Major Component flag, BUT, choose Comp Kind from a choice list. Send result to: [email protected] Subject = Exercise 4 16
  • Slide 17
  • EXAMPLE 5 - BETWEEN Problem: Select a component by name, by kind, with a slope between the user defined lowest RV and highest RV FROM component WHERE compkind IN (?) and (slope_r BETWEEN ? and ?) and compname matches ? 17
  • Slide 18
  • EXAMPLE 5 - BETWEEN 18
  • Slide 19
  • EXERCISE Develop a query that loads all components with the Lithic bedrock between 50 and 100 cm.
  • Slide 20
  • ADDING TABLES Additional tables can be included to the FROM clause Include all Tables so the path is continuous from top to bottom JOIN conditions can include full join condition or use by default 20
  • Slide 21
  • JOINING TABLES Joins can be INNER or OUTER 21 FROM area INNER JOIN legend by default INNER JOIN lmapunit by default INNER JOIN mapunit by default INNER JOIN correlation by default INNER JOIN datamapunit by default LEFT OUTER JOIN component by default
  • Slide 22
  • JOIN STRUCTURE New method: FROM mapunit INNER JOIN correlation by default INNER JOIN datamapunit by default;. Old method: FROM mapunit, correlation, datamapunit WHERE join mapunit to correlation and join correlation to datamapunit;. 22
  • Slide 23
  • TYPES OF JOINS INNER JOIN only matches values from both tables the most common type of join an INNER JOIN is the default join type. If an inner join is omitted from the join clause of a query, the NASIS SQL server will assume it to be an inner join allows you to join multiple tables in one query, but it requires specific condition for it to work. must ensure that the join statement has two tables with at least one common overlapping field. 23
  • Slide 24
  • TYPES OF JOINS OUTER JOINS left outer join all values from the left (table left of the word join) table and only the matching values in the right (table right of the word join) table right outer join all values from the right table and only the matching values from the left table full outer join All values from both tables regardless of matching values Fields will have null values that lack a matching row 24
  • Slide 25
  • EXERCISE 5 Using example 1, add the tables necessary to identify the survey areas where the map unit appears FROM mapunit WHERE muname MATCHES harney silt loam, 0 to 1 percent slopes Send to [email protected] 25
  • Slide 26
  • EXAMPLE 5 ANSWER FROM area INNER JOIN legend by default INNER JOIN mapunit by default WHERE muname MATCHES harney silt loam, 0 to 1 percent slopes Assumption: each area has a legend and each legend retrieved will have a Harney map unit. 26
  • Slide 27
  • OUTER JOINS Outer joins can be used to load data in which child tables are not populated. Find all sites regardless of whether the site mapunit overlap table is populated. Once you set an outer join, all subsequent joins within an AND clause are to be OUTER
  • Slide 28
  • OUTER JOIN FROM site LEFT OUTER JOIN sitemuoverlap by default WHERE usiteid matches '*KS155* FROM nasissite LEFT OUTER JOIN pedon by default LEFT OUTER JOIN phorizon by default LEFT OUTER JOIN phvnirscan by default LEFT OUTER JOIN phvnirscanrawdata by default WHERE nasissitename LIKE "MLRA%" 28
  • Slide 29
  • SPECIFIC JOINS FROM areatype INNER JOIN area by default INNER JOIN project BY nonmlra_ssa INNER JOIN projectmapunit by default INNER JOIN mapunit by default INNER JOIN correlation by default INNER JOIN data_mapunit by default WHERE areasymbol MATCHES ? "Soil Survey Area Symbol matches" AND muname MATCHES ? "Mapunit name matches" and correlation.repdmu IN (?)
  • Slide 30
  • TARGET TABLES the target table focuses the outcome of a particular query. Use this simple query to explain the process FROM datamapunit INNER JOIN component by default WHERE compname matches ? Run this as a local query First with Component as the target table Second with Datamapunit as the target table
  • Slide 31
  • TARGET TABLES FROM area INNER JOIN legend by default INNER JOIN lmapunit by default INNER JOIN mapunit by default INNER JOIN correlation by default INNER JOIN datamapunit by default INNER JOIN component by default INNER JOIN copedon by default INNER JOIN pedon by default INNER JOIN siteobs by default INNER jOIN site by default WHERE areasymbol MATCHES ? "Area Symbol matches" AND mustatus != additional and compname matches ? AND repdmu =1 AND legendsuituse = 3
  • Slide 32
  • ARITHMETIC OPERATORS Used in the WHERE clause to create a condition FROM chorizon WHERE sandtotal_r is not null and silttotal_r is not null and claytotal_r is not null and om_r < 36 and (sandtotal_r+silttotal_r+claytotal_r) NOT BETWEEN 99.995 and 100.005 32
  • Slide 33
  • EXERCISE 6 Problem: Select horizons in which the sum of the fine silt and coarse silt does not equal total silt. Send result to: [email protected] Subject = Exercise 6 33
  • Slide 34
  • EXERCISE 7 Problem: Select Kettles components by elevation that is >= 10,000 feet. Hint: 3.2808 ft. = 1 meter Send result to: [email protected] Subject = Exercise 7 34
  • Slide 35
  • EXERCISE 8 Problem: Select Martin components where their mean annual precipitation is >25 inches. Hint: the conversion is.0394. 35
  • Slide 36
  • OR CONDITION The OR condition returns a selection if either the first OR the second condition is true contrary to the AND condition where it returns a selection if the first AND the second condition are true. 36
  • Slide 37
  • USE OF OR Problem: Select Platte components with occasional or frequent flooding allowing the user to define a selected range of months. Concepts: Comparing a range or selection of values AND and OR in conditions Ordered codes in NASIS Storage of monthly data in NASIS 37
  • Slide 38
  • OR FROM component INNER JOIN comonth by default WHERE compname matches platte and (flodfreq =frequent OR flodfreq = occasional) flodfreq is a Choice field so the = is used instead of the matches 38
  • Slide 39
  • EXERCISE 9 Problem: Create a query that loads all major components and only those minor components that are hydric. 39
  • Slide 40
  • EXERCISE 9 ANSWER FROM datamapunit INNER JOIN component by default WHERE ((majcompflag=yes) OR (hydricrating = yes)) 40
  • Slide 41
  • CHOICE OPERATORS Choice ?? Choices identified in the Domains report Can use matches or = flodfreqcl = rare flodfreqcl matches 2 flodfreqcl = 2 41
  • Slide 42
  • YOU KNOW YOU ARE A TRUE KANSAN WHEN: 12. Your idea of creative landscaping is a statue of a deer next to your cedar. 13. You were unaware that there is a legal drinking age. 14. Down South to you means Oklahoma. 15. A brat is something you eat. 16. Your neighbor throws a party to celebrate his new pole shed. 42
  • Slide 43
  • NULL OPERATOR Problem: Select RaCA site records that do not have a RaCA group ID populated FROM site WHERE rcasiteid IS NOT NULL AND rcasoilgroupid IS NULL AND Concepts: IS NULL and IS NOT NULL operators 43
  • Slide 44
  • SUBQUERIES FROM area INNER JOIN legend by default INNER JOIN lmapunit by default INNER JOIN mapunit by default INNER JOIN correlation by default INNER JOIN data_mapunit by default INNER JOIN component by default INNER JOIN chorizon by default WHERE areasymbol IMATCHES ? AND mustatus = "correlated" AND repdmu = "yes" AND legendsuituse ="current wherever mapped" AND majcompflag = "yes" AND hzdepb_r IN (SELECT MAX(hzdepb_r) FROM chorizon WHERE JOIN chorizon TO component) AND EXISTS (SELECT * FROM copmgrp WHERE pmgroupname IMATCHES "* till *" AND pmgroupname NOT IMATCHES "* till over *" AND copmgrp.rvindicator = "yes" AND JOIN component TO copmgrp)
  • Slide 45
  • SUBQUERIES Subqueries are placed in the WHERE clause and used to retrieve data or set conditions on the retrieval of records ANY(SELECT * FROM copmgrp WHERE JOIN component to copmgrp and pmgroupname matches '*till*' and rvindicator = 1) hzdepb_r IN (SELECT MAX(hzdepb_r) FROM chorizon WHERE JOIN component to chorizon) comppct_r = (SELECT max(comppct_r) FROM component INNER JOIN datamapunit by default) 45
  • Slide 46
  • SUBQUERIES EXISTS (SELECT DISTINCT scoopheadspace FROM phdbscoopreading WHERE JOIN phdbscoop TO phdbscoopreading GROUP BY scoopheadspace HAVING COUNT(*) < 4) 46
  • Slide 47
  • AGGREGATION Group By Is used to build an aggregation inside the SQL statement Groups specific column(s) for aggregation FROM site INNER JOIN siteobs by default INNER JOIN pedon by default WHERE rcasiteid IS NOT NULL AND GROUP BY rcasiteid, siteiid HAVING count(upedonid) 5 47
  • Slide 48
  • EXERCISE 10 Write a query that identifies those surface layers that have more than one texture assigned as the RV 48
  • Slide 49
  • ANSWER 10 FROM datamapunit INNER JOIN component by default INNER JOIN chorizon by default INNER JOIN chtexturegrp by default WHERE chtgiid IN (SELECT chtgiidref FROM chtexture GROUP BY chtgiidref HAVING COUNT(*) > 1) and chtexturegrp.stratextsflag != "yes and hzdept_r = 0 49
  • Slide 50
  • ALIAS TABLES An alias is used to return to a table multiple times to retrieve various records An example would be to return to the area table to retrieve the MLRA overlap and the Non-MLRA survey area 50
  • Slide 51
  • FROM area_type at1, area a1, area area2, legend, lmapunit, mapunit, datamapunit, lmuaoverlap, laoverlap, correlation, area_type at2 WHERE mapunit.mapunit_status != "additional" and correlation.representative_dmu =1 and at1.area_type_name= "MLRA" and a1.area_symbol IMATCHES ? "MLRA symbol use a1 for local as target table" and legend.legendsuituse = "current wherever mapped" and at2.area_type_name = "Non-MLRA Soil Survey Area" and join at1 to a1 and join at2 to area2 and join area2 to legend and join legend to lmapunit and join lmapunit to mapunit and join a1 to laoverlap and join laoverlap to lmuaoverlap and join lmapunit to lmuaoverlap and JOIN correlation TO mapunit AND JOIN datamapunit TO correlation 51
  • Slide 52
  • USING THE FROM INNER JOIN FROM area_type at1 join area a1 by default and at1.area_type_name= "MLRA" and a1.area_symbol IMATCHES ? "MLRA symbol use a1 for local as target table" join laoverlap by default join lmuaoverlap by default join lmapunit by default join legend by default and legend.legendsuituse = "current wherever mapped" join area area2 by default join area_type at2 by default and at2.area_type_name = "Non-MLRA Soil Survey Area" join mapunit by default and mapunit.mapunit_status != "additional" join correlation by default and correlation.representative_dmu =1 join datamapunit by default 52
  • Slide 53
  • TEN BEST THINGS TO SAY IF YOU GET CAUGHT SLEEPING AT YOUR DESK: 1. "... in God's name, Amen." 2. "Someone must have put decaf in the wrong pot." 3. "The coffee machine is broken." 4. "Why did you interrupt me? I had almost figured out a solution to our biggest problem." 5. "I was doing a highly specific Yoga exercise to relieve work-related stress. Do you discriminate against people who practice Yoga?" 6. "I was testing my keyboard for drool resistance." 7. "I wasn't sleeping, I was meditating on the mission statement and envisioning a new paradigm." 8. "Whew! Guess I left the top off the white-out. You probably got here just in time." 9. "This is just a 15-minute power nap like they raved about in that time management course you sent me to." 10. "They told me at the blood bank this might happen." 53
  • Slide 54
  • QUERY IDEAS?? What queries can you dream of to build?
  • Slide 55
  • FINAL Write a query to extract those components in which the populated zone of saturation does not match your preconceived criteria for somewhat poorly drained soils. Modify the query because I want to be able to choose the drainage class and depth(s) Send me the finished sql 55
  • Slide 56
  • FINAL EXAMPLE FROM datamapunit INNER JOIN component by default INNER JOIN comonth by default INNER JOIN cosoilmoist by default WHERE drainagecl = 'somewhat poorly' and soimoiststat = 'wet' and soimoistdept_r > 0 and soimoistdept_r < 50 56