Advertisement

10.24.2007 at 10:37AM PDT, ID: 22915338
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

SQL INSERT or UPDATE query depending on SELECT matching algoritham

Asked by Thukaung in MS SQL Server, SQL Query Syntax

Tags: , , , ,

I have a patient database I am trying to INSERT or UPDATE data, with values parsed from an email, depending on whether the patient already exists in the database.

To parse the email I am using a program call Email2DB. The program will parse and collect values from the email, and does a SQL query INSERT or UPDATE depending on SELECT. It allows custom query scripts. (www.email2db.com)

Table Column Design (Table name is Patient)
[Chart Number] - Key Field
[Last Name]
[First Name]
[Social Number]
[Birth Date]
[Sex]
[Anything]

Email2DB Variables and example values
@Chart_Number  9999010190 (created from last 4SSN+DOB)
@Last_Name  Doe
@First_Name  John
@ Socail_Number  999-99-9999
@Birth_Date  01/01/1990
@Sex  0 (0 for male and 1for female)
@Anything = anything

Matching Algorithm Needed
I need to check whether the patient already exists in the database. Here is the algorithm I need.

1.      Read patient registration information received by email and gather data fields. (Done by Email2DB)
2.      Determine if record is new or existing (also need to be done in Email2DB)
             IF [Chart Number] == @Chart_Number
      IF ([Last Name] = @Last_Name AND [Birth Date] = @Birth_Date)
                   UPDATE record.
             ELSE IF [Social Number] == @Social_Number
                       IF ([Birth Date] == @Birth_Date AND [Last Name] == @Last_Name AND [First Name] == @First_Name)
                   UPDATE record
              ELSE IF [Last Name] == @Last_Name
                        IF ([First Name] == @First_Name AND [Brith Date] == @Birth_Date AND [Sex] == @Sex)
                                  UPDATE record
               ELSE
                                   INSERT record


Current Query commands (generate by Email2DB and modified by me)

Insert Query
INSERT INTO Patient ([Chart Number], [Last Name], [First Name], [Social Number], [Birth Date], [Sex], [Anything]) VALUES (@Chart_Number, @Last_Name, @First_Name, @Social_Number, @Birth_Date, @Sex, @Anything)

Update Query
UPDATE Patient SET [Last Name] = @Last_Name, [First Name] = @First_Name, [Social Number] = @Social_Number, [Birth Date] = @Birth_Date, [Sex] = @Sex, [Anything] = @Anything

Notes: Chart Number is omitted because it should not be change for existing patient.

Select Query (I will list two queries I tried out since I dont know how to combine these conditions)
Query 1: SELECT * FROM Patient WHERE ([Chart Number] = @Chart_Numer AND [Last Name] = @Last_Name AND [Birth Date] = @Birth_Date)

Query 2: SELECT * FROM Patient WHERE ([Last Name] = @Last_Name AND [Social Numer] = @Social_Number AND [Birth Date] = @Birth_Date AND [Last Name] = @Last_Name AND [First Name] = @First_Name)

Problem
1.      Lets say I have a patient in my table with following record: [Chart Number] = 9999010190, [Last Name] = Doe, [First Name] = John, [Social Number]=999-99-9999, [Birth Date] = 01/01/1990, [Sex] = 0
2.      Query 1 works just fine if I have the correct Chart Number, Last Name, and Birth Date. It will find the correct record and UPDATE it.
3.      If I have incorrect Chart Number, and has to use query 2, Email2DB will report existing record update but if I look at the actual table nothing is updated.
4.      I think this is because I dont have the chart number, which is the key field, SQL cannot find the record. Am I right?
5.      So I think what need to be done is
      - if the chart number is incorrect, but it is still a existing patient (using the matching algoritham)
      - Extract the correct chart number from the Patient table and put it in @Chart_Number
      - Then do an UPDATE.

Any help will be greatly appreciated.

Start Free Trial
[+][-]10.24.2007 at 11:42AM PDT, ID: 20141733

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.24.2007 at 11:46AM PDT, ID: 20141769

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.24.2007 at 11:48AM PDT, ID: 20141791

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.24.2007 at 02:01PM PDT, ID: 20142883

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.24.2007 at 03:11PM PDT, ID: 20143332

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.24.2007 at 03:26PM PDT, ID: 20143416

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.24.2007 at 05:54PM PDT, ID: 20144178

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, SQL Query Syntax
Tags: update, sql, insert, query, select
Sign Up Now!
Solution Provided By: Zberteoc
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20081112-EE-VQP-43 - Hierarchy / EE_QW_2_20070628