Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I want to insert values into P if the count rows of a specfic ID within the range in table Q.select count(*) from Pwhere ID=1if MIN<=count(*)<=MAXI guess the code could look like:
insert into pvalues(1,15)where p.id=1 and rowcnt between q.low and q.high
Thanks
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-08-23 : 00:55:29
what will be values you will be inserting for NUM ield? just a random value?
zhshqzyc
Posting Yak Master
240 Posts
Posted - 2008-08-23 : 08:38:48
NUM is a given value.When the condition is satisfied just insert it.Otherwise if count(*) beyonds the range, it is toprevent for insertion.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-08-23 : 10:17:32
May be this:-
INSERT INTO P(ID,NUM)SELECT q.ID,@NumFROM Q qINNER JOIN (SELECT ID,COUNT(1) AS RecCnt FROM P GROUP BY ID) pON p.ID=q.IDAND p.RecCnt BETWEEN q.MIN AND q.MAX