SQL - 组合行

时间:2015-04-28 09:53:59

标签: sql-server

我在表格中有如下行:

[date],[name],[duty],[holiday],[hdaypart],[sick],[sdaypart]

2015-04-27, person1, 1,0,NULL,0,NULL

2015-04-27, person1, 0 1,'fd',0,NULL

我想将这些行合并到:

[date],[name],[duty],[holiday],[hdaypart],[sick],[sdaypart]

2015-04-27, person1, 1,1,'fd',0,NULL

作为BIT专栏的职责,假日和病假专栏。

有办法吗?

我能提出的一个解决方案是使用子查询,但它会耗费大量时间。更快的解决方案会很好。

这就是我现在所拥有的:

SELECT DISTINCT [name],[date],[region],[cluster]
,CASE WHEN (SELECT SUM(CONVERT(INT,callduty)) FROM planning AS t2
            WHERE t1.[Date] = @datum AND t2.[Name] = t1.[name] AND t2.[Date] = t1.[date] ) > 0
      THEN 1 ELSE 0 END AS [CallDuty]
,CASE WHEN (SELECT SUM(CONVERT(INT,holiday)) FROM planning AS t2
            WHERE t1.[Date] = @datum AND t2.[Name] = t1.[name] AND t2.[Date] = t1.[date] ) > 0
      THEN 1 ELSE 0 END AS [Holiday]

FROM planning AS t1
where t1.[Date] = @datum AND t1.[Name] like @naam
group by t1.[date],t1.[name], t1.Region, t1.cluster
order by t1.[name]

4 个答案:

答案 0 :(得分:3)

您似乎希望按日期和名称进行分组,并选择每个组中的最大值或非空值。 MAX聚合函数适用于以下两种选择:

SELECT [date],[name], MAX([duty]), MAX([holiday]),
       MAX([hdaypart]), MAX([sick]), MAX([sdaypart])
FROM mytable
GROUP BY [date],[name]

答案 1 :(得分:1)

通过查看您的示例,我假设您希望获得特定用户的最大值。

您可以使用group by和max

执行此操作
    <local:BaseControl  x:Class="MyNamespace.CustomControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyNamespace"> 
   <Grid>
   </Grid>
   </local:BaseControl>

这不是很漂亮,但应该比使用子查询更好。

编辑: 如果您具有位sql类型的列,请使用

<UserControl  x:Class="MyNamespace.CustomControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
   <Grid>
   </Grid>
   </UserControl>

按照Giorgos Betsos的建议,在组中添加日期列,结果是

select max([date]),[name],max([duty]),max([holiday]),max([hdaypart]),max([sick]),max([sdaypart])
from yourtable
group by name

答案 2 :(得分:1)

declare @t table ([date] date,[name] varchar(10),[duty] varchar(10),[holiday] int,[hdaypart] varchar(10),[sick] int,[sdaypart]
int
)
insert into @t([date],[name],[duty],[holiday],[hdaypart],[sick],[sdaypart])values ('2015-04-27','person1',1,0,NULL,0,NULL),
('2015-04-27','person1',1,0,'fd',0,NULL)

select MAX([date]),MAX([name]),MAX([duty]),MAX([holiday]),MAX([hdaypart]), [sick],[sdaypart] from @t
group by sick,[sdaypart]

OR

select [date],[name],[duty],[holiday],MAX([hdaypart])AS H,[sick],[sdaypart] from @t
group by [date],[name],[duty],[holiday],[sick],[sdaypart]
UNION 
select [date],[name],[duty],[holiday],MAX([hdaypart])AS H,[sick],[sdaypart] from @t
group by [date],[name],[duty],[holiday],[sick],[sdaypart]

答案 3 :(得分:0)

CREATE TABLE #Combine
(
    [date] date,
    [name] VARCHAR(10),
    [duty] CHAR(1),
    [holiday] CHAR(1),
    [hdaypart] CHAR(5),
    [sick]  CHAR(1),
    [sdaypart] VARCHAR(10)

)

INSERT INTO #Combine VALUES('2015-04-27', 'person1', '1','0',NULL,'0',NULL),

('2015-04-27', 'person1', '0','1','fd','0',NULL)


SELECT MAX(Date) [date],MAX(name) [name],MAX(Duty) [duty],MAX(holiday) holiday,
MAX(hdaypart) hdaypart,max(sick) sick,max(sdaypart)sdaypart FROM #Combine