返回字段中的值是或否已更改

时间:2017-03-23 14:10:44

标签: sql

对SQL很新,所以如果这很容易就道歉。 我想要返回“Y”或“N”,具体取决于列中的值是否已更改。从下表中我想要为Dave返回一个Y和为Brad更换一个N,因为Dave对他的EE_Status进行了更改,而Brad没有对EE_status进行更改。表中的任何字段可能会也可能不会更改,但是如果它发生更改,我唯一想知道的是EE_Status,并且我希望每个EE返回1行。

所以,我希望结果看起来像这样

EE_ID      EE_Name        Had Status Change  
    1       Dave          Y
    2       Brad          N

我编写的SQL,为两个EE返回Y.

Select Distinct EE_ID, EE_Name,
CASE WHEN EXISTS(SELECT * FROM EE_JOB J2 WHERE J.EE_STATUS <> J2.EE_STATUS) Then 'Y' ELSE 'N' END AS "Had Status Change" FROM JOB J

表名:EE_JOB

EE_ID   EE_Name    EE_DEPT    EE_Status  Date
1       Dave       Sales      T          02/01/2017
1       Dave       Sales      A          01/01/2017
2       Brad       Market     A          12/152016
2       Brad       Dev        A          05/05/2016

感谢您的帮助!!

克雷格

2 个答案:

答案 0 :(得分:3)

戈登回答的一个变体是检查EE_STATUS的不同数量是否大于1,这意味着状态变化至少发生一次。

SELECT EE_ID, EE_NAME,
       CASE WHEN COUNT(DISTINCT EE_Status) > 1
            THEN 'Y' ELSE 'N' END AS HadStatusChange
FROM EE_JOB
GROUP BY EE_ID, EE_NAME

答案 1 :(得分:0)

如何检查最小值和最大值?

  <HEAD>


      <script type="text/javascript">
  function findTotalcol(){
    var arr = document.getElementsByName('col1');
    var tot=0;
    for(var i=0;i<arr.length;i++){
    if(parseInt(arr[i].value))
    tot += parseInt(arr[i].value);
          }
   document.getElementById('totalcol1').value = tot;
    }
  function findTotalcol2(){
  var arr = document.getElementsByName('col2');
  var tot=0;
  for(var i=0;i<arr.length;i++){
  if(parseInt(arr[i].value))
    tot += parseInt(arr[i].value);
     }
       document.getElementById('totalcol2').value = tot;
        }

  function findTotalcol3(){
  var arr = document.getElementsByName('col3');
  var tot=0;
  for(var i=0;i<arr.length;i++){
  if(parseInt(arr[i].value))
    tot += parseInt(arr[i].value);
    }
     document.getElementById('totalcol3').value = tot;
          }
    </script>


       </HEAD>                                     
         <BODY >                 

       <TABLE align="center" width="100%" border="1" cellspacing="0">
          <TR>           <!-- 1 -->
          <TD align="center" width="25%"> 

           <input onblur="findTotalcol()" type="text" name="col1" /><br>        

          </TD>  
                 <TD align="center" width="25%"> 

           <input onblur="findTotal2()" type="text" name="col2" /><br>        

          </TD> 

                      <TD align="center" width="25%"> 

          <input onblur="findTotalcol3()" type="text" name="col3" /><br>        

          </TD>   

                   <TD align="center" width="25%"> 

          Total row:<input onblur="" type="text" name="totalrow1" /><br>        

          </TD> 

          </TR>     
                  <TR>   <!-- 2 -->
          <TD align="center" width="25%"> 

         <input onblur="findTotalcol()" type="text" name="col1" /><br>        

          </TD>  

                     <TD align="center" width="25%"> 

           <input onblur="findTotalcol2()" type="text" name="col2" /><br>        

          </TD> 

                      <TD align="center" width="25%"> 

          <input onblur="findTotalcol3()" type="text" name="col3" /><br>        

          </TD>    
                        <TD align="center" width="25%"> 

          Total row:<input onblur="" type="text" name="totalrow2" /><br>        

          </TD> 


          </TR> 

                    <TR>  <!-- 3 -->
          <TD align="center" width="30%"> 

         <input onblur="findTotalcol()" type="text" name="col1" ><br>      

          </TD>  


           <TD align="center" width="30%"> 

           <input onblur="findTotalcol2()" type="text" name="col2" /><br>        

          </TD> 

                      <TD align="center" width="30%"> 

          <input onblur="findTotalcol3()" type="text" name="col3" /><br>        

          </TD>      

                            <TD align="center" width="25%"> 

         Total row: <input onblur="" type="text" name="totalrow3" /><br>        

          </TD> 

          </TR> 
                            <TR>    <!-- total column -->
          <TD align="center" width="30%"> 

      Total: <input type="text" name="totalcol1" id="totalcol1"/>    

          </TD>  

                     <TD align="center" width="30%"> 

      Total: <input type="text" name="totalcol2" id="totalcol2"/>    

          </TD>

               <TD align="center" width="30%"> 

     Total:  <input type="text" name="totalcol3" id="totalcol3"/>    

          </TD>


          </TR> 

          </TABLE>