Author |
Topic |
jayram11
Yak Posting Veteran
97 Posts |
Posted - 2011-07-05 : 12:14:48
|
Hello i have a table like thisCREATE TABLE [dbo].[ANNUAL]( [SERIAL] [char](5) NOT NULL, [START] [smalldatetime] NOT NULL, [GROUP] [varchar](4) NULL, [RW] [float] NULL, [PR] [float] NULL, [NU] [float] NULL, [MU] [float] NULL)values are likeinsert into ANNUAL select '12345', '04/01/2011', '0625' , 1.236498, 5.6465466, NULL, NULL unionselect '12345', '04/01/2011', '0625' , 1.236498, 5.6465466, NULL, 2.35 unionselect '12345', '04/01/2011', '0625' , 1.236498, 5.6465466, 0.95, NULL unionselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, NULL, 2.75 unionselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, NULL unionselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, NULL unionselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, NULL, 2.75 unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, 0.99, 3.26 unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, 0.94, 3.29 unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, NULL, NULL unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, NULL, NULL unioni want to update NU and MU where it is NULL for a SERIAL to a rate from another SERIAL within the same GROUP where there is a rate. i do it separately for NU and MU like thisupdate Aset A.nu = B.nu from (select * from ANNUAL where Start = '04/01/2011' and nu is null) Ainner join (select * from ANNUAL where Start = '04/01/2011' and nu is not null) Bon A.GROUP = B.GRoupand A.PR = B.PRand A.RW = B.RWand then i do it for MU.1. can i combine and do it one update statement2. if there are more than one distinct rate of NU and/or MU for the same GROUP and START date, i do not want to update anything but want to pull it out 'SELECT' and review it e.g. for GROUP '0628'3. can this be done in a function or sprocthanks |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-07-05 : 12:35:01
|
Don't you have a requirements issue here.Look at the last 4 rows in the data you have given.You would want to update the last two as they are null but you have two rows that are not null - which do you want to use for the update>?==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
jayram11
Yak Posting Veteran
97 Posts |
Posted - 2011-07-05 : 12:47:50
|
In that case i did not want to update it?from my requirement 2.2. if there are more than one distinct rate of NU or MU for the same GROUP and START date, i do not want to update anything but i want to pull it out in a separate 'SELECT' and review it manually e.g. for GROUP '0628'select '12345', '04/01/2011', '0628' , 1.252, 5.789,0.99, 3.26unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, 0.94, 3.29unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, NULL, NULL unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, NULL, NULL union |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-05 : 14:04:18
|
so what should be your output from above dataset?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
jayram11
Yak Posting Veteran
97 Posts |
Posted - 2011-07-05 : 14:07:38
|
Hi VisakhThis is the output i am looking forselect '12345', '04/01/2011', '0625' , 1.236498, 5.6465466, 0.95, 2.35 unionselect '12345', '04/01/2011', '0625' , 1.236498, 5.6465466, 0.95, 2.35 unionselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, 2.75 unionselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, 2.75 unionselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, 2.75 unionselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, 2.75 unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, 0.99, 3.26 unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, 0.94, 3.29 unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, NULL, NULL unionselect '12345', '04/01/2011', '0628' , 1.252, 5.789, NULL, NULL union |
 |
|
jayram11
Yak Posting Veteran
97 Posts |
Posted - 2011-07-05 : 14:12:58
|
select '12345', '04/01/2011', '0625' , 1.236498, 5.6465466, 0.95, 2.35 union allselect '12345', '04/01/2011', '0625' , 1.236498, 5.6465466, 0.95, 2.35 union allselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, 2.75 union allselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, 2.75 union allselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, 2.75 union allselect '12345', '04/01/2011', '0627' , 1.236445, 5.565, 0.97, 2.75 union allselect '12345', '04/01/2011', '0628' , 1.252, 5.789, 0.99, 3.26 union allselect '12345', '04/01/2011', '0628' , 1.252, 5.789, 0.94, 3.29 union allselect '12345', '04/01/2011', '0628' , 1.252, 5.789, NULL, NULL union allselect '12345', '04/01/2011', '0628' , 1.252, 5.789, NULL, NULL |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-07-05 : 14:21:11
|
Then you will have to deal with mu and nu separately. Can stil be a single update but the select will have to be a separate statement so why bother.update aset nu = case when b.numnu = 1 and a.nu is null then b.nu else a.nu end, mu = case when b.nummu = 1 and a.mu is null then b.mu else a.mu endfrom Annual ajoin (select GROUP, PR, RW, nu = max(nu), mu = max(mu), numnu = count(nu), nummu = count(mu) from Annual group by GROUP, PR, RW) bon a.GROUP = b.GROUPand a.PR = b.PRand a.RW = b.RWwhere (a.nu is null and b.nu is not null and b.numnu = 1)or (a.mu is null and b.mu is not null and b.nummu = 1)==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-05 : 14:22:26
|
seems like thisUPDATE tSET t.NU= t1.NU,t.MU=t1.MUFROM Table tINNER JOIN (SELECT SERIAL,GROUP,MAX(MU) AS MU,MAX(NU) AS NU, SUM(CASE WHEN MU IS NOT NULL AND NU IS NOT NULL THEN 1 ELSE 0 END) AS Cnt FROM Table GROUP BY SERIAL,GROUP)t1ON t1.SERIAL = t.SERIALAND t1.GROUP = t.GROUPWHERE t1.Cnt = 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
jayram11
Yak Posting Veteran
97 Posts |
Posted - 2011-07-05 : 14:43:48
|
Thanks!!! |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-05 : 14:45:20
|
w/c------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|