Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 5.63 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. SQL calculate item frequency using multiple / dependent columns?
  2. state     fruit     popularity
  3.  
  4. hawaii    apple    
  5. hawaii    apple    
  6. hawaii    banana      
  7. hawaii    kiwi      
  8. hawaii    kiwi      
  9. hawaii    mango        
  10. florida   apple      
  11. florida   apple        
  12. florida   apple        
  13. florida   orange      
  14. michigan  apple    
  15. michigan  apple    
  16. michigan  apricot  
  17. michigan  orange    
  18. michigan  pear      
  19. michigan  pear      
  20. michigan  pear      
  21. texas     apple    
  22. texas     banana    
  23. texas     banana    
  24. texas     banana    
  25. texas     grape
  26.        
  27. state     fruit     popularity
  28.  
  29. hawaii    apple     4
  30. hawaii    apple     4
  31. hawaii    banana    2  
  32. hawaii    kiwi      1
  33. hawaii    kiwi      1
  34. hawaii    mango     1  
  35. florida   apple     4
  36. florida   apple     4  
  37. florida   apple     4  
  38. florida   orange    2  
  39. michigan  apple     4
  40. michigan  apple     4
  41. michigan  apricot   1
  42. michigan  orange    2
  43. michigan  pear      1
  44. michigan  pear      1
  45. michigan  pear      1
  46. texas     apple     4
  47. texas     banana    2
  48. texas     banana    2
  49. texas     banana    2
  50. texas     grape     1
  51.        
  52. --outputs those fruits appearing multiple times in the table
  53. SELECT fruit, COUNT(*)
  54.   FROM table
  55.  GROUP BY fruit
  56. HAVING COUNT(*) > 1
  57.  ORDER BY COUNT(*) DESC
  58.  
  59. --outputs those fruits appearing only once in the table
  60. SELECT fruit, COUNT(*)
  61.   FROM table
  62.  GROUP BY fruit
  63. HAVING COUNT(*) = 1
  64.  
  65. --outputs list of unique fruits in the table
  66. SELECT COUNT (DISTINCT(fruit))
  67.   FROM table
  68.        
  69. update my_table x
  70.    set popularity = ( select count(distinct state)
  71.                         from my_table
  72.                        where fruit = x.fruit )
  73.        
  74. select state, fruit
  75.      , count(distinct state) over ( partition by fruit ) as popularity
  76.   from my_table
  77.        
  78. select fruit, COUNT(distinct state) statecount from #fruit group by fruit
  79.        
  80. update #fruit
  81. set popularity
  82.     = statecount
  83. from
  84.  #fruit
  85.     inner join
  86.       (select fruit, COUNT(distinct state) statecount from #fruit group by fruit) sc
  87.         on #fruit.fruit = sc.fruit
  88.        
  89. update table
  90. set count = cnt
  91. from
  92.   (
  93.     select fruit, count(distinct state) as cnt
  94.     from table
  95.     group by fruit) cnts
  96.   inner join table t
  97.     on cnts.fruit = t.fruit
  98.        
  99. SELECT fruit
  100. ,      COUNT(*)
  101. FROM
  102. (
  103. SELECT state
  104. ,      fruit
  105. ,      ROW_NUMBER() OVER (PARTITION BY state, fruit ORDER BY NULL) rn
  106. FROM   t
  107. )
  108. WHERE rn = 1
  109. GROUP BY fruit
  110. ORDER BY fruit;
  111.        
  112. select a.*,b.total
  113. from [table] as a
  114. left join
  115. (
  116. SELECT fruit,count(distinct [state]) as total
  117.   FROM [table]
  118.   group by fruit
  119. ) as b
  120. on a.fruit = b.fruit
  121.        
  122. WITH t
  123.   AS (SELECT 'hawaii' as STATE, 'apple' as fruit FROM dual
  124.       UNION ALL
  125.       SELECT 'hawaii' as STATE, 'apple' as fruit FROM dual
  126.       UNION ALL
  127.       SELECT 'hawaii' as STATE, 'banana' as fruit FROM dual
  128.       UNION ALL
  129.       SELECT 'hawaii' as STATE, 'kiwi' as fruit FROM dual
  130.       UNION ALL
  131.       SELECT 'hawaii' as STATE, 'kiwi' as fruit FROM dual
  132.       UNION ALL
  133.       SELECT 'hawaii' as STATE, 'mango' as fruit FROM dual
  134.       UNION ALL
  135.       SELECT 'florida' as STATE, 'apple' as fruit FROM dual
  136.       UNION ALL
  137.       SELECT 'florida' as STATE, 'apple' as fruit FROM dual
  138.       UNION ALL
  139.       SELECT 'florida' as STATE, 'apple' as fruit FROM dual
  140.       UNION ALL
  141.       SELECT 'florida' as STATE, 'orange' as fruit FROM dual
  142.       UNION ALL
  143.       SELECT 'michigan' as STATE, 'apple' as fruit FROM dual
  144.       UNION ALL
  145.       SELECT 'michigan' as STATE, 'apple' as fruit FROM dual
  146.       UNION ALL
  147.       SELECT 'michigan' as STATE, 'apricot' as fruit FROM dual
  148.       UNION ALL
  149.       SELECT 'michigan' as STATE, 'orange' as fruit FROM dual
  150.       UNION ALL
  151.       SELECT 'michigan' as STATE, 'pear' as fruit FROM dual
  152.       UNION ALL
  153.       SELECT 'michigan' as STATE, 'pear' as fruit FROM dual
  154.       UNION ALL
  155.       SELECT 'michigan' as STATE, 'pear' as fruit FROM dual
  156.       UNION ALL
  157.       SELECT 'texas' as STATE, 'apple' as fruit FROM dual
  158.       UNION ALL
  159.       SELECT 'texas' as STATE, 'banana' as fruit FROM dual
  160.       UNION ALL
  161.       SELECT 'texas' as STATE, 'banana' as fruit FROM dual
  162.       UNION ALL
  163.       SELECT 'texas' as STATE, 'banana' as fruit FROM dual
  164.       UNION ALL
  165.       SELECT 'texas' as STATE, 'grape' as fruit FROM dual)
  166. SELECT state,
  167.        fruit,
  168.        count(DISTINCT state) OVER (PARTITION BY fruit) AS popularity
  169.   FROM t;
  170.        
  171. florida     apple   4
  172. florida     apple   4
  173. florida     apple   4
  174. hawaii      apple   4
  175. hawaii      apple   4
  176. michigan    apple   4
  177. michigan    apple   4
  178. texas       apple   4
  179. michigan    apricot 1
  180. hawaii      banana  2
  181. texas       banana  2
  182. texas       banana  2
  183. texas       banana  2
  184. texas       grape   1
  185. hawaii      kiwi    1
  186. hawaii      kiwi    1
  187. hawaii      mango   1
  188. florida     orange  2
  189. michigan    orange  2
  190. michigan    pear    1
  191. michigan    pear    1
  192.        
  193. SELECT state,
  194.        fruit,
  195.        count(DISTINCT state) OVER (PARTITION BY fruit) AS popularity
  196.   FROM table_name;
  197.        
  198. create table states([state] varchar(10),fruit varchar(10),popularity int)
  199. INSERT INTO states([state],fruit)
  200. VALUES('hawaii','apple'),
  201. ('hawaii','apple'),    
  202. ('hawaii','banana'),      
  203. ('hawaii','kiwi'),      
  204. ('hawaii','kiwi'),      
  205. ('hawaii','mango'),        
  206. ('florida','apple'),      
  207. ('florida','apple'),        
  208. ('florida','apple'),        
  209. ('florida','orange'),      
  210. ('michigan','apple'),    
  211. ('michigan','apple'),    
  212. ('michigan','apricot'),  
  213. ('michigan','orange'),    
  214. ('michigan','pear'),      
  215. ('michigan','pear'),      
  216. ('michigan','pear'),      
  217. ('texas','apple'),    
  218. ('texas','banana'),    
  219. ('texas','banana'),    
  220. ('texas','banana'),
  221. ('texas','grape')
  222.  
  223. update t set t.popularity=a.cnt
  224. from states t inner join
  225. (SELECT fruit,count(distinct [state]) as cnt
  226.   FROM states
  227.   group by fruit) a
  228. on t.fruit =a.fruit