2013-02-22 118 views
-1

如果您拥有的数据与此列表:如何显示正确的结果

nvarchar int int nvarchar nvarchar 
Line  start end typ  color 
------------------------------------------- 
T11  1  null cookie  Blue 
T11  null 10  cookie  Blue 
T11  null null cookie  Blue 
T11  null null cookie  Blue 
T2  20  null computer Red 
T2  null null computer Red 
T2  null 52  computer Red 
T3  null null dark  black 
T3  52  null dark  black 
T3  null 10  dark  black 

请求的结果应该是:

Line  start end typ  color 
------------------------------------------- 
T11  1  10  cookie  Blue 
T2  20  52  computer Red 
T3  52  10  dark  black 

我应该怎么做在SQL Server 2012中?

回答

2

我认为这是你要求的。显然用表名替换tablename。

SELECT t.line, MIN(t.START), MAX(t.END), t.typ, t.color 
FROM dbo.tablename t 
GROUP BY t.line, t.typ, t.color