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 |
ramindya
Starting Member
5 Posts |
Posted - 2012-02-09 : 17:55:12
|
I am trying to design a patient and visit database.Patient table has following fields: MRN which is primary key, Patient_NameVisit Table has following fields:Visit_Number, MRNInformation:User of application is the interviewer and enters patient information for each visit as a record/row.John is patient 1 and made 2 visits.Andrew is patient 2 and made 3 visits.Visit_Number should increase from 1 for each unique patients.Expected correct result:MRN NAME Visit_Number----------------------123 John 1123 John 2456 Andrew 1456 Andrew 2456 Andrew 3Right now in SQL server after creating one to many relationship between pateint(MRN)and visit table(MRN)and setting Visit_Number as an identity with an increment of 1.My current output (wrong) is:MRN NAME Visit_Number----------------------123 John 1123 John 2456 Andrew 3456 Andrew 4456 Andrew 5Can you please let me know how to get the expected result ?Thanks in advance !!-Ram |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-09 : 18:04:48
|
just keep the identity as it is in tablebut while retrive use select likeselect p.MRN,p.Patient_Name,ROW_NUMBER() OVER (PARTITION BY p.MRN ORDER BY v.Visit_Number) AS Visit_Numberfrom Patient pinner join visits von v.MRN = p.MRN ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
ramindya
Starting Member
5 Posts |
Posted - 2012-02-09 : 18:19:00
|
Forgot to mention in my previous reply.My front end of this windows application is done with MS ACCESS 2010.It has two forms and there are many other informations fields which I havent mentioned for the simplicity.MRN information goes to the main form and the visit information goes to the Visits subform. The Visit number automatically populates ( Auto number ) in the Visit_Number field with the visit number as soon as user saves the record.There is right arrow to move to the next record both in the main form and also sub form depending on visit for the same patient or user is entering information for new patient record.Visit number is a automatically populated and incremented and user is not entering it. My apologizes if I am not clear.I am new bie to MS Access and database design.Thanks again !! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-09 : 18:30:20
|
quote: Originally posted by ramindya Forgot to mention in my previous reply.My front end of this windows application is done with MS ACCESS 2010.It has two forms and there are many other informations fields which I havent mentioned for the simplicity.MRN information goes to the main form and the visit information goes to the Visits subform. The Visit number automatically populates ( Auto number ) in the Visit_Number field with the visit number as soon as user saves the record.There is right arrow to move to the next record both in the main form and also sub form depending on visit for the same patient or user is entering information for new patient record.Visit number is a automatically populated and incremented and user is not entering it. My apologizes if I am not clear.I am new bie to MS Access and database design.Thanks again !!
Please post this in Access forums if you need access query------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
ramindya
Starting Member
5 Posts |
Posted - 2012-02-09 : 18:32:53
|
Visakh:Thanks for your reply..Is there any other way that I can populate the visit number in the frontend without using SQL query by adding additional table or additional field etc.Very flexible with re-designing the table/database.Thanks,Ram |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-09 : 18:35:46
|
quote: Originally posted by ramindya Visakh:Thanks for your reply..Is there any other way that I can populate the visit number in the frontend without using SQL query by adding additional table or additional field etc.Very flexible with re-designing the table/database.Thanks,Ram
No idea I'm not an Access expert thats why I suggested to post it in access forums for some Access guru to see and suggest a solution ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
ramindya
Starting Member
5 Posts |
Posted - 2012-02-10 : 14:59:43
|
Finally it works.Refer:http://www.dbforums.com/microsoft-access/1675537-one-many-relationship-pateint-visits.html#post6537838 |
|
|
|
|
|