Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Subtract value in one record from another


Subtract value in one record from another

Author
Message
M Joomun
M Joomun
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 196
Hello,

I have a table of Sample Results. Each patient can have up to three records in this table. I differentiate them by using a variable called 'SampleType', which can be 1, 2 or 3. I also have a variable called 'HB' which holds a decimal value.

I want to subtract the HB value in SampleType = 1 from the HB value in SampleType = 3 for a particular patient. I can use a variable called 'SubjectNumber' to identify a patient.

Is there a way to perform this calculation?

Many thanks for any help.
Mark Eckeard
Mark Eckeard
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 505
Hard to say w/o your schema and sample data but would this work?

select (T1.HB-T2.HB) as CalcValue
from SampleResults T1
inner join SampleResults T2
on T1.SubjectNumber = T2.SubjectNumber
where T1.SampleType = 1
and T2.SampleType = 3

Mark



M Joomun
M Joomun
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 196
Mark Eckeard (11/26/2013)
Hard to say w/o your schema and sample data but would this work?

select (T1.HB-T2.HB) as CalcValue
from SampleResults T1
inner join SampleResults T2
on T1.SubjectNumber = T2.SubjectNumber
where T1.SampleType = 1
and T2.SampleType = 3

Mark


Thank you Mark. That worked perfectly!
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