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.
Author |
Topic |
Chirag23
Starting Member
13 Posts |
Posted - 2012-10-09 : 04:04:49
|
Hi,I have a table that is populated with a USERID, CLIENTID and PROJECTID. One client can have multiple projects and so for each project a new entry has to be put in for a single user. For example:USERID_CLIENTID_PROJECTIDABB_012345_000001ABB_012345_000002ABB_012346_000034CBA_012346_000034What I'm trying to do is for each distinct project to add a new userID to it. So for example I want to add USERID ZXY to every project. So first I identify all the unique PROJECTID'sSELECT DISTINCT PROJECTID FROM TABLEPROJECTID000001000002000034What I need help on is how I use these values and INSERT INTO TABLE all these entries again with the USERID ZXY. So that my table looks like this:USERID_CLIENTID_PROJECTIDABB_012345_000001ABB_012345_000002ABB_012346_000034CBA_012346_000034ZXY_012345_000001ZXY_012345_000002ZXY_012346_000034The CLIENTID and PROJECTID are both unique values independent of each other. I hope this makes sense. I would really appreciate some help.ThanksChirag |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-10-09 : 07:30:40
|
Would this work for you?INSERT INTO YourTableSELECT 'ZXY',MAX(CLIENTID),PROJECTIDFROM YourTableGROUP BY PROJECTID; |
|
|
Chirag23
Starting Member
13 Posts |
Posted - 2012-10-09 : 10:39:33
|
Not quite: This worked for me:INSERT tbl(userid, clientid, projectid) SELECT DISTINCT 'ZXY', a.clientid, a.projectid FROM tbl a WHERE NOT EXISTS (SELECT * FROM tbl b WHERE b.userid = 'XYZ' AND b.clientid = a.clientid AND b.projectid = b.projectid)quote: Originally posted by sunitabeck Would this work for you?INSERT INTO YourTableSELECT 'ZXY',MAX(CLIENTID),PROJECTIDFROM YourTableGROUP BY PROJECTID;
|
|
|
|
|
|