From: | Bruno Wolff III <bruno(at)wolff(dot)to> |
---|---|
To: | Alain <arwebmail(at)skynet(dot)be> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: How do write a query... |
Date: | 2005-06-08 14:47:41 |
Message-ID: | 20050608144741.GB22888@wolff.to |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Please keep replies copied to the list unless you have a specific reason not
to. This will help you get help and will help other people learn from
the discussion.
On Wed, Jun 08, 2005 at 13:56:36 +0200,
Alain <arwebmail(at)skynet(dot)be> wrote:
>
> In fact, these are results of analyses. For one patient and one type of
> meseare, I can have results at different dates and I need to follow the
> differences between dates. So, if I have 5 records for one person, I'll
> have 4 values of differences (record2-rec1, rec3-rec2, rec4-rec3,
> rec5-rec4). Problem is to create the pairs from the records I have. I
> can do it with an algorithm. But a query - if possible - returning the
> number of days and the differences between the values would be nicer.
This is actually easier. You can write a subquery that gets the row that
has the greatest date less than the current date. With an index on Num
and the date column it shouldn't be too slow.
Below is an example test script and the output. This relies on Postgres
supprting ORDER BY in subselects which isn't standard SQL. You can do this
with standard SQL but that will require getting the max value of day less
than the day in the current record with a matching num and then joining
that result back to test to get the corresponding value.
DROP TABLE test;
CREATE TABLE test (
id SERIAL PRIMARY KEY,
num INT NOT NULL,
day DATE NOT NULL,
value INT NOT NULL,
UNIQUE (num, day)
);
INSERT INTO test (num, day, value) VALUES (10, '2005-01-01', 50);
INSERT INTO test (num, day, value) VALUES (10, '2005-05-31', 60);
INSERT INTO test (num, day, value) VALUES (25, '2005-02-02', 55);
INSERT INTO test (num, day, value) VALUES (25, '2005-03-15', 43);
INSERT INTO test (num, day, value) VALUES (25, '2005-05-28', 62);
SELECT num, day, value,
value - (SELECT value FROM test
WHERE
num = a.num
AND
day < a.day
ORDER BY num DESC, day DESC
LIMIT 1)
FROM test a
WHERE
(SELECT value FROM test
WHERE
num = a.num
AND
day < a.day
ORDER BY num DESC, day DESC
LIMIT 1)
IS NOT NULL
ORDER BY num, day
;
bruno=> \i test.sql
DROP TABLE
psql:test.sql:8: NOTICE: CREATE TABLE will create implicit sequence "test_id_seq" for serial column "test.id"
psql:test.sql:8: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
psql:test.sql:8: NOTICE: CREATE TABLE / UNIQUE will create implicit index "test_num_key" for table "test"
CREATE TABLE
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
num | day | value | ?column?
-----+------------+-------+----------
10 | 2005-05-31 | 60 | 10
25 | 2005-03-15 | 43 | -12
25 | 2005-05-28 | 62 | 19
(3 rows)
From | Date | Subject | |
---|---|---|---|
Next Message | KÖPFERL Robert | 2005-06-08 15:23:07 | Re: Cursor need it? |
Previous Message | Lucas Hernndez | 2005-06-08 14:44:27 | Cursor need it? |