首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

分组更新有关问题

2012-02-08 
分组更新问题create table #test(key_id nvarchar(36),j_name nvarchar(36),j_dt datetime,j_id nvarchar(

分组更新问题
create table #test(key_id nvarchar(36),j_name nvarchar(36),j_dt datetime,j_id nvarchar(36))
insert into #test(key_id,j_name,j_dt)
select newid(), 'jack','2011-11-11' union all
select newid(), 'jack','2011-11-11' union all
select newid(), 'jack','2011-11-11' union all
select newid(), 'jack','2011-11-11' union all
select newid(), 'jack','2011-11-11' union all
select newid(), 'jack1','2011-11-12' union all
select newid(), 'jack1','2011-11-12' union all
select newid(), 'jack1','2011-11-12' union all
select newid(), 'jack1','2011-11-12' union all
select newid(), 'jack1','2011-11-12' union all
select newid(), 'jack1','2011-11-12' union all
select newid(), 'jack2','2011-11-11' union all
select newid(), 'jack2','2011-11-11' union all
select newid(), 'jack2','2011-11-11' union all
select newid(), 'jack2','2011-11-11' union all
select newid(), 'jack2','2011-11-11' union all
select newid(), 'jack2','2011-11-11' 

要将j_name 和j_dt 相同的 数据更新j_id 为同样的newid()


[解决办法]

SQL code
update aset a.j_id = b.j_idfrom tb a join (select j_name,j_dt,min(j_id) as j_id from tb group by j_name,j_dt)b   on a.j_name = b.j_name and a.j_dt = b.j_dt
[解决办法]
SQL code
#define OPTTIMES 8doublepolyh2(double a[], double x, unsigned long degree) { double result1 = 0.0, result2 = 0.0, result3 = 0.0, result4 = 0.0, xpwr = x; double *end = &(a[degree - OPTTIMES + 1]); if(degree < OPTTIMES) {    result1 = *a++;    end = &(a[degree - 1]);  while(a<= end) {    result1 += *a++ * xpwr;    xpwr *= x;  }  return result1; } result1 = *a++; result2 = *a++ * xpwr; xpwr *= x; result3 = *a++ * xpwr; xpwr *= x; result4 = *a++ * xpwr; xpwr *= x; while(a < end) {    result1 = result1 + *a++ * xpwr;    xpwr *= x;    result2 = result2 + *a++ * xpwr;    xpwr *= x;    result3 = result3 + *a++ * xpwr;    xpwr *= x;    result4 = result4 + *a++ * xpwr;    xpwr *= x;    result1 = result1 + *a++ * xpwr;    xpwr *= x;    result2 = result2 + *a++ * xpwr;    xpwr *= x;    result3 = result3 + *a++ * xpwr;    xpwr *= x;    result4 = result4 + *a++ * xpwr;    xpwr *= x; } result1 = result1 + result2 + result3 + result4; end = &(a[degree - 1]); while(a <= end) {    result1 += *a++ * xpwr;    xpwr = xpwr * x; } return result1;}
[解决办法]
SQL code
create table #test(key_id nvarchar(36),j_name nvarchar(36),j_dt datetime,j_id nvarchar(36))insert into #test(key_id,j_name,j_dt)select newid(), 'jack','2011-11-11' union allselect newid(), 'jack','2011-11-11' union allselect newid(), 'jack','2011-11-11' union allselect newid(), 'jack','2011-11-11' union allselect newid(), 'jack','2011-11-11' union allselect newid(), 'jack1','2011-11-12' union allselect newid(), 'jack1','2011-11-12' union allselect newid(), 'jack1','2011-11-12' union allselect newid(), 'jack1','2011-11-12' union allselect newid(), 'jack1','2011-11-12' union allselect newid(), 'jack1','2011-11-12' union allselect newid(), 'jack2','2011-11-11' union allselect newid(), 'jack2','2011-11-11' union allselect newid(), 'jack2','2011-11-11' union allselect newid(), 'jack2','2011-11-11' union allselect newid(), 'jack2','2011-11-11' union allselect newid(), 'jack2','2011-11-11'go--取每组的第一条的key_id进行j_id的更新update t1 set j_id=t2.key_id from #test t1 cross apply (select top 1 * from #test where j_name=t1.j_name and j_dt=t1.j_dt) t2
[解决办法]
探讨
引用:

SQL code
create table #test(key_id nvarchar(36),j_name nvarchar(36),j_dt datetime,j_id nvarchar(36))
insert into #test(key_id,j_name,j_dt)


select newid(), 'jack','2011-11-11'……

热点排行