SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


code to multiply dose by times administered


code to multiply dose by times administered

Author
Message
prb88
prb88
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 50
Hey Guys

I have to create a report which shows how many times a drug was administered and the dose is multiplied by the number of times administered (i.e 20mg dose x 2 times administered = 40 mg). The dose field is derived from a table. The table used to create the report from contains details about the the PatientNo., Dose, time prescribed, time administered.

The report needs to look like this:

PATIENT NO. | Number of Times Administred | DOSE
Patient A 2 40
Patient B 10 200

The parameters of the report is a startdate and an enddate.

I need help with SQL code to multiply the dose by times administered and identify how many times each patient no. has had the drug and to multiply that to get the value shown in the dose field.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66028 Visits: 17980
Hi and welcome to SSC. It is nearly impossible to offer much assistance based on the details in your post. I am willing to help but I need something to work with. Please take a few minutes and read the article found at the first link in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
peterzeke
peterzeke
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2082 Visits: 1766
Couple of quick thoughts for you:

1) The key to making this work is to strip the unit of measure from the initial dose. For example, if a drug dose is "10 mg" taken twice, then you first need to strip "mg" from "10 mg". You'll be left with the 10 which you can then multiply against 2 to get 20. All of this parsing should be done in SQL (i.e., in the dataset).

2) Since you didn't indicate if your dataset contains different types of drugs with different types of units of measure, I"ll assume for now that "mg" is your only unit of measure. To strip "mg" from "10 mg" just use the sql replace command to eliminate "mg" with a zero-length string: Replace(drugdose,'mg','') * NumberOfTimesTaken = total dosing. If you have different units of measure, then nest your replace statements for each unit of measure.

3) Things get much trickier if a given drug has multiple active ingredients, suchas "10mg;5mg" -- in this scenario, the drug has a total of 15mg, but the information is split between two active ingredients. A much more elaborate parsing technique is required, which I won't go into right now.

--Pete



prb88
prb88
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 50
okay many thanks for your suggestions.
davoscollective
davoscollective
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1745 Visits: 1008
Just a note on units of measurement.

I have previously worked with many laboratory databases and there is always need for a unit conversion look up table for any sort of calculations.

Contents would look like this
SourceUnits ConvertedUnits Multiplier
mg g 0.001
g mg 1000

The use of a standard conversion table like this avoids maintaining multiple calculations elsewhere in the database.

Calculations should be converted to standard units, performed, and then converted to display units, and the very last operation performed should be any rounding if required. Particularly with chemistry or pharmaceuticals this is very important.

The fields in the rest of the database should not contain the values & units in the same column, fields should not contain multiple pieces of data as per best practice, which means you avoid any parsing to strip out units.
Dave
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search