如何从表中将每两行放入单行

时间:2017-03-14 09:48:13

标签: sql sql-server

如何将每2行显示为单行

var banking = angular.module('banking', []);
banking.controller('accountCtrl', ['$scope', '$http',
  function($scope, $http) {
    $scope.getClient = function() {
      $http({
        method: 'GET',
        url: 'account',
        params: {
          action: "getClient",
          numClient: $scope.numClient
        }
      }).success(function(data, status, headers, config) {
        alert(ok);
        $scope.nameCli = data.nomComplet;
        $scope.adresse = data.adresse;
        $scope.email = data.email;
        $scope.tel = data.telephone;
        $scope.sexe = data.sexe;
        $scope.datenaiss = data.dateNaissance;
      });
    };
    alert("ok");
  }
]);



<body ng-controller="accountCtrl">
  <div class="row">
    <div class="col-lg-10 col-md-9">
      <!--                        <div class="col-lg-1"><img src="img/ajout_contact.png" alt=""></div>-->
      <h1 class="titre-contact">Creation d'un compte</h1>
    </div>
  </div>
  <div class="container col-lg-10" style="border: 1px solid silver; border-radius: 1px 1px 1px 1px">
    <div class="row">
      <div class="col-sm-4">
        <div class="tableaudebord">
          <div class="contact_container">
            <div class="row">
              <div class="col-lg-8 col-lg-12 col-md-offset-1">
                <!--   <h2>Ajouter un client</h2> -->
                <div class="nb_com" style="display:none">9</div>
              </div>
            </div>
          </div>
          <div class="container">
            <div class="row">
              <form class="form-horizontal" role="form" data-toggle="validator">
                <div class='row'>
                  <div class="col-lg-8 col-lg-12 col-md-offset-1">
                    <div class="titleprghp">
                      <span class="prg">Informations Client</span>
                    </div>
                  </div>
                </div>
                <div class='row'>
                  <div class="col-lg-4 col-lg-offset-1">
                    <!--<form class="form-horizontal" role="form">-->
                    <div class="form-group">
                      <label for="cin" class="col-sm-2 control-label">Num&eacute;ro D'identit&eacute;</label>
                      <div class="col-sm-8">
                        <input type="text" class="form-control" id="cin" ng-model="numClient" ng-blur="getClient()">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="nom" class="col-sm-2 control-label">Nom :</label>
                      <div class="col-sm-8">
                        <input type="text" class="form-control" id="nameCli" ng-model="nameCli">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="datenaiss" class="col-sm-2 control-label">Date Naissance</label>
                      <div class="col-sm-8" data-validate="date">
                        <input type="date" class="form-control" name="datenaiss" ng-model="datenaiss" id="datenaiss" required="" data-format="DD/MM/YYYY">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="sexe" class="col-sm-2 col-xs-12 control-label">Sexe</label>
                      <div class="form-check">
                        <label class="form-check-label">
                                                        <input  class="form-check-input" type="radio" id="inlineCheckbox1" value="m" ng-model="sexe"> Masculin
                                                    </label>
                        <!--                                            <div class=" hidden-lg hidden-md col-xs-2"></div>-->
                        <label class="form-check-label">
                                                        <input class="form-check-input" type="radio" id="inlineCheckbox2" value="f" ng-model="sexe"> Feminin
                                                    </label>
                      </div>
                    </div>
                    <!--                                    </form>-->
                  </div>
                  <div class="col-lg-4 col-lg-offset-0">
                    <!--<form class="form-horizontal" role="form">-->
                    <div class="form-group">
                      <label for="adresse" class="col-sm-2 control-label">Adresse :</label>
                      <div class="col-sm-8">
                        <input type="text" class="form-control" id="adresse" ng-model="adresse">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="mail" class="col-sm-2 control-label">Email :</label>
                      <div class="col-sm-8" data-validate="email">
                        <input type="email" class="form-control" name="email" id="email" ng-model="email" data-error="Adresse email invalide" required>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="tel" class="col-sm-2 control-label">T&eacute;l&eacute;phone :</label>
                      <div class="col-sm-8" data-validate="phone">
                        <input type="tel" class="form-control" name="tel" id="tel" ng-model="tel" data-error="Le numero de tel est invalide" pattern="^[0-9]{1,}" maxlength="9" placeholder="(+221)77-745-89-65" required>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                  </div>
                </div>
                <div class='row'>
                  <div class="col-lg-8 col-lg-12 col-md-offset-1">
                    <div class="titleprghp">
                      <span class="prg">Informations Compte</span>
                    </div>
                  </div>
                </div>
                <!--<div class="blochp_contact">-->
                <div class='row'>
                  <div class="col-lg-4 col-lg-offset-1">
                    <!--<form class="form-horizontal" role="form">-->
                    <div class="form-group">
                      <label for="numcpt" class="col-sm-2 control-label">Num Compte :</label>
                      <div class="col-sm-8">
                        <input type="text" class="form-control" name="numcpt" id="numcpt">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="nom2" class="col-sm-2 control-label">Nom Client :</label>
                      <div class="col-sm-8">
                        <input type="text" class="form-control" name="nom2" id="nom2">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="" class="col-sm-2 col-xs-12 control-label">Type Compte</label>
                      <div class="form-check">
                        <label class="form-check-label">
                                                        <input  class="form-check-input" type="radio" id="inlineCheckbox1" value="simple"> Simple
                                                    </label>
                        <!--                                            <div class=" hidden-lg hidden-md col-xs-2"></div>-->
                        <label class="form-check-label">
                                                        <input class="form-check-input" type="radio" id="inlineCheckbox2" value="xeweul"> Xeweul
                                                    </label>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-4 col-lg-offset-0">
                    <!--<form class="form-horizontal" role="form">-->
                    <div class="form-group">
                      <label for="dateouv" class="col-sm-2 control-label">Date Ouverture</label>
                      <div class="col-sm-8" data-validate="date">
                        <input type="date" class="form-control" name="dateouv" id="dateouv" required="" data-format="DD/MM/YYYY">
                      </div>
                    </div>
                    <div class="form-group">
                      <label for="solde" class="col-sm-2 control-label">Solde</label>
                      <div class="col-sm-8">
                        <input type="number" class="form-control" name="solde" id="solde">
                      </div>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-lg-5 col-lg-12 col-md-offset-1">
                    <div class="boutton">
                      <div class="col-lg-7 col-md-7 hidden-md hidden-xs"></div>
                      <div class="col-lg-1 col-md-1 col-xs-12">
                        <button name="submit" id="cancel" type="reset" value="reset" class="btn btn-default">Cancel<span class="glyphicon glyphicon-remove"></span></button>
                      </div>
                      <div class="col-lg-3 col-lg-offset-1 col-md-3 col-md-offset-2 col-xs-12 col-sm-push-1">
                        <button name="submit" id="submit" type="submit" value="submit" class="btn btn-default"><span class="glyphicon glyphicon-floppy-disk"></span>Enregistrer <span class="glyphicon glyphicon-chevron-right"></span></button>
                      </div>
                    </div>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

预期输出

    @WebServlet(name = "Account_Servlet", urlPatterns = {"/account"})
public class AccountServlet extends HttpServlet {

    @EJB
private CompteEJBLocal compteEJB;
@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //response.sendRedirect("/home/ajouter_compte.jsp");
        System.out.println("ok");
        String action = request.getParameter("action");
        System.out.println("action " + action);
        switch (action) {
            case "getClient": {
                String numCli = request.getParameter("numClient");
                Client foundCli = compteEJB.unClient(numCli);
                System.out.println("client " + foundCli);
                ObjectMapper map = new ObjectMapper();
                String json = map.writeValueAsString(foundCli);
                response.getWriter().println(json);
            }
            break;
            default: {
            }
            break;
        }
    }

此表也将是n nunber of column

如何进行查询以将每两行合并为一行

5 个答案:

答案 0 :(得分:1)

你可以试试这样的东西

select  coalesce(t2.batch, t1.batch),
        coalesce(t2.maker, t1.maker),
        coalesce(t2.checker, t1.checker)
from    table t1
left join
        table t2
on      t1.id + 1 = t2.id and
        t1.id % 2 = 1

如果奇数行存在,则会将其与以下行相连接。如果存在,则从偶数行中选择列,否则从奇数行中选择。

答案 1 :(得分:0)

col1上的组,isnull(col2,'raja'),col3

答案 2 :(得分:0)

SELECT * FROM Players WHERE id % 2 = 0

答案 3 :(得分:0)

试试这样:

07:53:58 09-03-2017

答案 4 :(得分:0)

试试这个,

DECLARE @Table TABLE (Rowid int,batch varchar(100),maker varchar(100)
,checker varchar(100),date varchar(100))

INSERT INTO  @Table
SELECT 1,101,'john',null,'4feb' UNION ALL
SELECT 2,101,'john','raja','4feb' UNION ALL
SELECT 3,101,'john',null,'5feb' UNION ALL
SELECT 4,101,'john','raja','6feb' UNION ALL
SELECT 5,101,'john',null,'9feb'
select Batch, maker, checker from @Table
where Rowid  %(2 )=0
union ALL
select Batch, maker, checker from @Table a
where  Rowid  %(2 )=1
and not exists(select * from @Table b
where b.Rowid>a.Rowid and Rowid  %(2 )=0)