在sql server中合并多行到一行

时间:2017-12-13 09:43:36

标签: sql-server tsql merge

我有一张如下表格,

Date                 Type     ClassA   ClassB
--------------------------------------------------
02/05/2015             A        1        
04/05/2015             B        2        - 
04/05/2015             B        -        3  
05/05/2015             A        4        - 
05/05/2015             B        5        - 

我正在尝试合并以下行(必需输出)

Date                 Type     ClassA   ClassB
--------------------------------------------------
02/05/2015             A        1        -
04/05/2015             B        2        3 
05/05/2015             A        4        - 
05/05/2015             B        5        - 

在上面的例子中,当'日期'和'键入'在2个单独的行中匹配,我应该合并其他列值以成为单行

2 个答案:

答案 0 :(得分:2)

ul {
  list-style: none;
}

ul#pri.nav {
  margin-left: 0px;
  z-index: -1;
  border-top-right-radius: 15px;
  border-top-left-radius: 15px;
  text-decoration: none;
  width: 97%;
  background-color: rgb(180, 221, 180);
}

ul.nav {
  height: 10px;
}

ul.nav li {
  float: left;
  margin-top: 0px;
  padding: 10px 13px;
  font-family: Arial, sans-serif;
  font-size: small;
  line-height: 15px;
  cursor: pointer;
  font-weight: 700;
}

ul.nav#pri li {
  margin-top: 0px;
  float: left;
  margin-right: 5px;
  font-family: 'Arial Rounded MT';
  color: #fff;
  width: 100px;
  height: 6px;
  text-align: center;
  padding: 20px;
  display: block;
  -moz-border-radius-topleft: 15px;
  -webkit-border-top-left-radius: 15px;
  -moz-border-radius-topright: 15px;
  -webkit-border-top-right-radius: 15px;
  border-bottom-style: none;
  border-bottom-color: inherit;
  border-bottom-width: medium;
  background-color: #0fc15e;
  position: relative;
  bottom: 45px;
}

ul.nav#pri li a {
  text-decoration: none;
  color: #fff;
}

ul.nav#pri li.active,
ul.nav#pri li:hover ul.nav#sec {
  display: block;
  font-family: 'Arial Rounded MT';
  color: #a67cd5;
}

.nav#pri li:hover ul {
  color: #2b95b2;
}

ul.nav#sec {
  display: none;
  /*float:left;
text-align:center;
border-top-right-radius:15px;
border-top-left-radius:15px;
text-decoration:none;
color:#fff;


 font-family:'Arial Rounded MT';
 font-size:15px;
 color:#fff;
 margin-top:-1px;
 width:97%;
    */
  /*height:25px;
 border-top-right-radius:15px;*/
}


/*ul.nav#pri li li.active
{

}*/

ul.nav#sec li a {
  text-decoration: none;
  color: #fff;
  font-size: 14px;
}

ul.nav#sec li:hover {
  font-family: 'Arial Rounded MT';
  text-decoration: none;
  color: #fff;
  background-color: #747171;
}

答案 1 :(得分:0)

试试这个:

SELECT date, type, SUM(classA), SUM(classB)
FROM yourtable
GROUP BY date, type