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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Enter row ids into the database

Author  Topic 

Japboix1
Starting Member

24 Posts

Posted - 2011-07-13 : 09:51:59
I have a large database, with the values already entered, that I need to assign id numbers to. I can use the dense_rank function to give me appropriate row ids, however I don't know how to actually enter those values into my database.

Does anyone know how to do this?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-13 : 10:48:35
Here is an example of how you can do it. If it doesn't do what you are trying to do, post the table DDL and some sample data?
CREATE TABLE #tmp (id INT, val1 INT);

INSERT INTO #tmp (val1)
SELECT 11 UNION ALL
SELECT 11 UNION ALL
SELECT 20 UNION ALL
SELECT 30 UNION ALL
SELECT 30 UNION ALL
SELECT 45 ;

WITH cte AS
(
SELECT *,DENSE_RANK() OVER (ORDER BY val1) AS RN
FROM #tmp
)
UPDATE cte SET id = rn;

SELECT * FROM #tmp;

DROP TABLE #tmp;
Go to Top of Page

Japboix1
Starting Member

24 Posts

Posted - 2011-07-13 : 11:03:08
quote:
Originally posted by sunitabeck

Here is an example of how you can do it. If it doesn't do what you are trying to do, post the table DDL and some sample data?

Using:
 select dense_rank () over (order by Policynumber) as ID
,[PolicyNumber]
,[ClientId]
,[InsuredName]
FROM policy
Order by insuredname

gives me this:

ID PolicyNumber ClientID InsuredName

96882 00001 NULL Company 1
96882 00001 NULL Company 1
96882 00001 NULL Company 1
58960 01010 NULL Company 2
62504 02034 NULL Company 3
89298 11230 NULL Company 4
89298 11230 NULL Company 4
89298 11230 NULL Company 4

The table is called Policy
Basically, the ID column is the result I get when I use the dense_rank function. What I want is to enter those values into the ClientId column of the function. All of the data has already been entered into the table.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-13 : 11:38:16
[code]WITH CTE AS (
select dense_rank () over (order by Policynumber) as ID
,[PolicyNumber]
,[ClientId]
,[InsuredName]
FROM policy
-- Order by insuredname
)
UPDATE CTE SET [ClientId] = ID;
[/code]Test before doing for real.
Go to Top of Page

Japboix1
Starting Member

24 Posts

Posted - 2011-07-13 : 11:42:55
Thank you so much XD It works perfectly.
Go to Top of Page

Japboix1
Starting Member

24 Posts

Posted - 2011-07-13 : 12:33:49
Actually, sorry, I have a small problem. The Query works fine and it creates the clientID values properly. However, I want to know if there is a way to edit clientID values. Right now, I have it organized by the policynumber. Is there a way that I could edit the entire thing to also sort itself by insuredname?

For example, I have this:
PolicyNumber ClientId InsuredName
0700000 19434 ATELIERS GABRIEL MORIN INC. 10833, AVENUE
0700000 11681 ATELIERS GABRIEL MORIN INC.
0700000 11681 ATELIERS GABRIEL MORIN INC.

I sorted it by the insuredname. However, I also want anything with the same Policynumber to have the same ClientID. Is there anyway to update the table so that anything with either the same policynumber or the same insuredname will have the same ClientID?
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-13 : 12:44:41
Add InsuredName also to your dense_rank sorting:

select dense_rank () over (order by [InsuredName],Policynumber) as ID
Go to Top of Page

Japboix1
Starting Member

24 Posts

Posted - 2011-07-13 : 13:02:22
quote:
Originally posted by sunitabeck

Add InsuredName also to your dense_rank sorting:

select dense_rank () over (order by [InsuredName],Policynumber) as ID




This doesn't work. I need it to be like this.

InsuredName PolicyNumber ClientID
Company A 00001 1
Company A 00002 1 (because the InsuredName is the same)
Company B 00001 1 (Because it has the same Policy Number
as the one before it.)
Company C 00003 2 (Because neither of the columns are the
same.)


If I do what you suggested, it still sorts it seperately if either of the columns are different.
This is basically what happens when I use the method you suggested.

InsuredName PolicyNumber ClientID
Company A 00001 1
Company A 00002 2 (because the InsuredName is the same,
but the PolicyNumber is different)
Company B 00001 3 (Because the name is different)
Company C 00003 4

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-13 : 13:12:52
When you add both columns to the order by clause, each unique combination of the two columns is assigned a rank. But it looks like what you want is to have a rank if either of the columns are the same. That seems to be not very well-defined problem. For example, in the following example, what should the client ids be?

POLICY# INSUREDNAME
------- -----------
0700001 SOMETHING
0700001 SOMETHINGELSE
0700002 SOMETHINGELSE
0700002 YETAGAINSOMETHINGELSE
0700003 YETAGAINSOMETHINGELSE
Go to Top of Page

Japboix1
Starting Member

24 Posts

Posted - 2011-07-13 : 13:17:39
quote:
Originally posted by sunitabeck

When you add both columns to the order by clause, each unique combination of the two columns is assigned a rank. But it looks like what you want is to have a rank if either of the columns are the same. That seems to be not very well-defined problem. For example, in the following example, what should the client ids be?

POLICY# INSUREDNAME
------- -----------
0700001 SOMETHING
0700001 SOMETHINGELSE
0700002 SOMETHINGELSE
0700002 YETAGAINSOMETHINGELSE
0700003 YETAGAINSOMETHINGELSE




Yeah, exactly, it's not well defined I guess. For your example, it would be:

POLICY# INSUREDNAME ClientID
------- ----------- --------
0700001 SOMETHING 1
0700001 SOMETHINGELSE 1
0700002 SOMETHINGELSE 1
0700002 YETAGAINSOMETHINGELSE 1
0700003 YETAGAINSOMETHINGELSE 1


But I'm assuming that there shouldn't be a lot of cases like this. Usually, there are just a lot of policy numbers under one name or a lot of names, under a single policy number.

The biggest issue is that there are simply too many entries to go through and fix manually.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-13 : 13:25:04
I don't know of an easy way to do this in SQL without resorting to while loops or something similar that is unsightly.

One possibility is that you separate them out into two groups, one where there is one-to-many relationship from Policy Number to Insured Name and none the other way, and the second group exactly the opposite. Assign numbers to those. Then examine whatever is left out to see what needs to be done.
Go to Top of Page

Japboix1
Starting Member

24 Posts

Posted - 2011-07-13 : 13:32:59
quote:
Originally posted by sunitabeck

I don't know of an easy way to do this in SQL without resorting to while loops or something similar that is unsightly.

One possibility is that you separate them out into two groups, one where there is one-to-many relationship from Policy Number to Insured Name and none the other way, and the second group exactly the opposite. Assign numbers to those. Then examine whatever is left out to see what needs to be done.


Hmm, so it's just a really messy situation? How exactly do you do while loops? I know how to do while loops in C, so is it similar to that?
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-13 : 15:49:21
quote:
Originally posted by Japboix1


Hmm, so it's just a really messy situation? How exactly do you do while loops? I know how to do while loops in C, so is it similar to that?

It is messy, at least to my simple-minded way of thinking.

There is a while construct in SQL (http://msdn.microsoft.com/en-us/library/ms178642.aspx).

Let us see if anyone else would be able to suggest simpler ways of doing this. In the meantime, if the light bulb goes on in my head, I will post back.

How many rows do you have in the table that you are trying to update?

Go to Top of Page

Japboix1
Starting Member

24 Posts

Posted - 2011-07-13 : 15:51:35
Ok, thanks for the help so far XD

There are roughly... 3000 rows to update. Possibly a little more than that.
Go to Top of Page
   

- Advertisement -