在postgreSQL

时间:2016-10-11 22:54:27

标签: sql json postgresql

我用很少的,没有文档来继承这个数据库。因此,我不确定下面字段中存储的数据是否为JSON,但对我来说它看起来像一个数组。这是表中一条记录的输出,这是我试图解析的记录。

  • 表:事件
  • 列/字段:会话

以下数据显示在“会话”单元格的“事件表”中的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';
  

错误:函数json_array_length(jsonb)不存在   第3行:交叉连接lateral generate_series(0,json_array_length(e ....   提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。   查询失败   PostgreSQL说:函数json_array_length(jsonb)不存在   提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。

我尝试做的甚至可能吗?

数据块:

    [
  {
    "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 个答案:

答案 0 :(得分: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;