Guest User

Untitled

a guest
Jan 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. SELECT * FROM table WHERE category IN (
  2. SELECT category FROM (
  3. SELECT category, COUNT(*) AS c2 FROM (
  4. SELECT category, completed, COUNT(*) AS c1 FROM table
  5. GROUP BY category, completed
  6. ) AS t1
  7. GROUP BY category
  8. HAVING COUNT(*) > 1
  9. ) AS t2
  10. )
  11.  
  12. B No
  13. C Yes
  14. B No
  15. B Yes
  16. C No
  17. C Yes
  18.  
  19. public void Test()
  20. {
  21. try
  22. {
  23. var fLayer = ArcMap.Document.Maps.get_Item(0).get_Layer(0) as IFeatureLayer;
  24. var categories = Categorize((ITable)fLayer.FeatureClass, "Category", "Completed");
  25. foreach (KeyValuePair<string, List<string>> kvp in categories)
  26. {
  27. if (kvp.Value.Count > 1)
  28. Debug.Print("Category {0} has {1} unique values", kvp.Key, kvp.Value.Count);
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33. Debug.Print(ex.Message);
  34. }
  35. }
  36.  
  37. public Dictionary<string, List<string>> Categorize(ITable table, string categoryFld, string otherFld)
  38. {
  39. var outList = new Dictionary<string, List<string>>();
  40. IQueryFilter qf = new QueryFilter();
  41. qf.SubFields = String.Format("{0},{1}", categoryFld, otherFld);
  42.  
  43. int idxCat = FindField(table,categoryFld);
  44. int idxOther = FindField(table,otherFld);
  45. ICursor cur = null;
  46. // **edited to release cursor in a finally block**
  47. try
  48. {
  49. cur = table.Search(qf, true);
  50. IRow row;
  51. while ((row = cur.NextRow()) != null)
  52. {
  53. string category = GetStrVal(row, idxCat);
  54. string other = GetStrVal(row, idxOther);
  55. if (!outList.ContainsKey(category))
  56. outList.Add(category, new List<string>());
  57. if (!outList[category].Contains(other))
  58. outList[category].Add(other);
  59. }
  60. }
  61. catch
  62. {
  63. throw;
  64. }
  65. finally
  66. {
  67. System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cur);
  68. }
  69. return outList;
  70. }
  71.  
  72. public static string GetStrVal(IRow row, int idx)
  73. {
  74. return row.get_Value(idx) is DBNull ? "<Null>" : row.get_Value(idx).ToString();
  75. }
  76.  
  77. public static int FindField(ITable table, string fldName)
  78. {
  79. int idx = table.FindField(fldName);
  80. if(idx == -1)
  81. throw new Exception(String.Format("field {0} not found on table {1}",
  82. fldName,((IDataset)table).Name));
  83. return idx;
  84. }
Add Comment
Please, Sign In to add comment