如何将两行合并为一行?

时间:2013-03-26 10:40:34

标签: sql sql-server sql-server-2008

如何将两行合并为一行。

EMPCode  actHours    actDate     rowno
107 8.00        2013-01-21  1
107 8.30        2013-01-22  1
107 4.00        2013-01-23  1
107 4.00        2013-01-23  2
107 4.10        2013-01-24  1
107 4.20        2013-01-24  2
107 4.30        2013-01-25  1
107 4.30        2013-01-25  2

I want the result as follows:

actHours    actDate 
8.00        2013-01-21 
8.30        2013-01-22
8.00        2013-01-23 
8.30        2013-01-24
9.00        2013-01-25  

任何帮助将不胜感激。在此先感谢: - )

3 个答案:

答案 0 :(得分:2)

试试这个:

SELECT SUM(actHours) actHours, actDate FROM Tbl GROUP BY actDate

实际上,如果提供列的数据类型,答案可能更具体。

答案 1 :(得分:0)

如果数据包含这些内容怎么办?

108 3.40        2013-01-26  1
108 3.50        2013-01-26  2

如果数据类型为time,则您可以使用此查询..

with datecte
as
(
 SELECT actDate,  SUM(DATEDIFF(MINUTE, '0:00:00', actHours)) actHours
 FROM Tb1
 GROUP BY actDate
)
SELECT actDate,
actHours = RTRIM(actHours/60) + ':' + RIGHT('0' + RTRIM(actHours%60),2)
FROM datecte

答案 2 :(得分:0)

然后使用它,

with datecte
as
(
 select  actDate, SUM(DATEDIFF(MINUTE, '0:00:00', actHours)) actHours
 from (
   SELECT actDate,CAST(REPLACE(actHours,'.',':') as time) actHours
   FROM #tb1
      ) k 
 group by actDate
)


SELECT actDate,
actHours = RTRIM(actHours/60) + ':' + RIGHT('0' + RTRIM(actHours%60),2)
FROM datecte

希望这能为您提供解决方案..

好的@praveen ..