问题
I am finding this a bit weird, though I do know SQL, but i have never written such queries in my career.
My requirement is like this:
I have a table named PSPROJECTITEM, which have these columns:
PROJECTNAME OBJECTTYPE OBJECTID1 OBJECTVALUE1 OBJECTID2 OBJECTVALUE2 OBJECTID3 OBJECTVALUE3 OBJECTID4 OBJECTVALUE4
I need to write a query which has to fetch the child rows or the child PROJECTNAME of a PARENT PROJECTNAME, which i will pass in the query.
SELECT PROJECTNAME AS PARENTPROJECT, COUNT(*) AS PARENTOBJECTCOUNT FROM PSPROJECTITEM where PROJECTNAME = 'AAAA_JOB_KJ'
My goal is to find the child projects of this PROJECT which i have passed here, and also , to get the count of the other values, which i think is the total number of row count, as AAAA_JOB_KJ have total count of 199 in the table.
I want to get child project, the count, and also, the number of duplicate count, i.e, how many parent and child objects are similar, i mean, the number of objects which are similar between the parent and the child. Hope this is clear now.
Here is the sample data:
Parent Project Name Parent Project Count Child Project Child Count Similar Object Count
AAAA_JOB_KJ 199 AZ_AUTOFILL_SP1 11 3
AAAA_JOB_KJ 199 AZ_CSRIDHAR_0518 3 1
AAAA_JOB_KJ 199 AZ_DUP_TERM_FACT 2 1
AAAA_JOB_KJ 199 AZ_E000316038_HCM_789 2 1
AAAA_JOB_KJ 199 AZ_E000368318_PROJ 3 2
AAAA_JOB_KJ 199 AZ_HCM_1104_SPD_1028 7 1
AAAA_JOB_KJ 199 AZ_HCM_889_SPD_871 11 1
AAAA_JOB_KJ 199 AZ_JOB_CHANGE 5 1
AAAA_JOB_KJ 199 AZ_MGR_TERMIN 31 1
AAAA_JOB_KJ 199 AZ_PAYROLL_VALIDATIONS 19 4
AAAA_JOB_KJ 199 AZ_Q4AUTOCHG 22 2
AAAA_JOB_KJ 199 AZ_Q4AUTOCHG_ENHCMT 8 2
AAAA_JOB_KJ 199 AZ_Q4_BCKUP 225 130
Please note that the count of child objects can be more than the parent project.
Also, note that there are two kind of projects, one is peoplesoft delivered and others are custom project. I think a general query will be able to handle this.
Kindly let me know if any other modification is needed, as I know that this is very complex, and also, this needs little bit of editing, i tried it, but could not find on how to to do this, I am sorry for this
Database in use is ORACLE.
All are in the same table.
Edited by moderator to add:
The parent project is in the PSPROJECTITEM
. Suppose i have query,
SELECT PROJECTNAME AS PARENTPROJECT, COUNT(*) AS PARENTOBJECTCOUNT
FROM PSPROJECTITEM where PROJECTNAME = 'AAAA_JOB_KJ'
I need a query to fetch the child projects of this project which i have passed in the where clause, the count of the child projects, and also, the duplicate count.
** Addition **
The query below is able to do most of the intended.
Now, a bit of modification is needed:
My sample data is like this:
Parent Project Name Parent Project Count Child Project Child Count Similar Object Count AAAA_JOB_KJ 199 AZ_AUTOFILL_SP1 11 3
but the query output is this:
Parent Project Name Parent Project Count Child Project Child Count Similar Object Count AAAA_JOB_KJ 5 AZ_AUTOFILL_SP1 5
5 is wrong, 199 is the desired out, because the count of AAAA_JOB_KJ is 199 in the table, same for AZ_AUTOFILL_SP1.
Also, need to find the similar objects amongst these table, and display there count.
I think either an inner join or cross join will do to display the count.
回答1:
This is most of the answer, as we determined in the interactive chat:
SELECT A.projectName as PARENT,COUNT(A.PROJECTNAME) AS PARENTPROJECTCOUNT,
B.ProjectName as CHILD, COUNT(B.PROJECTNAME) AS CHILDPROJECT from psprojectitem
a INNER JOIN psProjectItem B
ON a.objecttype = b.objecttype
AND a.objectid1 =b.objectid1
AND a.objectvalue1 = b.objectvalue1
AND a.objectid2 = b.objectid2
AND a.objectvalue2 = b.objectvalue2
AND a.objectid3 = b.objectid3
AND a.objectvalue3 = b.objectvalue3
AND a.objectid4 = b.objectid4
AND a.objectvalue4 = b.objectvalue4
WHERE A.projectname in
(SELECT ProjectName from psProjectDefn WHERE lastupdoprid <> 'pplsoft')
AND a.projectname <> B.projectName
and A.PROJECTNAME = 'AAAA_JOB_KJ'
ORDER BY B.PROJECTNAME
回答2:
I was able to achieve most of the requirement:
SELECT A.projectName as PARENT,
(select COUNT(*) from PSPROJECTITEM WHERE PROJECTNAME = A.PROJECTNAME) parentprojecount,
B.ProjectName as CHILD,
(select COUNT(*) from PSPROJECTITEM WHERE PROJECTNAME = B.PROJECTNAME) CHILDPROJECT from psprojectitem
a INNER JOIN psProjectItem B
ON a.objecttype = b.objecttype
AND a.objectid1 =b.objectid1
AND a.objectvalue1 = b.objectvalue1
AND a.objectid2 = b.objectid2
AND a.objectvalue2 = b.objectvalue2
AND a.objectid3 = b.objectid3
AND a.objectvalue3 = b.objectvalue3
AND a.objectid4 = b.objectid4
AND a.objectvalue4 = b.objectvalue4
WHERE A.projectname in
(SELECT ProjectName from psProjectDefn WHERE lastupdoprid <> 'pplsoft')
AND a.projectname <> B.projectName
and A.PROJECTNAME = 'AAAA_JOB_KJ'
group by A.PROJECTNAME,B.PROJECTNAME
ORDER BY B.PROJECTNAME
The output generated is:
AAAA_JOB_KJ 199 AZ_AUTOFILL_SP1 11.
The only thing which is pending is finding duplicate rows amongst the child and parent, i.e. what are the counts of the common objects of child and parent.
Help on this will be great, Thanks.
来源:https://stackoverflow.com/questions/17079606/how-to-find-the-child-project-of-a-parent-project-which-is-passed-in-the-where