为什么我的Postgres交叉表产生错误:'查询指定的返回元组有7列,但交叉表返回6'?

时间:2011-12-29 00:13:06

标签: postgresql crosstab

我正在使用Postgres 8.4.9并且无法让他们的交叉表工作。

以下是我的数据:

   rowname     |       bucket        | bucket_value 
----------------+---------------------+--------------
 CL100001 10011 | 1 A01 | 01/01/2001  | 14 16
 CL100001 10011 | 1 A01 | 01/01/2001  |  
 CL100001 10011 | 1 A01 | 01/01/2001  |  
 CL100001 10011 | 1 A01 | 01/01/2001  | 440 480
 CL100001 10011 | 1 A01 | 01/01/2001  | 475 475
 CL100002 10021 | 1 B01 | 01/01/2001  | 16 16
 CL100002 10021 | 1 B01 | 01/01/2001  |  
 CL100002 10021 | 1 B01 | 01/01/2001  |  
 CL100002 10021 | 1 B01 | 01/01/2001  | 440 480
 CL100002 10021 | 1 B01 | 01/01/2001  | 475 475
 CL100003 10030 | 1 C01 | 01/01/2001  | 14 16
 CL100003 10030 | 1 C01 | 01/01/2001  |  
 CL100003 10030 | 1 C01 | 01/01/2001  |  
 CL100003 10030 | 1 C01 | 01/01/2001  | 440 440
 CL100003 10030 | 1 C01 | 01/01/2001  | 475 475

...

这是从表ct中选择的,用于准备数据。

接下来,我的查询看起来像

SELECT gtreport.* FROM crosstab('SELECT 
    rowname, 
    bucket,
    bucket_value
FROM
    ct  
ORDER BY 
    rowname, bucket',
'SELECT DISTINCT
    markername  
FROM 
    markers M,
    genotypes G,
    gsamples S,
    guploads U
WHERE 
    M.markerid=G.markers_id
    AND G.gsamples_id=S.id 
    AND S.guploads_id=U.id 
    AND ( U.ibg_study_id=15 AND U.ibg_project_id is NULL) 
ORDER BY 
    M.markername')
AS gtreport(
 labid text, 
 box_well_run_date text, 
 HTTLPR text, 
 Amelo text,
 Caspi text,
 DAT1 text,
 DRD4 text)

我试图按照Postgres文档构建我也可以。 但这会产生错误 “查询指定的返回元组有7列,但交叉表返回6。”

令人费解的是。如果有人看到我做错了什么,我将非常感激....

- 瑞克

1 个答案:

答案 0 :(得分:0)

您的问题在结果集中找不到box_well_run_date

请改为尝试:

SELECT gtreport.* FROM crosstab('SELECT 
    rowname, 
    bucket,
    bucket_value
FROM
    ct  
ORDER BY 
    rowname, bucket',
'SELECT DISTINCT
    markername  
FROM 
    markers M,
    genotypes G,
    gsamples S,
    guploads U
WHERE 
    M.markerid=G.markers_id
    AND G.gsamples_id=S.id 
    AND S.guploads_id=U.id 
    AND ( U.ibg_study_id=15 AND U.ibg_project_id is NULL) 
ORDER BY 
    M.markername')
AS gtreport(
 labid text, 
 HTTLPR text, 
 Amelo text,
 Caspi text,
 DAT1 text,
 DRD4 text);