2016-10-11 31 views
0

我已經繼承了這個數據庫很少,沒有文檔。所以,我不確定存儲在下面字段中的數據是否是JSON,但它看起來像一個數組。以下是表格中一條記錄的輸出,這是我試圖解析的記錄。解析看起來像在PostgreSQL中的數組的數據

  • 表:活動
  • 列/字段:下面會話

的數據顯示在1個記錄,在事件表,在會話細胞。有關數據庫表格的示例,請參見see here。我正在嘗試編寫一個查詢,它返回下面的數組中的每個ID,以便我可以將該ID加入另一個表以查詢有關該記錄的更多詳細信息。首先,我只想要一個顯示每個ID的查詢。然後,我打算編寫一個加入查詢來查詢另一個表,以便我可以報告更多加入該ID的信息。

我已經試過這一點,但它不工作,它只是返回NULL:

select (sessions::json ->> 'id')::int as session_id 
from "1".events e 
where e.id = '9040c7c3-3396-4a68-9bb7-0118afe0276d'; 

我已經試過這一點,但它不工作,它拋出一個錯誤:

select e.sessions -> x.idx 
from "1".events e 
    cross join lateral generate_series(0, json_array_length(e.sessions) - 1) as x(idx) 
where (e.sessions -> x.idx) ->> 'id' = '9040c7c3-3396-4a68-9bb7-0118afe0276d'; 

ERROR: function json_array_length(jsonb) does not exist LINE 3: cross join lateral generate_series(0, json_array_length(e.... HINT: No function matches the given name and argument types. You might need to add explicit type casts. Query failed PostgreSQL said: function json_array_length(jsonb) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts.

正是我試圖做甚至可能嗎?

數據塊:

[ 
    { 
    "id": "12dfc464-f998-49c7-9768-cdcd2f3e3d83", 
    "name": "Registration & Breakfast", 
    "room": "26722045-596d-4e61-ad67-b363d6d32c79", 
    "type": "Regular", 
    "track": "1c4c1367-3fbf-4108-9750-fe40ff144486", 
    "venue": null, 
    "endDate": "2016-10-13", 
    "endHour": "09:30", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "09:00", 
    "description": "", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "01bd62b4-4c3a-4901-a27d-4b1c8bc26eb9", 
    "name": "Welcome & ABC Solutions Roadmap", 
    "room": "", 
    "type": "Regular", 
    "track": "686cedbb-b03a-4984-914f-accb2f5b1de8", 
    "endDate": "2016-10-13", 
    "endHour": "11:00", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "09:30", 
    "description": "From Irish Mcintyre, Steve Smith & Laurence Kiddle - Acme Corp", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "1fb29a8b-e596-49c0-ae97-af7057af6575", 
    "name": "Refreshment Break", 
    "room": "26722045-596d-4e61-ad67-b363d6d32c79", 
    "type": "Regular", 
    "track": "1c4c1367-3fbf-4108-9750-fe40ff144486", 
    "venue": null, 
    "endDate": "2016-10-13", 
    "endHour": "11:30", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "11:00", 
    "description": "", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "902fbd77-039d-4899-ab45-8c1d5d98c949", 
    "name": "Direct Tax Integration and Automation", 
    "room": "", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "12:30", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "11:30", 
    "description": "From Mark Hart - Acme Corp & Anthony Davis - EY", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "51801307-161b-45ef-96f5-2b3942ef6adc", 
    "name": "Streamlining Tax Data ManagementInformal roundtable where users can discuss current and future developments with our Digita Company Secretarial product specialists.", 
    "room": "92134b87-e4d7-47f6-9681-cedf0ba59474", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "venue": null, 
    "endDate": "2016-10-13", 
    "endHour": "12:30", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "11:30", 
    "description": "From Matt Balls - Acme Corp & 
Stef Merks, Tim Van Uden - ATC 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "f41fc3bd-c5a3-475a-a2a5-735651d8d239", 
    "name": "Managing Your Indirect Tax Risk Profile Globally", 
    "room": "", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "12:30", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "11:30", 
    "description": "From Kim Hau - Acme Corp & 
Jilly McCullagh, Stuart Martin - Deloitte 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "72335a6f-e797-44d7-81ae-0ea116a01aa5", 
    "name": "Solution Showcase - ABC Tax Provision", 
    "room": "c295f0cc-5b9d-4376-a694-341599d022e9", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "12:30", 
    "pollURL": "", 
    "speakers": {}, 
    "startDate": "2016-10-13", 
    "startHour": "11:30", 
    "attachments": {}, 
    "description": "<p>From James Byrne/Bilal Mansoor - Acme Corp</p> 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "10a37efb-0893-47bc-bc50-aea1160a876a", 
    "name": "Solution Showcase - ABC Transfer Pricing", 
    "room": "0e0cc3df-68b9-4bfa-aded-db68572f4ac0", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "12:30", 
    "pollURL": "", 
    "speakers": {}, 
    "startDate": "2016-10-13", 
    "startHour": "11:30", 
    "attachments": {}, 
    "description": "<p>From Thembani Mtetwa - Acme Corp</p> 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "4030af86-f1c0-434d-ad60-985a92e65a12", 
    "name": "Lunch", 
    "room": "26722045-596d-4e61-ad67-b363d6d32c79", 
    "type": "Regular", 
    "track": "1c4c1367-3fbf-4108-9750-fe40ff144486", 
    "venue": null, 
    "endDate": "2016-10-13", 
    "endHour": "13:15", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "12:30", 
    "description": "", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "2e7e21b3-f16f-4a91-ae77-344389121999", 
    "name": "Guest Speaker - The Future of The Professions", 
    "room": "", 
    "type": "Regular", 
    "track": "686cedbb-b03a-4984-914f-accb2f5b1de8", 
    "endDate": "2016-10-13", 
    "endHour": "14:15", 
    "pollURL": "", 
    "speakers": ["45a42e2b-7e0b-49d9-b5b9-cfb2440005ed"], 
    "startDate": "2016-10-13", 
    "startHour": "13:15", 
    "description": "Daniel Susskind has also co-authored 'The Future of the Professions' which explains how \"increasingly capable systems\" - from telepresence to artificial intelligence - will bring fundamental change in the way that the expertise of specialists is made available in society. In an Internet society, he suggests, we will neither need nor want doctors, teachers, accountants, architects, the clergy, consultants, lawyers, and many others, to work as they did in the 20th century. Based on his in-depth research of more than ten professions, and illustrated by numerous examples from each, Daniel assesses and questions the relevance of the professions in the 21st century.", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "78e218ac-4172-4b1a-be9f-d50d4942110f", 
    "name": "BEPS Update - Regulations and Technology Solutions", 
    "room": "", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "15:15", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "14:15", 
    "description": "From Ayomi Wickramasinghe - Acme Corp & Paul Daly - BDO", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "0c8a78cf-fd96-4125-8163-9a5a37abd44b", 
    "name": "Managing Tax and Compliance in Shared Service Centres Informal roundtable where users can discuss current and future developments with our Digita Company Secretarial product specialists.", 
    "room": "92134b87-e4d7-47f6-9681-cedf0ba59474", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "15:15", 
    "pollURL": "", 
    "speakers": {}, 
    "startDate": "2016-10-13", 
    "startHour": "14:15", 
    "attachments": {}, 
    "description": "<p>From Joe Spadaro - Acme Corp & James Tooley, Demian De Souza - Deloitte</p> 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "a1df50f9-875e-4932-99ce-067e6c24cf51", 
    "name": "ABC FATCA/CRS Reporting ", 
    "room": "", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "15:15", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "14:15", 
    "description": "From Mark Wilton & Dickson Alfred - Acme Corp", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "29fa0ea9-7c3a-40f3-9fa2-ea5e320c61a4", 
    "name": "Solution Showcase - ABC Workflow Manager", 
    "room": "c295f0cc-5b9d-4376-a694-341599d022e9", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "15:15", 
    "pollURL": "", 
    "speakers": {}, 
    "startDate": "2016-10-13", 
    "startHour": "14:15", 
    "attachments": {}, 
    "description": "<p>From Charles Jaeger - Acme Corp</p> 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "b96b74a1-1660-46fa-8ab6-1fdc6ff3fa17", 
    "name": "Solution Showcase - ABC Indirect Tax Compliance", 
    "room": "0e0cc3df-68b9-4bfa-aded-db68572f4ac0", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "venue": null, 
    "endDate": "2016-10-13", 
    "endHour": "15:15", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "14:15", 
    "description": "From Laurence Pollinsky - Acme Corp", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "0c932792-8baf-4dfd-8390-66c08734ff4c", 
    "name": "Refreshment Break", 
    "room": "26722045-596d-4e61-ad67-b363d6d32c79", 
    "type": "Regular", 
    "track": "1c4c1367-3fbf-4108-9750-fe40ff144486", 
    "venue": null, 
    "endDate": "2016-10-13", 
    "endHour": "15:45", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "15:15", 
    "description": "", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "b1e6129d-7ae9-4bdd-b00e-ce7914a00535", 
    "name": "Automating Tax Provision ", 
    "room": "", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "16:45", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "15:45", 
    "description": "From James Byrne - Acme Corp & Richard Scammell and Severin Petri - PWC", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "62932cdb-61dd-4299-b8ea-e3ad00754417", 
    "name": "How to Build a Business Case for Tax Technology and Transformation Projects", 
    "room": "92134b87-e4d7-47f6-9681-cedf0ba59474", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "venue": null, 
    "endDate": "2016-10-13", 
    "endHour": "16:45", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "15:45", 
    "description": "From Joe Spadaro - Acme Corp & Dan Cowan - Accenture", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "7c74007d-8fe2-4b73-a80d-fdf5c4f21653", 
    "name": "Managing Complex Supply Chains and Global Trade Management", 
    "room": "", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "16:45", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "15:45", 
    "description": "From Kim Hau - Acme Corp & 
Bob Jones, Kirsty Rockall - KPMG 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "c6121153-357f-4caf-8263-d623530004e7", 
    "name": "Solution Showcase - ABC Accounts Production", 
    "room": "c295f0cc-5b9d-4376-a694-341599d022e9", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "16:45", 
    "pollURL": "", 
    "speakers": {}, 
    "startDate": "2016-10-13", 
    "startHour": "15:45", 
    "attachments": {}, 
    "description": "<p>From Shahina Mostafa - Acme Corp</p> 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "6b9e215d-8928-4d1e-8767-1c77543324a9", 
    "name": "Solution Showcase - ABC Audit Manager", 
    "room": "0e0cc3df-68b9-4bfa-aded-db68572f4ac0", 
    "type": "Regular", 
    "track": "962db150-736e-4076-87a3-c8bedd3fcb48", 
    "endDate": "2016-10-13", 
    "endHour": "16:45", 
    "pollURL": "", 
    "speakers": {}, 
    "startDate": "2016-10-13", 
    "startHour": "15:45", 
    "attachments": {}, 
    "description": "<p>From Charles Jaeger - Acme Corp</p> 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "83fd7d79-6e53-49ee-8a0c-b5f7ea6fde85", 
    "name": "Implementing BEPS and Country By Country Reporting Panel Discussion", 
    "room": "", 
    "type": "Regular", 
    "track": "686cedbb-b03a-4984-914f-accb2f5b1de8", 
    "endDate": "2016-10-13", 
    "endHour": "17:30", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "16:45", 
    "description": "From Michal Chelpinski - Veritas, Wendy Nichols - Grant Thornton, Malcolm Joy - BDO 
Fabrizio Lolliri - Hogans Lovells 
Danny Gal - Acme Corp 
Laurence Kiddle (Moderator) - Acme Corp 
", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "f68dd3be-7a7f-4d92-ad4f-d21240eb044a", 
    "name": "Closing Remarks", 
    "room": "", 
    "type": "Regular", 
    "track": "686cedbb-b03a-4984-914f-accb2f5b1de8", 
    "endDate": "2016-10-13", 
    "endHour": "17:35", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "17:30", 
    "description": "From Laurence Kiddle - Acme Corp", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    }, 
    { 
    "id": "ae699855-61cc-4cce-b8ad-e8d243096fd6", 
    "name": "Cocktails", 
    "room": "26722045-596d-4e61-ad67-b363d6d32c79", 
    "type": "Regular", 
    "track": "1c4c1367-3fbf-4108-9750-fe40ff144486", 
    "venue": null, 
    "endDate": "2016-10-13", 
    "endHour": "19:00", 
    "pollURL": "", 
    "speakers": "", 
    "startDate": "2016-10-13", 
    "startHour": "17:35", 
    "description": "", 
    "feedbackEnabled": true, 
    "showInActivities": true 
    } 
] 
+1

顯然你的列是一個'jsonb'列。所以你需要使用'jsonb_array_length()'而不是'json_array_length()' –

+0

好消息,沒有更多的錯誤!壞消息是,我需要的數據還沒有回來。例如,請查看此處隔離的ID嗎?這是我希望查詢返回的唯一的事情。 [示例](https://dl.dropboxusercontent.com/u/18312/Screen%20Shot%202016-10-12%20at%204.56.59%20PM.png)@a_horse_with_no_name –

回答

0
select s::jsonb->>'id' 
from (select jsonb_array_elements(sessions) as s 
from "1".events where id='517cbd38-7ced-4617-a46e-79866c89a877') as t; 
相關問題