日期:2014-05-18  浏览次数:20519 次

关于数据库表结构的操作
ProductSN Gain_min Gain_max VSWR_ANT_879 VSWR_Rx F_ANT_Rx_20 F_ANT_Rx_816
A8319050032411501784 NULL NULL NULL NULL NULL NULL
A8319050032411501784 NULL NULL NULL NULL NULL 52.1600
A8319050032411501784 NULL NULL NULL NULL 106.8700 NULL
A8319050032411501784 NULL NULL NULL 25.3600 NULL NULL
A8319050032411501784 NULL NULL 23.3800 NULL NULL NULL
A8319050032411501784 19.5200 20.4100 NULL NULL NULL NULL



上面是我的表结构 想把它变成一行(像下面的一样) 有什么办法没

ProductSN Gain_min Gain_max VSWR_ANT_879 VSWR_Rx F_ANT_Rx_20 F_ANT_Rx_816
A8319050032411501784 19.5200 20.4100 23.3800 25.3600 106.8700 52.1600


------解决方案--------------------
SQL code

select ProductSN,max(col1) col1,max(col2) col2,max(col3) col3,...
from tb
group by ProductSN

------解决方案--------------------
SQL code
create table tb(ProductSN varchar(120),Gain_min decimal(18,6), Gain_max decimal(18,6),VSWR_ANT_879 decimal(18,6),VSWR_Rx decimal(18,6),F_ANT_Rx_20 decimal(18,6), F_ANT_Rx_816 decimal(18,6))
go
insert into tb  
select 'A8319050032411501784',NULL, NULL ,NULL, NULL, NULL, NULL  union all
select 'A8319050032411501784',NULL,NULL, NULL, NULL ,NULL ,52.1600 union all
select 'A8319050032411501784',NULL, NULL, NULL ,NULL, 106.8700, NULL union all
select 'A8319050032411501784',NULL, NULL, NULL, 25.3600 ,NULL, NULL union all
select 'A8319050032411501784',NULL ,NULL ,23.3800, NULL, NULL, NULL union all
select 'A8319050032411501784',19.5200, 20.4100, NULL, NULL ,NULL, NULL
go
select ProductSN,max(Gain_min) Gain_min,max(Gain_max) Gain_max,max(VSWR_ANT_879) VSWR_ANT_879,max(VSWR_Rx) VSWR_Rx,max(F_ANT_Rx_20) F_ANT_Rx_20,max(F_ANT_Rx_816) F_ANT_Rx_816
from tb
group by ProductSN
/*
ProductSN    Gain_min    Gain_max    VSWR_ANT_879    VSWR_Rx    F_ANT_Rx_20    F_ANT_Rx_816
A8319050032411501784    19.520000    20.410000    23.380000    25.360000    106.870000    52.160000
*/