title: SQL query based on Group by, need derived column based on min and max
body: <pre><code>Create table test123 (
      CustId int,
      [Level]  int,
      RowNum int,
      USAFlag bit
  )
  
  insert into test123(CustId,[Level],RowNum,USAFlag)values
  (101,1,1,0),
  (102,2,1,0),
  (102,2,2,1),
  (103,3,1,0),
  (103,3,2,1),
  (103,3,3,0),
  (104,4,1,1),
  (104,4,2,0),
  (104,4,3,0),
  (104,4,4,1),
  (105,2,1,1),
  (105,2,2,0),
  (106,2,1,0),
  (106,2,2,0),
  (107,3,1,0),
  (107,3,2,0),
  (107,3,3,1),
  (108,1,1,1)
  </code></pre>
  
  <p>OUtput</p>
  
  <pre><code>CustID USARootLeaf
  101 ONlyONeLevel_NonUSA
  102 Leaf_USA
  103 Root_Leaf_NonUSA
  104 Root_Leaf_USA
  105 Root_USA
  106 Root_Leaf_NonUSA
  107 Leaf_USA
  108 OnlyOneLvel_USA
  </code></pre>
  
  <p>Logic:
  If Level is 1 then USARootLeaf value should be OnlyOneLvel_USA or OnlyOneLvel_NonUSA based on USAFlag value</p>
  
  <p>If Level is >1, then  USARootLeaf value should be Root_Leaf_USA,Root_Leaf_NonUSA,Root_USA,Leaf_USA based on min(level) and max(level) value of USAFlag is true/false</p>