当前位置: 代码迷 >> Sql Server >> 分组更新有关问题
  详细解决方案

分组更新有关问题

热度:61   发布时间:2016-04-27 14:53:44.0
分组更新问题
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)
  相关解决方案