Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...keep up the good work with this forum, I think this is the best one around. ...you actually try to help people learn for themselves. ...I commend you on providing a very good, open learning atmosphere, where usually egos are left behind..."

Geography

Where in the world do Tek-Tips members come from?

Help please returning value from row below into top row

FB1 (Instructor)
9 Jun 12 4:29
Hi everyone, newbie to this as you can see from my last correspondence.

have a table which show rows 'patient id', 'date' , 'weight', 'post or pre'

need to do a query that show last date entry, pre weight at the time of that entry, also the post weight of the entry before, then I can calculate weight +/-.

I will need to show the last 12 entries for that patient. the paient gets weighed 2 in a day, pre before treatment and post after treatment. therefore it will look at 25 entries to do teh calculation.

Your help is most appreciated

Ralph

feherke (Programmer)
9 Jun 12 7:34
Hi

You mean, the date field should have the same value in both the pre and post records ?

Given that you wrote "25 entries", I suppose you also want the last pre measurement which has no post pair yet. In such circumstances I prefer to use 2 sub-selects :

CODE --> MySQL

select
date,pre.weight,post.weight,pre.weight-post.weight

from (
select
*
from f1

where post_or_pre='pre'
and patient_id=1

order by date desc

limit 12
) pre
left join (
select
*

from f1

where post_or_pre='post'
and patient_id=1
) post using (patient_id,date)

order by date

Feherke.
http://feherke.github.com/

FB1 (Instructor)
11 Jun 12 5:23
Hi Feherke

This is my Query for pulling information out
select
oid as recid,
fk_oid as patientid,
DIDAT as date,
prepost,
DIWT as weight,
DIBPS, DIBPD, DIGAIN, DIIW, DIHEIGHT, sp_bmi(oid) as BMI


from p_anthropometry
where fk_oid = '3'

order by fk_oid, didat desc
this gives me
recid ,fk_oid, date , Prepost , weight
'188856', '3', '1982-02-26 00:00:00', '200100', '71.8', '150', '60', NULL, NULL, NULL, NULL
'188855', '3', '1982-02-26 00:00:00', '200101', '69.3', '120', '60', NULL, NULL, NULL, NULL
'188858', '3', '1982-02-24 00:00:00', '200100', '72.3', '145', '65', NULL, NULL, NULL, NULL
'188857', '3', '1982-02-24 00:00:00', '200101', '70.5', '120', '60', NULL, NULL, NULL, NULL
'188859', '3', '1982-02-22 00:00:00', '200101', '71.1', '120', '60', NULL, NULL, NULL, NULL
'188860', '3', '1982-02-22 00:00:00', '200100', '73.3', '145', '60', NULL, NULL, NULL, NULL
'188862', '3', '1982-02-19 00:00:00', '200100', '73.4', '150', '60', NULL, NULL, NULL, NULL
'188861', '3', '1982-02-19 00:00:00', '200101', '71.3', '120', '60', NULL, NULL, NULL, NULL
'188864', '3', '1982-02-17 00:00:00', '200100', '73.5', '160', '60', NULL, NULL, NULL, NULL
'188863', '3', '1982-02-17 00:00:00', '200101', '72.1', '125', '60', NULL, NULL, NULL, NULL
'188866', '3', '1982-02-15 00:00:00', '200100', '74', '150', '60', NULL, NULL, NULL, NULL
'188865', '3', '1982-02-15 00:00:00', '200101', '71.8', '125', '60', NULL, NULL, NULL, NULL
'188868', '3', '1982-02-12 00:00:00', '200100', '74', '165', '60', NULL, NULL, NULL, NULL
'188867', '3', '1982-02-12 00:00:00', '200101', '71.8', '125', '60', NULL, NULL, NULL, NULL
'188870', '3', '1982-02-10 00:00:00', '200100', '74.2', '150', '60', NULL, NULL, NULL, NULL
'188869', '3', '1982-02-10 00:00:00', '200101', '72.3', '120', '60', NULL, NULL, NULL, NULL
'188872', '3', '1982-02-08 00:00:00', '200100', '75', '155', '60', NULL, NULL, NULL, NULL
'188871', '3', '1982-02-08 00:00:00', '200101', '73', '125', '60', NULL, NULL, NULL, NULL
'188874', '3', '1982-02-05 00:00:00', '200100', '75', '155', '60', NULL, NULL, NULL, NULL
'188873', '3', '1982-02-05 00:00:00', '200101', '73.5', '130', '60', NULL, NULL, NULL, NULL
'188876', '3', '1981-12-16 00:00:00', '200100', '74', '165', '60', NULL, NULL, NULL, NULL
'188875', '3', '1981-12-16 00:00:00', '200101', '72.8', '130', '60', NULL, NULL, NULL, NULL


200100 = 'Pre'
200101 = 'Post'

I need to show the following

'Date' 'Preweight' ' Last Post weight before preweight i.e before preday' 'difference between preweight and last post weight' 'Interval between Prewieght and last post weight dates'



so it looks like this

Date PreWt lastpostwt IDFG Interval
08/02/2012 101.6 100.0 1.6 2 days
06/02/2012 100.3 100.3 1.2 3 days
01/02/2012 101.4 99.8 1.6 2 days
to show last 6
feherke (Programmer)
11 Jun 12 6:24
Hi

CODE --> MySQL

select
pre.fk_oid,
pre.didat pre_date,
pre.diwt pre_weight,
post.didat post_date,
post.diwt post_weight,
datediff(pre.didat,post.didat) date_difference,
pre.diwt-post.diwt weight_difference

from (
select
*,
(
select
didat

from p_anthropometry

where prepost='200101'
and didat<pre.didat
and fk_oid=pre.fk_oid

order by didat desc

limit 1
) post_date

from p_anthropometry pre

where prepost='200100'

order by didat desc

limit 6
) pre
left join p_anthropometry post on post.didat=pre.post_date
and post.fk_oid=pre.fk_oid
and post.prepost='200101'

order by pre.didat

Feherke.
http://feherke.github.com/

FB1 (Instructor)
11 Jun 12 9:16
That is brilliant thank you ever so much feherke.

I need to create this as a view, because there are several sub queries, I am trying to break them down.
This is your query, change it a little to show newest date. I need to run this query against each patient, ( at the moment just going against date)
I will then join the view's and add it as cr command as a sub report
Could I please ask for you to give me a bit more help.

select pre.fk_oid,
pre.didat pre_date,
pre.diwt pre_weight,
post.didat post_date,
pre.didat,
post.diwt post_weight,
datediff(pre.didat,post.didat) date_difference,
pre.diwt-post.diwt weight_difference
from ( select *,
( select didat from p_anthropometry
where prepost='200100'
and didat<pre.didat
and fk_oid=pre.fk_oid
order by didat desc limit 1 )
post_date
from p_anthropometry pre
where prepost='200100' order by didat desc limit 12) pre
left join p_anthropometry post on post.didat=pre.post_date
and post.fk_oid=pre.fk_oid
and post.prepost='200101'
-- where pre.fk_oid = '12'
order by pre.didat desc


//I have created a view for Pre_date_hdObs
select pre.fk_oid, pre.prepost,
pre.didat pre_date,
pre.diwt pre_weight
from p_anthropometry as pre
where prepost='200100'
-- and didat<pre.didat
and fk_oid=pre.fk_oid
order by pre.didat desc

//also view for Post_date_HdObs
Select post.fk_oid, post.didat post_date,
post.diwt post_weight, prepost
from p_anthropometry as post
where post.prepost='200101'
--and post.fk_oid = '12'
order by post.didat desc

But now I am lost, I tried to do the following
SELECT pre_date_hdobs.*,
post_date_hdobs.post_date,
post_date_hdobs.post_weight,
datediff(pre_date_hdobs.Pre_date,post_date_hdobs.Post_date) as date_difference,
pre_date_hdobs.Pre_weight - post_date_hdobs.Post_weight as weight_difference
FROM pre_date_hdobs
join post_date_hdobs on post_date_hdobs.fk_oid=pre_date_hdobs.fk_oid;

Again thank you for all your help, would most appreciated a little bit more

Ralph
FB1 (Instructor)
12 Jun 12 4:39
Hi Ferherke

Manage to get it working on single patient, thank you very much for all your help.

If I wanted to group in each last Pre entry in a month and show the weight difference for each month.
what do I need to change
Date Weigth Change/Month
06/02/2012 100.0 -0.2 loss
11/01/2012 100.2 0.0 gain
16/12/2011 100.2 0.2 gain
21/11/2011 100.0 -0.2 loss
26/10/2011 100.2 -0.2 loss
26/09/2011 100.4 1.6 gain

Your help is most appreciated
Thank you once again
Ralph

your code:
select pre.fk_oid,
pre.didat pre_date,
pre.diwt pre_weight,

post.didat post_date,
pre.didat,
post.diwt post_weight,
datediff(pre.didat,post.didat) date_difference,
pre.diwt-post.diwt weight_difference
from ( select *,
( select didat from p_anthropometry
where prepost='200100'
and didat<pre.didat
and fk_oid=pre.fk_oid
order by didat desc limit 1 )
post_date
from p_anthropometry pre
where prepost='200100' and pre.fk_oid = '12'
order by didat desc limit 12) pre
left join p_anthropometry post on post.didat=pre.post_date
and post.fk_oid=pre.fk_oid
and post.prepost='200101'
and pre.fk_oid = '12'
and post.fk_oid = '12'
order by pre.didat desc

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close