2017-03-17 59 views
0

的值我的當前sae_table(id,cbid,description,value)如圖所示。表的交叉表功能沒有攜帶沿着

我想轉動它,所以它可以是這樣的:

id  cbid month day year test actual_value normal_ran no 
1 60051  09  27 2016 "Urinary" "some vegetans" 
2 60051  09  30 2016 "Chest" 
3 60052 .... 

我已經tryied做用ID,說明和值交叉表,但所有的值只有每月欄下顯示。

SELECT * FROM CROSSTAB('SELECT id, description,value from sae_test') 
AS ct ("id" integer, "Month" character varying(4000),"Day" character varying(4000),"Year" character varying (4000), 
"Test" character varying(4000),"Actual Value" character varying(4000),"Normal Range" character varying(4000),"No Test option" character varying(4000)); 

以上交叉結果(值不正確分配翻過列):

id Month Day Year  ... 
1  09      ... 
2  27      ... 
3  2016     ... 

我也嘗試使用CBID,說明和值擺動而已。但它只顯示獨特的cbids。在這種情況下,一個cbid可以有多行。

SELECT * FROM CROSSTAB('SELECT * from sae_rel_data2()') 
AS ct ("CBID" character varying(4000), "Month" character varying(4000),"Day" character varying(4000),"Year" character varying (4000), 
"Test" character varying(4000),"Actual Value" character varying(4000),"Normal Range" character varying(4000),"No Test" character varying(4000)); 

用於查詢上面(省去了相同的CBID第二條目,當那些條目應一直保持)其結果是:

cbid month day year ... 
60051 09  27  2016 ... 
60052       ... 
60053 09  27  2016 ... 
60029       ... 

enter image description here

更新:

如果我有可幫助識別CBID的第n個記錄的序數,該怎麼辦?然後,我可以創建一個循環函數,它將爲每個序號級別的cbid執行交叉表,然後將每個結合UNION或JOIN語句?這會起作用嗎?如果是這樣,該循環如何創建?我不熟悉它。

例子:

event_crf_id;  description,   value,  ordinal 
444;     "CBID";    "60051";   1 
444;     "Month";    "09";    1 
444;     "Day";    "27";    1 
444;     "Year";    "2016";   1 
444;     "Test";   "Urinary tract US";  1 
444;    "Actual Value"; "some vegetans lesions"; 1 
444;     "Normal Range";   "";    1 
444;    "No tests option";  "";    1 
444;     "Month";    "09";   2 
444;     "Day";     "30";   2 
444;     "Year";    "2016";   2 
444;     "Test";    "Chest/abdomen CT"; 2 
444;    "Actual Value";   "3 bladder lesions"; 2 
444;    "Normal Range";    "";   2 
444;    "No tests option";   "";   2 

喜歡的東西:

count=count (distinct ordinal) from sae_test() 
for each event_crf_id in (select * from sae_test() where ordinal=count) 
    SELECT * FROM CROSSTAB('SELECT event_crf_id, description, value from sae_test()) 
JOIN ... 
count=count+1 

有沒有這種可能性?這個連接怎麼執行?或者postgres自動知道,在循環中新條目將繼續添加到表? (對不起,我對Postgres和數據庫來說真的很陌生)

+0

您的數據不明確。行'60051'和列'Day'應該顯示什麼值?應該是「27」還是「30」? (順便說一句,以文本形式提供數據,不提供圖像)。 – klin

+0

應該有一個獨特的值來確定哪個日期或測試或「實際值」是2. 3.或第n個60051的一部分?第二個問題:如果這些值以未排序的方式插入,如何分隔每個60051行組? –

+0

就這樣想,60051代表一個病人,那個病人在不同的日期可以有多個實驗室。這就是爲什麼你反覆看到60051的原因。所以一個cbid可以有多個條目,但每個條目都有一個唯一的id。但是,爲什麼當我執行SELECT * FROM CROSSTAB('SELECT id,description,value from sae_test')時,所有數據都顯示在第二列之下? –

回答

0

我從字面上不得不做這個艱難的,不成熟的方法。但它確實爲我提供了我想要的結果。我如何以更復雜,更高效的方式來做到這一點?

Select a.event_crf_id, rel_test_CBID, a.row_number, a.rel_test_name, rel_test_actual_value, rel_test_month, rel_test_day, rel_test_year, rel_test_normal_range From (Select id.event_crf_id, id.ordinal as row_number, id.value as rel_test_name 
from item i, item_data id 
where i.item_id=id.item_ID and id.item_id IN (2185,2262,2263,2264,2265,2266,2267,2268) and i.description = 'Test') as a 

left join (Select id.event_crf_id, id.ordinal as row_number, id.value as rel_test_actual_value 
from item i, item_data id 
where i.item_id=id.item_ID and id.item_id IN (2185,2262,2263,2264,2265,2266,2267,2268) and i.description = 'Actual Value') as b 
on a.event_crf_id = b.event_crf_id and a.row_number = b.row_number 

left join (Select id.event_crf_id, id.ordinal as row_number, id.value as rel_test_month 
from item i, item_data id 
where i.item_id=id.item_ID and id.item_id IN (2185,2262,2263,2264,2265,2266,2267,2268) and i.description = 'Month') as c 
on a.event_crf_id = c.event_crf_id and a.row_number = c.row_number 

left join (Select id.event_crf_id, id.ordinal as row_number, id.value as rel_test_day 
from item i, item_data id 
where i.item_id=id.item_ID and id.item_id IN (2185,2262,2263,2264,2265,2266,2267,2268) and i.description = 'Day') as d 
on a.event_crf_id = d.event_crf_id and a.row_number = d.row_number 

left join (Select id.event_crf_id, id.ordinal as row_number, id.value as rel_test_year 
from item i, item_data id 
where i.item_id=id.item_ID and id.item_id IN (2185,2262,2263,2264,2265,2266,2267,2268) and i.description = 'Year') as e 
on a.event_crf_id = e.event_crf_id and a.row_number = e.row_number 

left join (Select id.event_crf_id, id.ordinal as row_number, id.value as rel_test_normal_range 
from item i, item_data id 
where i.item_id=id.item_ID and id.item_id IN (2185,2262,2263,2264,2265,2266,2267,2268) and i.description = 'Normal Range') as f 
on a.event_crf_id = f.event_crf_id and a.row_number = f.row_number 

left join (Select id.event_crf_id, id.ordinal as row_number, id.value as rel_test_CBID 
from item i, item_data id 
where i.item_id=id.item_ID and id.item_id IN (2185,2262,2263,2264,2265,2266,2267,2268) and id.ordinal = 1 and i.description = 'CBID') as g 
on a.event_crf_id = g.event_crf_id 

order by a.event_crf_id, a.row_number asc; 
+0

用這兩個表中的'insert into from yourtable'將您的示例數據[hier](http://rextester.com/l/postgresql_online_compiler)。 –