Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
admin:indexing:activecounts:sql [2011/01/21 16:19]
els
admin:indexing:activecounts:sql [2016/06/28 22:38] (current)
Line 1: Line 1:
 +~~NOTOC~~
 {{page>:​top_add&​nofooter&​noeditbtn}} {{page>:​top_add&​nofooter&​noeditbtn}}
  
Line 21: Line 22:
 First, the user requested to see the states and their counts: First, the user requested to see the states and their counts:
  
 +<​code>​
   select ​       STATE, count(*)   select ​       STATE, count(*)
     from        LIST     from        LIST
-    group by    STATE+    group by    STATE
 +</​code>​
  
 The user selected the states in the Western United States, and then requested to see the Genders and their counts: The user selected the states in the Western United States, and then requested to see the Genders and their counts:
  
 +<​code>​
   select ​       GENDER, count(*)   select ​       GENDER, count(*)
     from        LIST     from        LIST
     where       STATE in ('​AZ','​CA','​CO','​NM','​OR','​UT','​WA'​)     where       STATE in ('​AZ','​CA','​CO','​NM','​OR','​UT','​WA'​)
-    group by    GENDER+    group by    GENDER
 +</​code>​
  
 The user selected the Women in the Western United States, and then requested to see the Educations and their counts: The user selected the Women in the Western United States, and then requested to see the Educations and their counts:
  
 +<​code>​
   select ​       EDUCATION, count(*)   select ​       EDUCATION, count(*)
     from        LIST     from        LIST
     where       STATE in ('​AZ','​CA','​CO','​NM','​OR','​UT','​WA'​) and      where       STATE in ('​AZ','​CA','​CO','​NM','​OR','​UT','​WA'​) and 
                 GENDER = '​F'​                 GENDER = '​F'​
-    group by    EDUCATION+    group by    EDUCATION
 +</​code>​
  
 The user selected those people with graduate degrees, and may have continued narrowing his search.  ​ The user selected those people with graduate degrees, and may have continued narrowing his search.  ​
Line 44: Line 51:
 As you can see, these SQL statements simple request counts that are filtered by the criteria entered so far, grouped by the next value they want to see.  The simplicity of this approach allows it to be more easily integrated into an application. As you can see, these SQL statements simple request counts that are filtered by the criteria entered so far, grouped by the next value they want to see.  The simplicity of this approach allows it to be more easily integrated into an application.
  
 +==== More Complex SQL ====
  
 +The actual SQL statements for the example on the previous page was slightly more complex, only in that it references dimension tables to provide descriptions rather than codes. ​ The actual SQL statements are as follows:
  
-First, the user requested to see the states ​and their counts:+First, the presentation of the States: 
 + 
 +<​code>​ 
 +  select ​       STATES.DESCRIPTION,​ 
 +                LIST.STATE,​ 
 +                count(*) 
 +    from        LIST 
 +    join        STATES on LIST.STATE = STATES.STATE 
 +    group by    STATES.DESCRIPTION,​ 
 +                LIST.STATE;​ 
 +</​code>​ 
 + 
 +Notice that the DESCRIPTION column was retrieved for presentation, ​and the STATE column was retrieved for inclusion in future criteria. 
 + 
 +Second, the presentation of the Genders: 
 + 
 +<​code>​ 
 +  select ​       GENDERS.DESCRIPTION,​ 
 +                LIST.GENDER,​ 
 +                count(*) 
 +    from        LIST 
 +    join        GENDERS on LIST.GENDER = GENDERS.GENDER 
 +    where       ​LIST.STATE in ('​AZ',​ '​CA',​ '​CO',​ '​NM',​ '​OR',​ '​UT',​ '​WA'​) 
 +    group by    GENDERS.DESCRIPTION,​ 
 +                LIST.GENDER;​ 
 +</​code>​ 
 + 
 +Third, the presentation of the Educations:​ 
 +  
 +<​code>​ 
 +  select ​       $ifnull(EDUCATION.DESCRIPTION,​ case LIST.EDUCATION 
 +                  when ' ' then 'Did not specify'​ 
 +                  else LIST.EDUCATION 
 +                end), 
 +                LIST.EDUCATION,​ 
 +                count(*) 
 +    from        LIST 
 +    left join   ​EDUCATION on LIST.EDUCATION = EDUCATION.EDUCATION 
 +    where       ​LIST.GENDER = '​F'​ and 
 +                LIST.STATE in ('​AZ',​ '​CA',​ '​CO',​ '​NM',​ '​OR',​ '​UT',​ '​WA'​) 
 +    group by    EDUCATION.DESCRIPTION,​ 
 +                LIST.EDUCATION 
 +    order by    LIST.EDUCATION;​ 
 +</​code>​ 
 + 
 +Notice that this SQL statement is more complex, involving $IFNULL functions and Left Outer Joins. ​ These were beneficial because not every row in the LIST table had a value in the EDUCATION column. ​ This SQL would be the standard approach for obtaining the descriptions for those entries that did have a value. ​ This example shows that the SQL can be more complex and still be used in ActiveCounts. 
 + 
 +All of these SQL statements are straightforward,​ traditional SQL.  The secret to ActiveCounts is the optimization of the SQL statements. ​ Specialized optimization that uses the Omnidex indexing allows these queries to be fast, even on very large databases. ​ Continue on to read about this optimization.
  
  
 
Back to top
admin/indexing/activecounts/sql.1295626787.txt.gz · Last modified: 2016/06/28 22:38 (external edit)