0

我继承了 PostgreSQL 9.2.4 数据库,虽然我在 SQL Server 方面拥有相当广泛的背景,但我在解决遇到的问题时遇到了一些麻烦。

我有一张表,其中包含三个字段(除其他外)。“age_years”、“age_months”和“age_days”。如果表中的某个人是 2 个月大或更小,那么他们在“age_days”字段中的值是他们的天数。如果他们不到 3 岁但超过 2 个月,那么他们在“age_months”字段中有一个值。任何超过 3 岁且在“age_years”字段中有值的东西。

给定记录仅在这三个字段之一中具有非零值。例如,永远不会出现 age_days 和 age_years 都具有非零值的情况。这些记录代表医院就诊,年龄是就诊时个人的年龄。

在另一个表中,我有几个字符可变 [] 字段,最多 20 个值。它们是 ref_age_cd、ref_age、ref_clow 和 ref_chigh。这是该表中的示例记录(仅出于显示目的,值少于最大值):

对于下面的丑陋线条,我深表歉意。我似乎无法让它们以非常易读的条件格式化。

ref_age_cd | ref_age | ref_clow | ref_chigh

[D,D,D,M,M,Y,Y,Y]   [1,4,15,2,7,13,18,199]  [9.1,9.8,5.4,5.5,7.9,5.1,4.8,4.8]   [27.1,27.8,16.4,15.8,15.9,11.1,10.8,10.8]

ref_age_cd 字段确定您正在查看的年龄类型(天、月或年)。ref_age 确定值,然后根据这两个值,您可以从 ref_clow 和 ref_chigh 字段中获取低值和高值。例如,如果某人在 age_months 字段中有 13,那么您将查看 ref_age_cd 并在数组中找到“M”值,然后查看相应的 ref_age 字段并找到低于年龄月字段。所以数组索引将是 5。然后你在 ref_clow 和 ref_chigh 字段中获取第五个值作为低值和高值。(分别为 7.9 和 15.9)

如果某人 10 天大,则要查看的数组索引为 2(ref_age_cd 为 'D' 和 ref_age 为 4)。这将指示 9.8 和 27.8 的低值和高值。如果他们是 80 岁,则索引将为 7(“Y”的 ref_age_cd 和 18 的 ref_age)。4.8 和 10.8 的低值和高值。

我只是不知道如何编程,所以当我从表 A(带有age_days、age_months 或 age_years 字段)加入参考表时,我可以为 ref_clow 和 ref_chigh 提取正确的数组索引。

我还应该提到,我无法对此数据库进行任何更改。我需要用我得到的东西来完成这项工作。

4

2 回答 2

0

这最终成功了。发布以便其他人可以使用它。

--test data in first two "with" statements
with a AS (
  select 1 AS patient_nr, CAST(2 AS INT) AS age_days, CAST(NULL AS INT) AS age_months, CAST(NULL AS INT) AS age_years
  UNION ALL
  select  2 AS patient_nr, CAST(16 AS INT) AS age_days, CAST(NULL AS INT) AS age_months, CAST(NULL AS INT) AS age_years
  UNION ALL
  select  3 AS patient_nr, CAST(NULL AS INT) AS age_days, CAST(13 AS INT) AS age_months, CAST(NULL AS INT) AS age_years
  UNION ALL
  select  4 AS patient_nr, CAST(10 AS INT) AS age_days, CAST(NULL AS INT) AS age_months, CAST(NULL AS INT) AS age_years
  UNION ALL
  select  5 AS patient_nr, CAST(NULL AS INT) AS age_days, CAST(NULL AS INT) AS age_months, CAST(80 AS INT) AS age_years
), b as (
  SELECT ARRAY['D','D','D','M','M','Y','Y','Y'] AS ref_age_cd
       , ARRAY[1,4,15,2,7,13,18,199] AS ref_age
       , ARRAY[9.1,9.8,5.4,5.5,7.9,5.1,4.8,4.8] AS ref_clow
       , ARRAY[27.1,27.8,16.4,15.8,15.9,11.1,10.8,10.8] AS ref_chigh
), refTable AS (
SELECT unnest(ref_age_cd) ref_age_cd
 , unnest(ref_age) ref_age
 , unnest(ref_clow) ref_clow
 , unnest(ref_chigh) ref_chigh
  FROM b
), res AS (
SELECT A.*, rt.*, ROW_NUMBER() OVER(PARTITION BY patient_nr ORDER BY ref_age DESC) AS rn
  FROM A
  LEFT JOIN refTable rt ON (rt.ref_age_cd = 'D' AND a.age_days > rt.ref_age)
                        OR (rt.ref_age_cd = 'M' AND a.age_months > rt.ref_age)
                        OR (rt.ref_age_cd = 'Y' AND a.age_years > rt.ref_age)
 )
 SELECT * 
   FROM res
  WHERE rn = 1
于 2017-09-06T17:45:05.280 回答
0

对于单个患者,请尝试以下操作:

/* Creating test environment
CREATE TABLE refs (
  id serial NOT NULL,
  ref_age_cd character(1)[],
  ref_age integer[],
  ref_clow double precision[],
  ref_chigh double precision[],
  CONSTRAINT refs_pkey PRIMARY KEY (id)
);
INSERT INTO refs(ref_age_cd, ref_age, ref_clow, ref_chigh)
       VALUES ('{"D","D","D","M","M","Y","Y","Y"}',
               '{1,4,15,2,7,13,18,199}',
               '{9.1,9.8,5.4,5.5,7.9,5.1,4.8,4.8}',
               '{27.1,27.8,16.4,15.8,15.9,11.1,10.8,10.8}');
CREATE TABLE pats (
  id serial NOT NULL,
  name varchar(255) NOT NULL,
  age_years integer,
  age_months integer,
  age_days integer,
  CONSTRAINT pats_pkey PRIMARY KEY (id)
);
INSERT INTO pats
       VALUES (DEFAULT, 'newborn', NULL, NULL, 10),
              (DEFAULT, 'baby', NULL, 13, NULL),
              (DEFAULT, 'adult', 80, NULL, NULL);
*/

-- Replace filters here to select only one row...
WITH tt AS ( SELECT * FROM refs WHERE id = 1 )
SELECT w.*, ref_clow, ref_chigh
FROM ( SELECT row_number() OVER () AS nr, unnest AS ref_age_cd
       FROM UNNEST( (SELECT ref_age_cd FROM tt ) ), tt ) q1
JOIN ( SELECT row_number() OVER () AS nr, unnest AS ref_age
       FROM UNNEST( (SELECT ref_age FROM tt ) ), tt ) q2 USING ( nr )
JOIN ( SELECT row_number() OVER () AS nr, unnest AS ref_clow
       FROM UNNEST( (SELECT ref_clow FROM tt ) ), tt ) q3 USING ( nr )
JOIN ( SELECT row_number() OVER () AS nr, unnest AS ref_chigh
       FROM UNNEST( (SELECT ref_chigh FROM tt ) ), tt ) q4 USING ( nr )
JOIN ( SELECT id, name, age_years, age_months, age_days,
              CASE WHEN age_years IS NOT NULL THEN 'Y'
                   WHEN age_months IS NOT NULL THEN 'M'
                   WHEN age_days IS NOT NULL THEN 'D' END AS ref_age_cd,
              CASE WHEN age_years IS NOT NULL THEN age_years
                   WHEN age_months IS NOT NULL THEN age_months
                   WHEN age_days IS NOT NULL THEN age_days END AS age
       -- Replace filters here to select only one row...
       FROM pats WHERE id = 2
     ) w USING (ref_age_cd)
WHERE ref_age <= age
ORDER BY ref_age DESC
LIMIT 1;

输出:

2;"baby";<NULL>;13;<NULL>;"M";13;7.9;15.9
于 2017-09-06T08:21:59.217 回答