尝试将数据从多个表拉到单个表

时间:2020-04-10 12:51:36

标签: postgresql postgresql-9.1

我试图从多个表中提取数据,但我对customfield和customfieldoption以及customfield值表感到震惊。您可以帮助查询提取数据吗?

Customfieldvalue表与jiraissue表链接,Customfield表与customfieldvalue表链接,而customfieldoption表与customfieldvalue表链接。 某些customfield值将参考customfieldvalue存储在customfieldoption表下。

表1:项目

Id  Pkey
10000   PIONEER
10001   PICOIN
10002   ATTEST
10003   DEVOPS
10004   DCP


Table2: jiraissue

Id  Project Issuenum    Summary Issuestatus Issuetype   Resolution  Priority    Created date
12000   10004    30 Test    18004   19002   20002   3   "2019-03-24 23:13:32.96+00"
12001   10002   78  Testing 18001   19004   20003   5   "2019-03-13 14:59:50.87+00"
12002   10000   89  PDF submission  18002   19001   20000   4   "2019-05-17 19:04:03.93+00"
12003   10003   20  Screenshot  18003   19003   20004   1   "2019-05-23 14:03:05.894+00"
12004   10001   65  Sorting 18000   19000   20001   5   "2019-03-24 23:33:35.419+00"


Table3: Customfieldvalue

Id  Issue   Customfield stringvalue
16000   12003   15000   "10504"
16001   12001   15004   "puppalr"
16002   12004   15003   "10106"
16003   12002   15001   “Global”
16004   12000   15002   “”

Table4: Customfield

Id  Cfname
15000   Requirement
15001   Approvals
15002   Category
15003   Team
15004   Name


Table5: label

Id  Issue   label
17000   12003   Payroll
17001   12002   Pcc
17002   12004   Iat
17003   12001   UAT
17004   12000   AWS


Table6: Issuestatus

Id  Pname
18000   ToDo
18001   Open
18002   In Progress
18003   Done
18004   Completed


Table7: Issuetype

Id  pname
19000   Task
19001   Bug
19002   Epic
19003   Story
19004   Sub-task


Table8: Resolution

Id  Pname
20000   Done
20001   Won’t Do
20002   Duplicate
20003   Declined
20004   Cannot Reproduce


Table9: Priority

Id  Pname
1   Highest
2   High
3   Low
4   Lowest
5   Blocker

Table10: Projectversion

Id  Project Vname
11000   1150    FY20_Q1
11001   1152    FY20_Q2
11002   1167    FY19_Q3
11003   1189    FY19_Q4
11004   1190    FY19_Q1


Table11: nodeassociation

Source_node_id  Sink_node_id    source_node_entity  association_type
12000   11004   "Issue" "IssueFixVersion"
12042   11056   "Issue" "IssueComponent"
12004   11001   "Issue" "IssueFixVersion"
12056   11045   "Issue" "IssueComponent"
12078   11002   "Issue" "IssueFixVersion"


Table12: Cutomfieldoption

Id  Customvalue
10106   PSS
10207   UAT
10504   QA
10604   FIT



PostgreSQL Query:

select jp.PKey || '-' || ji.IssueNum as IssueKey, ji.Summary, cf.cfname as Customfield, cfv.stringvalue as Value, array_to_string(array_agg(l.label), ',') as label, x.fixversion,i.pname as Status, h.pname as Type, r.pname as resolution, p.pname as priority, to_char(ji.created, 'YYYY-MM-DD') as created, to_char(ji.updated, 'YYYY-MM-DD') as updated, to_char(ji.resolutiondate, 'YYYY-MM-DD') as Resolved
 from project jp 
JOIN jiraissue ji ON jp.id = ji.project and jp.pkey ='DCP'
JOIN customfieldvalue cfv ON cfv.issue = ji.id and ji.issuenum = '30'
Left Outer JOIN customfield cf ON cf.id = cfv.customfield
JOIN customfieldoption cfo ON cfv.stringvalue = cfo.id 
LEFT OUTER JOIN label l on ji.id = l.issue 
JOIN issuestatus i on i.id = ji.issuestatus 
JOIN issuetype h on h.id = ji.issuetype 
LEFT OUTER JOIN resolution r ON r.id = ji.resolution JOIN priority p ON p.id = ji.priority LEFT OUTER JOIN (select issuenum, IssueId,array_to_string(array_agg(version), ', ') as fixversion, project 
from (SELECT (SELECT (SELECT pkey FROM project WHERE project.ID = jiraissue.project) from jiraissue WHERE jiraissue.ID = nodeassociation.source_node_id) AS project, 
(SELECT issuenum from jiraissue WHERE jiraissue.ID = nodeassociation.source_node_id) AS issuenum, 
(SELECT id from jiraissue WHERE jiraissue.ID = nodeassociation.source_node_id) AS IssueId, 
(SELECT vname FROM projectversion WHERE projectversion.ID = nodeassociation.sink_node_id) AS version FROM nodeassociation 
JOIN projectversion ON nodeassociation.sink_node_id = projectversion.ID 
WHERE source_node_entity='Issue' and association_type='IssueFixVersion') a 
group by a.issuenum, a.project, a.IssueId) x ON x.project = jp.pkey and x.IssueId= ji.id 
group by jp.id, ji.id, jp.PKey ,ji.IssueNum , ji.Summary, ji.Description, cf.cfname, cfv.stringvalue ,ji.Assignee , ji.reporter, x.fixversion, ji.issuestatus, i.pname, h.pname, r.pname, p.pname, ji.created, ji.updated, ji.resolutiondate


Expected Result:
Issuekey    Summary Fixversion  Label   Type    Status  Priority    Customfield value   created Resolution
DCP-30  Test    FY20_Q1 UAT Epic    Open    High    Team, Category,  Name   PSS,””, puppalr 2019-03-24  Done

0 个答案:

没有答案