473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Limiting rows utilised by a join to a single *CLOSEST* row

I'm very sorry because I suspect that this is the same-old-same-old to some of you. I did search first.

With MySQL 5.0.27, I have the query shown below. It *almost* does what I want. In fact it does exactly what I want if there is only one row in p_ferritin table. However, I am trying to limit the number of rows available to one of the tables - specifically p_ferritin. I want that table only to make available a single row (to the entire join process) where that row contains the date (FEDAT1) CLOSEST to and EARLIER than the p_haematology.HMDATE in question as the join executes. So I would expect to see my p_aematology Data varying, but I want to see p_ferritin showing a constant value until a next *closest* is detected in p_ferritin. I do hope I've explained the problem. I tried a subquery but still cannot seme to get it right. regards

Expand|Select|Wrap|Line Numbers
  1. SELECT     p_haematology.oid, p_haematology.fk_oid, p_haematology.HMDATE, p_haematology.HMHGB, p_haematology.HMMCV, p_ferritin.FEDAT1, 
  2.                       p_ferritin.FEFL1, p_medication.PCDATS, p_medication.PCDATF, p_medication.PCDRUG, p_medication.PCDOSE, p_medication.PCUNIT, 
  3.                       p_medication.PCROUT, p_medication.PCFREQ, p_medication.PCDR, p_medication.PCOTHER, p_medication.PCCREATOR, 
  4.                       p_haematologytext.annotations, p_haematologytext.fk_to_haematology
  5. FROM         p_medication INNER JOIN
  6.                       codes ON p_medication.PCDRUG = codes.proID AND codes.DictCode = 0 AND codes.AccessCode = '1' RIGHT OUTER JOIN
  7.                       p_haematology ON (p_medication.PCDATF >= p_haematology.HMDATE OR
  8.                       p_medication.PCDATF IS NULL) AND p_medication.PCDATS <= p_haematology.HMDATE AND 
  9.                       p_medication.fk_oid = p_haematology.fk_oid LEFT OUTER JOIN
  10.                       p_ferritin ON p_haematology.HMDATE >= p_ferritin.FEDAT1 AND p_haematology.fk_oid = p_ferritin.fk_oid LEFT OUTER JOIN
  11.                       p_haematologytext ON p_haematology.oid = p_haematologytext.fk_to_haematology
  12. WHERE     (p_haematology.fk_oid = 123)
  13. ORDER BY p_haematology.HMDATE DESC, p_medication.PCDATS DESC, p_medication.PCDATF, p_ferritin.FEDAT1 DESC
Sep 9 '07 #1
1 1685
mwasif
802 Expert 512MB
Use CODE tags when posting code. It makes the post clear and more understandable for others.
Sep 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alan Mailer | last post by:
Imagine I had a table called MyTable with the following values: Field1 Field2 ____ _____ A X AA R B X BB T Now, say I want to return only rows which match *BOTH* of the
3
by: Greg Neef | last post by:
I need to develop a Tab Form where selecting a particular key from an unbound combo listbox will resulting in the display one or many records from several tables, where each tab has the matching...
3
by: | last post by:
need some help with some sql I can't seem to get the syntax right. I've tried several but to no avail the one below gets me the closest but I can't get the guardians records pulled. SELECT...
10
by: Steve Atkins | last post by:
I have a large table (potentially tens or hundreds of millions of rows) and I need to extract some number of these rows, defined by an integer primary key. So, the obvious answer is select *...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
33
by: Steve | last post by:
One of our clients recently upgraded their Office version to 2003. When they tried to run our program (written in Access 2000), they ended up with the wrong data. My coworker and I have tested this...
15
by: Hexman | last post by:
Hello All, How do I limit the number of detail records selected in a Master-Detail set using SQL? I want to select all master records for a date, but only the first 3 records for the details...
17
by: pbd22 | last post by:
Hi. How does one return a range of rows. I know that "Top 5" will return rows 0 - 5 but, how do I get 6 - 10? thanks
3
by: epiphyte | last post by:
Let's say I have a table with a list of young folk (table1). This table is related to a table that lists all their booty calls (table2). A third table lists their booty calls' booty calls (table3)....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.