473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Single complex INSERT or INSERT plus UPDATE

Hello,

I am writing a stored procedure that will take data from several
different tables and will combine the data into a single table for our
data warehouse. It is mostly pretty straightforward stuff, but there is
one issue that I am not sure how to handle.

The resulting table has a column that is an ugly concatenation from
several columns in the source. I didn't design this and I can't hunt
down and kill the person who did, so that option is out. Here is a
simplified version of what I'm trying to do:

CREATE TABLE Source (
grp_id INT NOT NULL,
mbr_id DECIMAL(18, 0) NOT NULL,
birth_date DATETIME NULL,
gender_code CHAR(1) NOT NULL,
ssn CHAR(9) NOT NULL )
GO
ALTER TABLE Source
ADD CONSTRAINT PK_Source
PRIMARY KEY CLUSTERED (grp_id, mbr_id)
GO
CREATE TABLE Destination (
grp_id INT NOT NULL,
mbr_id DECIMAL(18, 0) NOT NULL,
birth_date DATETIME NULL,
gender_code CHAR(1) NOT NULL,
member_ssn CHAR(9) NOT NULL,
subscriber_ssn CHAR(9) NOT NULL )
GO
ALTER TABLE Destination
ADD CONSTRAINT PK_Destination
PRIMARY KEY CLUSTERED (grp_id, mbr_id)
GO

The member_ssn is the ssn for the row being imported. Each member also
has a subscriber (think of it as a parent-child kind of relationship)
where the first 9 characters of the mbr_id (as a zero-padded string)
match and the last two are "00". For example, given the following
mbr_id values:

12345678900
12345678901
12345678902
11111111100
22222222200

They would have the following subscribers:

mbr_id subscriber mbr_id
12345678900 12345678900
12345678901 12345678900
12345678902 12345678900
11111111100 11111111100
22222222200 22222222200

So, for the subscriber_ssn I need to find the subscriber using the
above rule and fill in that ssn.

I have a couple of ideas on how I might do this, but I'm wondering if
anyone has tackled a similar situation and how you solved it.

The current system does an insert with an additional column for the
subscriber mbr_id then it updates the table using that column to join
back to the source. I could also join the source to itself in the first
place to fill it in without the extra update, but I'm not sure if the
extra complexity of the insert statement would offset any gains from
putting it all into one statement. I plan to test that on Monday.

Thanks for any ideas that you might have.

-Tom.

Jul 23 '05 #1
4 5795
Thomas R. Hummel (to********@hotmail.com) writes:
The member_ssn is the ssn for the row being imported. Each member also
has a subscriber (think of it as a parent-child kind of relationship)
where the first 9 characters of the mbr_id (as a zero-padded string)
match and the last two are "00". For example, given the following
mbr_id values:


I am afraid that I will have to repeat the standard recommendations
(which you as a regular participant of the group should know by now :-)

o CREATE TABLE statements for the tables (OK, those we got)
o INSERT statements for sample data.
o The desired result given the sample data.

It might be that it's a late Friday night, and I am tired after a working
week, but I could not make out where this subscriber mbr_id comes from;
there was no such column in the table. Nor did I understand how the
SSNs came into the picture.

And it should come us no surprise that I don't like guessing.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Sorry, it was a late Friday for all of us :-)

Using the above CREATE TABLE statements:

INSERT INTO Source VALUES (1, 12345678900, '1968-02-02', 'M',
'111111111')
INSERT INTO Source VALUES (1, 12345678901, '1971-04-11', 'M',
'222222222')
INSERT INTO Source VALUES (1, 12345678902, '2001-10-03', 'F',
'333333333')
INSERT INTO Source VALUES (1, 11111111100, '1974-08-15', 'F',
'444444444')
INSERT INTO Source VALUES (1, 22222222200, '1974-09-22', 'F',
'555555555')
GO

I would like the destination table filled like this:

grp_id mbr_id birth_date gender_code member_ssn
subscriber_ssn
1 12345678900 1968-02-02 M 111111111 111111111
1 12345678901 1971-04-11 M 222222222 111111111
1 12345678902 2001-10-03 F 333333333 111111111
1 11111111100 1974-08-15 F 444444444 444444444
1 22222222200 1974-09-22 F 555555555 555555555

Since the first 9 characters of the mbr_id for the first three rows are
the same, they all of the same subscriber, which is the one that has
"00" as the last two characters. In other words, the first 9 characters
of the mbr_id are really more of a family ID, while the last two
differentiate the members of the family, with the primary family member
having "00". They need that primary family member's SSN included in the
destination table.

Right now I am planning to have an intermediate table anyway, so I will
either split up the mbr_id into those two parts so that I can easily
join the table to itself to fill the destination table, or I will just
fill in a new column (subscriber_mbr_id) in the intermediate table with
the first 9 characters plus a hard-coded "00" and use that to join the
table to itself. BTW, there will ALWAYS be a record that ends in "00"
for each SUBSTRING(mbr_id, 1, 9). In other words, if there is a mbr_id
of 12345678901 then there is guaranteed to be a mbr_id of 12345678900
somewhere in the table.

Thanks,
-Tom.

Jul 23 '05 #3
Thomas R. Hummel (to********@hotmail.com) writes:
I would like the destination table filled like this:

grp_id mbr_id birth_date gender_code member_ssn
subscriber_ssn
1 12345678900 1968-02-02 M 111111111 111111111
1 12345678901 1971-04-11 M 222222222 111111111
1 12345678902 2001-10-03 F 333333333 111111111
1 11111111100 1974-08-15 F 444444444 444444444
1 22222222200 1974-09-22 F 555555555 555555555


Here's way to skin the cat:

INSERT Destination (grp_id, mbr_id, birth_date, gender_code, member_ssn,
subscriber_ssn)
SELECT a.grp_id, a.mbr_id, a.birth_date, a.gender_code, a.ssn,
b.ssn
FROM Source a
JOIN Source b ON b.grp_id = a.grp_id
AND b.mbr_id = floor(a.mbr_id / 100) * 100
Since mdr_id was declared as numeric, I used mathematical functrions
rather than string functions.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Erland,

Thanks for the SQL. I used something similar to that and it worked
well.

Thanks,
-Tom.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tuhin Kumar | last post by:
Hi, I would like to know how to insert multiple rows into a table, using a single INSERT statement. My requirement is like this I have a table ABC which contains multiple employees entries with...
22
by: Bryan Guilliams | last post by:
I'm trying to come up with an elegant, simple way to compare two consecutive values from the same table. For instance: SELECT TOP 2 datavalues FROM myTable ORDER BY timestamp DESC That...
8
by: J.Haan | last post by:
Hi all. I'm currently coping with a problem on which I hope you could shed some light. Imagine the following: I have table in DB2 8.1 (.5) which is defined as: table test { t1 smallint,...
12
by: Bob Stearns | last post by:
This is probably the wrong forum for this, but I thought it might start some discussion. The INSERT statement, in its current form, has problems being formatted so a human reader can follow it....
3
by: Solution Seeker | last post by:
I want to Store the String value with Single Quotes in the Field of Database where if i try to Store the String value with Single Quotes (as it is) then it is throwing the error as SQL String...
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
3
by: renatois | last post by:
SqlDataSource UpdateCommand plus Insert I have a SqlDataSource with an UpdateCommand but besides that i need also an Insert command triggered on the same update command. That´s because i need...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Have a complex process where I need to Import a large amount of data then run some transformations on this data then import into DataBase. The transformation involves multiple fields and multiple...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.