Merging two rows into a single row
Hi ,
I wish to merge few columns of tow rows into a single row.
if i use union it gives two rows as o/p.
What is the exact sql statement for that?
Thanks
Hi Sridhar,
This is my code for merging and i used union.
Suggest me good solution.
CREATE OR REPLACE VIEW INSPECTINT.INSPECT_LOC_TRACKING_RPT_VIEW AS
SELECT
VEN_COMPANY_NAME VEN_COMPANY_NAME,
VEN_COMPANY_NAME_2 VEN_COMPANY_NAME_2,
VEN_CONTACT_FIRST_NAME VEN_CONTACT_FIRST_NAME,
VEN_CONTACT_MIDDLE_NAME VEN_CONTACT_MIDDLE_NAME,
VEN_CONTACT_LAST_NAME VEN_CONTACT_LAST_NAME,
VEN_ADDRESS_LINE_1 VEN_ADDRESS_LINE_1,
VEN_ADDRESS_LINE_2 VEN_ADDRESS_LINE_2,
VEN_CITY VEN_CITY,
VEN_CNT_COUNTY_CODE VEN_CNT_COUNTY_CODE,
VEN_STC_STATE_CODE VEN_STC_STATE_CODE,
VEN_ZIP_CODE VEN_ZIP_CODE,
VEN_PHONE_NO_1 VEN_PHONE_NO_1,
VEN_PHONE_EXTN_1 VEN_PHONE_EXTN_1,
VEN_PHONE_NO_2 VEN_PHONE_NO_2,
VEN_PHONE_EXTN_2 VEN_PHONE_EXTN_2,
VEN_FAX_NO_1 VEN_FAX_NO_1,
VEN_VENDOR_NO VEN_VENDOR_NO,
VEN_APV_AP_VENDOR_ID VEN_APV_AP_VENDOR_ID,
VEN_EMAIL_ADDRESS VEN_EMAIL_ADDRESS,
--all these should be null
to_number(NULL) PHY_LCN_PARAMETER_ID,
to_char(NULL) PHY_LCN_CURRENT_ADDRESS_IND,
to_char(NULL) PHY_LCN_STATUS_CODE,
to_char(NULL) PHY_LCN_LTY_LOCATION_TYPE,
to_char(NULL) PHY_LCN_ADT_ADDRESS_TYPE,
to_char(NULL) PHY_LCN_HOUSE_NO,
to_char(NULL) PHY_LCN_ADDRESS_LINE_1,
to_char(NULL) PHY_LCN_ADDRESS_LINE_2,
to_char(NULL) PHY_LCN_CITY,
to_char(NULL) PHY_LCN_STC_STATE_CODE,
to_char(NULL) PHY_LCN_ZIP_CODE,
to_char(NULL) PHY_LCN_CNT_COUNTY_CODE,
to_date(NULL) PHY_END_DATE,
to_number(NULL) PHY_LCN_VEN_VENDOR_ID_GTA,
to_char(NULL) PHY_LCN_SUB_LOCATION_GTA,
to_char(NULL) PHY_LCN_DIRECTION_GTA,
---all these should be null
to_char(NULL) LOW_LCN_PARAMETER_ID,
to_char(NULL) LOW_LCN_CURRENT_ADDRESS_IND,
to_char(NULL) LOW_LCN_STATUS_CODE,
to_char(NULL) LOW_LCN_LTY_LOCATION_TYPE,
to_char(NULL) LOW_LCN_ADT_ADDRESS_TYPE,
to_char(NULL) LOW_LCN_HOUSE_NO,
to_char(NULL) LOW_LCN_ADDRESS_LINE_1,
to_char(NULL) LOW_LCN_ADDRESS_LINE_2,
to_char(NULL) LOW_LCN_CITY,
to_char(NULL) LOW_LCN_STC_STATE_CODE,
to_char(NULL) LOW_LCN_ZIP_CODE,
to_char(NULL) LOW_LCN_CNT_COUNTY_CODE,
to_char(NULL) LOW_LCN_LANDLORD,
to_number(NULL) LOW_LCN_LANDLORD_PHONE,
to_date(NULL) LOW_END_DATE,
to_char(NULL) LOW_LCN_SUB_LOCATION_GTA,
to_char(NULL) LOW_LCN_DIRECTION_GTA
FROM LOCATIONS LOC , VENDORS VEN
WHERE LOC.LCN_VEN_VENDOR_ID_GTA IS NOT NULL
AND LOC.LCN_VEN_VENDOR_ID_GTA = VEN_VENDOR_ID
AND LOC.LCN_LTY_LOCATION_TYPE ='AS'
UNION ALL
SELECT
to_char(NULL) VEN_COMPANY_NAME,
to_char(NULL) VEN_COMPANY_NAME_2,
to_char(NULL) VEN_CONTACT_FIRST_NAME,
to_char(NULL) VEN_CONTACT_MIDDLE_NAME,
to_char(NULL) VEN_CONTACT_LAST_NAME,
to_char(NULL) VEN_ADDRESS_LINE_1,
to_char(NULL) VEN_ADDRESS_LINE_2,
to_char(NULL) VEN_CITY,
to_char(NULL) VEN_CNT_COUNTY_CODE,
to_char(NULL) VEN_STC_STATE_CODE,
to_char(NULL) VEN_ZIP_CODE,
to_number(NULL) VEN_PHONE_NO_1,
to_number(NULL) VEN_PHONE_EXTN_1,
to_number(NULL) VEN_PHONE_NO_2,
to_number(NULL) VEN_PHONE_EXTN_2,
to_number(NULL) VEN_FAX_NO_1,
to_char(NULL) VEN_VENDOR_NO,
to_char(NULL) VEN_APV_AP_VENDOR_ID,
to_char(NULL) VEN_EMAIL_ADDRESS,
decode(lcn_adt_address_type,'P',to_number(LCN_PARAMETER_ID),to_number(NULL)) PHY_LCN_PARAMETER_ID,
decode(lcn_adt_address_type,'P',LCN_CURRENT_ADDRESS_IND,to_char(NULL)) PHY_LCN_CURRENT_ADDRESS_IND,
decode(lcn_adt_address_type,'P',LCN_STATUS_CODE,to_char(NULL)) PHY_LCN_STATUS_CODE,
decode(lcn_adt_address_type,'P',LCN_STATUS_CODE,to_char(NULL)) PHY_LCN_LTY_LOCATION_TYPE,
decode(lcn_adt_address_type,'P',LCN_ADT_ADDRESS_TYPE,to_char(NULL)) PHY_LCN_ADT_ADDRESS_TYPE,
decode(lcn_adt_address_type,'P',LCN_HOUSE_NO,to_char(NULL)) PHY_LCN_HOUSE_NO,
decode(lcn_adt_address_type,'P',LCN_ADDRESS_LINE_1,to_char(NULL)) PHY_LCN_ADDRESS_LINE_1,
decode(lcn_adt_address_type,'P',LCN_ADDRESS_LINE_2,to_char(NULL)) PHY_LCN_ADDRESS_LINE_2,
decode(lcn_adt_address_type,'P',LCN_CITY,to_char(NULL)) PHY_LCN_CITY,
decode(lcn_adt_address_type,'P',LCN_STC_STATE_CODE,to_char(NULL)) PHY_LCN_STC_STATE_CODE,
decode(lcn_adt_address_type,'P',LCN_ZIP_CODE,to_char(NULL)) PHY_LCN_ZIP_CODE,
decode(lcn_adt_address_type,'P',LCN_CNT_COUNTY_CODE,to_char(NULL)) PHY_LCN_CNT_COUNTY_CODE,
decode(lcn_adt_address_type,'P',END_DATE,to_char(NULL)) PHY_END_DATE,
decode(lcn_adt_address_type,'P',LCN_VEN_VENDOR_ID_GTA,to_char(NULL)) PHY_LCN_VEN_VENDOR_ID_GTA,
decode(lcn_adt_address_type,'P',LCN_SUB_LOCATION_GTA,to_char(NULL)) PHY_LCN_SUB_LOCATION_GTA,
decode(lcn_adt_address_type,'P',LCN_DIRECTION_GTA,to_char(NULL)) PHY_LCN_DIRECTION_GTA,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_PARAMETER_ID,to_char(NULL)) LOW_LCN_PARAMETER_ID,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_CURRENT_ADDRESS_IND,to_char(NULL)) LOW_LCN_CURRENT_ADDRESS_IND,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_STATUS_CODE,to_char(NULL)) LOW_LCN_STATUS_CODE,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_LTY_LOCATION_TYPE,to_char(NULL)) LOW_LCN_LTY_LOCATION_TYPE,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_ADT_ADDRESS_TYPE,to_char(NULL)) LOW_LCN_ADT_ADDRESS_TYPE,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_HOUSE_NO,to_char(NULL)) LOW_LCN_HOUSE_NO,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_ADDRESS_LINE_1,to_char(NULL)) LOW_LCN_ADDRESS_LINE_1,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_ADDRESS_LINE_2,to_char(NULL)) LOW_LCN_ADDRESS_LINE_2,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_CITY,to_char(NULL)) LOW_LCN_CITY,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_STC_STATE_CODE,to_char(NULL)) LOW_LCN_STC_STATE_CODE,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_ZIP_CODE,to_char(NULL)) LOW_LCN_ZIP_CODE,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_CNT_COUNTY_CODE,to_char(NULL)) LOW_LCN_CNT_COUNTY_CODE,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_LANDLORD,to_char(NULL)) LOW_LCN_LANDLORD,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_LANDLORD_PHONE,to_char(NULL)) LOW_LCN_LANDLORD_PHONE,
decode(LCN_ADT_ADDRESS_TYPE,'D',END_DATE,to_char(NULL)) LOW_END_DATE,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_SUB_LOCATION_GTA,to_char(NULL)) LOW_LCN_SUB_LOCATION_GTA,
decode(LCN_ADT_ADDRESS_TYPE,'D',LCN_DIRECTION_GTA,to_char(NULL)) LOW_LCN_DIRECTION_GTA
FROM LOCATIONS LOC
WHERE (LCN_LTY_LOCATION_TYPE ='AS' OR LCN_ADT_ADDRESS_TYPE='D')
AND LCN_CURRENT_ADDRESS_IND ='Y'
thanks
Similar Messages
-
Merging two results into a single row
Hi, Everyone... I think this is a weighted question and I feel like it's a bit difficult to phrase, so if it doesn't make sense - let me know and I'll try to clarify or provide some more concrete examples (if I can) :)
I have a few tables inner joined in my sql query - I'd post the entire query here but it's lengthy and will probably add a bit more confusion so let's start with theorizing - where the results sometimes generate more than one row. If more than one row exists, I'd like to take the lowest number in one field and the highest number in another field, and display them on a single row.
For instance, when I join the PERIOD and SCHEDULE tables, the PERIOD table might provide two rows like the following:
SCHEDULE_ID PERIOD_ID START_TIME END_TIME
123 100 32820 35640
123 101 35940 38760My final result should take the lowest START_TIME and the highest END_TIME, even if they exist is separate rows.
START_TIME END_TIME
32820 38760It doesn't seem easy to me so if someone can make sense of this - you deserve an award. :) If I can provide some more clarification, please let me know. Thanks for any help. Oracle 11g
Edited by: nage62587 on Sep 27, 2012 11:32 AMnage62587 wrote:
Thanks! I'll try that... I have more than these columns in the result and I'm thinking a Group By likely wouldn't work - I will try but in the event it doesn't, does anyone else have any other suggestions?Group by is the correct way to do this. You group several rows of output into one row. Then you decide which values from inside that group you want to see. This is certainly possible. There are more aggregation functions available than you might think, not only min/max, also min/max + KEEP, count(case expression) and so on. -
Oracle query - Merging multiple rows into a single row output
Hi All,
I have to have a multiple row output to be converted into a single row output.My current output looks as follows:
ID YR INC_CODE OFFN SCHOOLNO
8006 2002 00175 SC03 12
8006 2002 00175 DC06 12
8006 2002 00175 DC03 12
8006 2002 00175 DC02 12
ID,INCIDENT CODE,OFFENSE are all Primary keys
So I need the output as follows:(IN ONE ROW)
ID YR INC_CODE OFFN1 OFFN2 OFFN3 OFFN4 SCHOOLNO
8006 2002 00175 SC03 DC06 DC03 DC02 12
Can you help me on this since have been spinning the wheel and this has to be a query since will have couple of tables join to produce a materialized view.
Thanks in advanceHi Nigel,
Thanks for the reply I tested out the portion having the decode and I get the output as follows:
ID YR INC_CODE OFFN1 OFFN2 OFFN3 OFFN4 OFFN5
8982 2002 2175 DOC01 -----------------------
8982 2002 2175 DOC02-------------------
8982 2002 2175 DOC03------------
8982 2002 2175 DOC06-------
8982 2002 2175 SCV03
There is no value as max for OFFN and each INC_CODE MAY HAVE UP TO A MAX OF 5 OFFN.My query is as follows:
select distinct STU_STUDENT_ID, INC_BEG_SCH_YR,INC_INCIDENT_CODE
, decode(rank() over (partition by INC_CODE order by OFFN),1,OFFN,null) as offn1
, decode(rank() over (partition by INC_CODE order by OFFN),2,OFFN,null) as offn2
, decode(rank() over (partition by INC_CODE order by OFFN),3,OFFN,null) as offn3
, decode(rank() over (partition by INC_CODE order by OFFN),4,OFFN,null) as offn4
, decode(rank() over (partition by INC_CODE order by OFFN),5,OFFN,null) as offn5
from stu_offn where
stu_offn.ID = '8982' and stu_offn.INC_CODE = '2175'
(****Where clause is just given to just check a value)
So as you know I need to just have all the OFFN in a single row ie as follows:
ID YR INC_CODE OFFN1 OFFN2 OFFN3 OFFN4 OFFN5
8982 2002 2175 DOC01 DOC02 DOC03 DOC06 SCV03
Can you just give me a step by step procedure to go through this and the table in this case is just 'STU_OFFN'
Thanks for the earlier reply appreciate it!
****Sending this again to show the exact way the output is coming -
Flatten rows into a single row
Hi All,
I have a table1 with records and PK for table 1 is (Date,Time).
All I have to do is for a particular transaction, I have to get all the records and flatten them into a single row.
For ex: Table 1 has the following
Date Time Col1 Value
08/03/2010 10:00am Employee_name John
08/03/2010 10:00am Employee_ID 20
08/03/2010 10:00am Salary 10000
Now I have a table 2 which has the following structure:
Date Time Employee_name Employee_ID Salary
08/03/2010 10:00am John 20 10000
Please let me know how to achieve this.,
Thank You.Hi,
Are you saying that you want to create table2, given table1?
Displaying one column from many rows as if they were many columns on one row is called a Pivot , and here's one way to do it:
SELECT date_time
, MIN (CASE WHEN col1 = 'Employee_name' THEN value END) AS employee_name
, MIN (CASE WHEN col1 = 'Employee_ID' THEN value END) AS employee_id
, MIN (CASE WHEN col1 = 'Salary' THEN value END) AS salary
FROM table1
GROUP BY date_time
;Whenever you have a question, say what version of Oracle you're using.
The query above works in Oracle 8.1 and up, but it might be simpler using the SELECT ... PIVOT feature that was introduced in Oracle 11.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements), and also post the results you want from that data. -
Merging Multiple Rows into a single Row
I've read a number of posts with no real good answers or answers that might be good for tables with 3 columns of data. I have a table with 33 columns that will all need to be combined. A little history. Apparently in production the Primary Key Constraint
was dropped which allowed some duplicate data into the table. Now they want me to squish the records together to fix it.
Sorry I couldn't include the screen shot of the data, MSDN says my account isn't verified...
My requirements when I'm putting them together, CERElibilityID is a unique column. This is a sticky problem because in the related tables it is only one of the parent records gets child records related to it.
I just want to keep the one that has the child records in CERPrepActivity table. If there are no child records then the rule will be like all the other records with data.
If a field is null and another record has data we take the record that has data.
If multiple records have data we take the record that has the highest "ModifiedOn" timestamp column value.
I feel like I'm asking a lot, but I'm in a bit of a bind and I've spent the morning Googling and have come up with nothing
Thank you for any help the community can provide and the more specific you can be the better. I'm not a DBA or SQL guru by any stretch so I'm way out of my element.
ThanksUsing the systables, you can get information about your table. I put these together:
SELECT '
LEFT OUTER JOIN #CEREligibility ['+c.name+']
ON s.caseID = ['+c.name+'].caseID
AND s.modifiedOn = ['+c.name+'].modifiedOn
AND ['+c.name+'].['+c.name+'] = (SELECT TOP 1 ['+c.name+'] FROM #CEREligibility WHERE caseID = s.caseID AND ['+c.name+'] IS NOT NULL ORDER BY ModifiedOn)
AND ['+c.name+'].['+c.name+'] IS NOT NULL'
FROM tempdb.sys.objects o
INNER JOIN tempdb.sys.columns c
ON o.object_id = c.object_id
AND o.name LIKE '#CEREligibility%'
SELECT '['+c.name+'].['+c.name+'] AS ['+c.name+'], '
FROM tempdb.sys.objects o
INNER JOIN tempdb.sys.columns c
ON o.object_id = c.object_id
AND o.name LIKE '#CEREligibility%'
On my test system I created your table as a temp table named #CEREligibility.
The first of these two queries generates LEFT OUT JOIN SQL for a self join. Because we're using the columns systable, a LOJ for each column is generated.
The second, produces a select list.
Putting the two together like so, and adding a FROM and GROUP BY gives us:
SELECT
MAX([CEREligibilityId].[CEREligibilityId]) AS [CEREligibilityId], s.[CaseId], MAX([M2Eligibility].[M2Eligibility]) AS [M2Eligibility], MAX([CDREligibility].[CDREligibility]) AS [CDREligibility], MAX([M2Comments].[M2Comments]) AS [M2Comments], MAX([M2CommentsRtf].[M2CommentsRtf]) AS [M2CommentsRtf], MAX([CDRComments].[CDRComments]) AS [CDRComments], MAX([CDRCommentsRtf].[CDRCommentsRtf]) AS [CDRCommentsRtf], MAX([RIAWA].[RIAWA]) AS [RIAWA], MAX([RIAWADate].[RIAWADate]) AS [RIAWADate], MAX([EducationAndTraining].[EducationAndTraining]) AS [EducationAndTraining], MAX([EducationAndTrainingDate].[EducationAndTrainingDate]) AS [EducationAndTrainingDate], MAX([Internship].[Internship]) AS [Internship], MAX([InternshipDate].[InternshipDate]) AS [InternshipDate], MAX([Apprenticeship].[Apprenticeship]) AS [Apprenticeship], MAX([ApprenticeshipDate].[ApprenticeshipDate]) AS [ApprenticeshipDate], MAX([Entrepreneurship].[Entrepreneurship]) AS [Entrepreneurship], MAX([EntrepreneurshipDate].[EntrepreneurshipDate]) AS [EntrepreneurshipDate], MAX([EmploymentPrep].[EmploymentPrep]) AS [EmploymentPrep], MAX([EmploymentPrepDate].[EmploymentPrepDate]) AS [EmploymentPrepDate], MAX([OTPrep].[OTPrep]) AS [OTPrep], MAX([OTPrepDate].[OTPrepDate]) AS [OTPrepDate], MAX([ExitInterview].[ExitInterview]) AS [ExitInterview], MAX([ExitInterviewDate].[ExitInterviewDate]) AS [ExitInterviewDate], MAX([CreatedOn].[CreatedOn]) AS [CreatedOn], MAX([CreatedBy].[CreatedBy]) AS [CreatedBy], MAX([ModifiedOn].[ModifiedOn]) AS [ModifiedOn], MAX([ModifiedBy].[ModifiedBy]) AS [ModifiedBy], MAX([BCPermInEligibility].[BCPermInEligibility]) AS [BCPermInEligibility], MAX([CCPermInEligibility].[CCPermInEligibility]) AS [CCPermInEligibility], MAX([M2EligibilityDate].[M2EligibilityDate]) AS [M2EligibilityDate], MAX([CDREligibilityDate].[CDREligibilityDate]) AS [CDREligibilityDate], MAX([ExitSeparationCode].[ExitSeparationCode]) AS [ExitSeparationCode], MAX([ExitSeparationSubCode].[ExitSeparationSubCode]) AS [ExitSeparationSubCode]
FROM #CEREligibility s
LEFT OUTER JOIN #CEREligibility [CEREligibilityId]
ON s.caseID = [CEREligibilityId].caseID
AND s.modifiedOn = [CEREligibilityId].modifiedOn
AND [CEREligibilityId].[CEREligibilityId] = (SELECT TOP 1 [CEREligibilityId] FROM #CEREligibility WHERE caseID = s.caseID AND [CEREligibilityId] IS NOT NULL ORDER BY ModifiedOn)
AND [CEREligibilityId].[CEREligibilityId] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [CaseId]
ON s.caseID = [CaseId].caseID
AND s.modifiedOn = [CaseId].modifiedOn
AND [CaseId].[CaseId] = (SELECT TOP 1 [CaseId] FROM #CEREligibility WHERE caseID = s.caseID AND [CaseId] IS NOT NULL ORDER BY ModifiedOn)
AND [CaseId].[CaseId] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [M2Eligibility]
ON s.caseID = [M2Eligibility].caseID
AND s.modifiedOn = [M2Eligibility].modifiedOn
AND [M2Eligibility].[M2Eligibility] = (SELECT TOP 1 [M2Eligibility] FROM #CEREligibility WHERE caseID = s.caseID AND [M2Eligibility] IS NOT NULL ORDER BY ModifiedOn)
AND [M2Eligibility].[M2Eligibility] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [CDREligibility]
ON s.caseID = [CDREligibility].caseID
AND s.modifiedOn = [CDREligibility].modifiedOn
AND [CDREligibility].[CDREligibility] = (SELECT TOP 1 [CDREligibility] FROM #CEREligibility WHERE caseID = s.caseID AND [CDREligibility] IS NOT NULL ORDER BY ModifiedOn)
AND [CDREligibility].[CDREligibility] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [M2Comments]
ON s.caseID = [M2Comments].caseID
AND s.modifiedOn = [M2Comments].modifiedOn
AND [M2Comments].[M2Comments] = (SELECT TOP 1 [M2Comments] FROM #CEREligibility WHERE caseID = s.caseID AND [M2Comments] IS NOT NULL ORDER BY ModifiedOn)
AND [M2Comments].[M2Comments] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [M2CommentsRtf]
ON s.caseID = [M2CommentsRtf].caseID
AND s.modifiedOn = [M2CommentsRtf].modifiedOn
AND [M2CommentsRtf].[M2CommentsRtf] = (SELECT TOP 1 [M2CommentsRtf] FROM #CEREligibility WHERE caseID = s.caseID AND [M2CommentsRtf] IS NOT NULL ORDER BY ModifiedOn)
AND [M2CommentsRtf].[M2CommentsRtf] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [CDRComments]
ON s.caseID = [CDRComments].caseID
AND s.modifiedOn = [CDRComments].modifiedOn
AND [CDRComments].[CDRComments] = (SELECT TOP 1 [CDRComments] FROM #CEREligibility WHERE caseID = s.caseID AND [CDRComments] IS NOT NULL ORDER BY ModifiedOn)
AND [CDRComments].[CDRComments] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [CDRCommentsRtf]
ON s.caseID = [CDRCommentsRtf].caseID
AND s.modifiedOn = [CDRCommentsRtf].modifiedOn
AND [CDRCommentsRtf].[CDRCommentsRtf] = (SELECT TOP 1 [CDRCommentsRtf] FROM #CEREligibility WHERE caseID = s.caseID AND [CDRCommentsRtf] IS NOT NULL ORDER BY ModifiedOn)
AND [CDRCommentsRtf].[CDRCommentsRtf] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [RIAWA]
ON s.caseID = [RIAWA].caseID
AND s.modifiedOn = [RIAWA].modifiedOn
AND [RIAWA].[RIAWA] = (SELECT TOP 1 [RIAWA] FROM #CEREligibility WHERE caseID = s.caseID AND [RIAWA] IS NOT NULL ORDER BY ModifiedOn)
AND [RIAWA].[RIAWA] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [RIAWADate]
ON s.caseID = [RIAWADate].caseID
AND s.modifiedOn = [RIAWADate].modifiedOn
AND [RIAWADate].[RIAWADate] = (SELECT TOP 1 [RIAWADate] FROM #CEREligibility WHERE caseID = s.caseID AND [RIAWADate] IS NOT NULL ORDER BY ModifiedOn)
AND [RIAWADate].[RIAWADate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [EducationAndTraining]
ON s.caseID = [EducationAndTraining].caseID
AND s.modifiedOn = [EducationAndTraining].modifiedOn
AND [EducationAndTraining].[EducationAndTraining] = (SELECT TOP 1 [EducationAndTraining] FROM #CEREligibility WHERE caseID = s.caseID AND [EducationAndTraining] IS NOT NULL ORDER BY ModifiedOn)
AND [EducationAndTraining].[EducationAndTraining] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [EducationAndTrainingDate]
ON s.caseID = [EducationAndTrainingDate].caseID
AND s.modifiedOn = [EducationAndTrainingDate].modifiedOn
AND [EducationAndTrainingDate].[EducationAndTrainingDate] = (SELECT TOP 1 [EducationAndTrainingDate] FROM #CEREligibility WHERE caseID = s.caseID AND [EducationAndTrainingDate] IS NOT NULL ORDER BY ModifiedOn)
AND [EducationAndTrainingDate].[EducationAndTrainingDate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [Internship]
ON s.caseID = [Internship].caseID
AND s.modifiedOn = [Internship].modifiedOn
AND [Internship].[Internship] = (SELECT TOP 1 [Internship] FROM #CEREligibility WHERE caseID = s.caseID AND [Internship] IS NOT NULL ORDER BY ModifiedOn)
AND [Internship].[Internship] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [InternshipDate]
ON s.caseID = [InternshipDate].caseID
AND s.modifiedOn = [InternshipDate].modifiedOn
AND [InternshipDate].[InternshipDate] = (SELECT TOP 1 [InternshipDate] FROM #CEREligibility WHERE caseID = s.caseID AND [InternshipDate] IS NOT NULL ORDER BY ModifiedOn)
AND [InternshipDate].[InternshipDate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [Apprenticeship]
ON s.caseID = [Apprenticeship].caseID
AND s.modifiedOn = [Apprenticeship].modifiedOn
AND [Apprenticeship].[Apprenticeship] = (SELECT TOP 1 [Apprenticeship] FROM #CEREligibility WHERE caseID = s.caseID AND [Apprenticeship] IS NOT NULL ORDER BY ModifiedOn)
AND [Apprenticeship].[Apprenticeship] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [ApprenticeshipDate]
ON s.caseID = [ApprenticeshipDate].caseID
AND s.modifiedOn = [ApprenticeshipDate].modifiedOn
AND [ApprenticeshipDate].[ApprenticeshipDate] = (SELECT TOP 1 [ApprenticeshipDate] FROM #CEREligibility WHERE caseID = s.caseID AND [ApprenticeshipDate] IS NOT NULL ORDER BY ModifiedOn)
AND [ApprenticeshipDate].[ApprenticeshipDate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [Entrepreneurship]
ON s.caseID = [Entrepreneurship].caseID
AND s.modifiedOn = [Entrepreneurship].modifiedOn
AND [Entrepreneurship].[Entrepreneurship] = (SELECT TOP 1 [Entrepreneurship] FROM #CEREligibility WHERE caseID = s.caseID AND [Entrepreneurship] IS NOT NULL ORDER BY ModifiedOn)
AND [Entrepreneurship].[Entrepreneurship] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [EntrepreneurshipDate]
ON s.caseID = [EntrepreneurshipDate].caseID
AND s.modifiedOn = [EntrepreneurshipDate].modifiedOn
AND [EntrepreneurshipDate].[EntrepreneurshipDate] = (SELECT TOP 1 [EntrepreneurshipDate] FROM #CEREligibility WHERE caseID = s.caseID AND [EntrepreneurshipDate] IS NOT NULL ORDER BY ModifiedOn)
AND [EntrepreneurshipDate].[EntrepreneurshipDate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [EmploymentPrep]
ON s.caseID = [EmploymentPrep].caseID
AND s.modifiedOn = [EmploymentPrep].modifiedOn
AND [EmploymentPrep].[EmploymentPrep] = (SELECT TOP 1 [EmploymentPrep] FROM #CEREligibility WHERE caseID = s.caseID AND [EmploymentPrep] IS NOT NULL ORDER BY ModifiedOn)
AND [EmploymentPrep].[EmploymentPrep] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [EmploymentPrepDate]
ON s.caseID = [EmploymentPrepDate].caseID
AND s.modifiedOn = [EmploymentPrepDate].modifiedOn
AND [EmploymentPrepDate].[EmploymentPrepDate] = (SELECT TOP 1 [EmploymentPrepDate] FROM #CEREligibility WHERE caseID = s.caseID AND [EmploymentPrepDate] IS NOT NULL ORDER BY ModifiedOn)
AND [EmploymentPrepDate].[EmploymentPrepDate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [OTPrep]
ON s.caseID = [OTPrep].caseID
AND s.modifiedOn = [OTPrep].modifiedOn
AND [OTPrep].[OTPrep] = (SELECT TOP 1 [OTPrep] FROM #CEREligibility WHERE caseID = s.caseID AND [OTPrep] IS NOT NULL ORDER BY ModifiedOn)
AND [OTPrep].[OTPrep] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [OTPrepDate]
ON s.caseID = [OTPrepDate].caseID
AND s.modifiedOn = [OTPrepDate].modifiedOn
AND [OTPrepDate].[OTPrepDate] = (SELECT TOP 1 [OTPrepDate] FROM #CEREligibility WHERE caseID = s.caseID AND [OTPrepDate] IS NOT NULL ORDER BY ModifiedOn)
AND [OTPrepDate].[OTPrepDate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [ExitInterview]
ON s.caseID = [ExitInterview].caseID
AND s.modifiedOn = [ExitInterview].modifiedOn
AND [ExitInterview].[ExitInterview] = (SELECT TOP 1 [ExitInterview] FROM #CEREligibility WHERE caseID = s.caseID AND [ExitInterview] IS NOT NULL ORDER BY ModifiedOn)
AND [ExitInterview].[ExitInterview] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [ExitInterviewDate]
ON s.caseID = [ExitInterviewDate].caseID
AND s.modifiedOn = [ExitInterviewDate].modifiedOn
AND [ExitInterviewDate].[ExitInterviewDate] = (SELECT TOP 1 [ExitInterviewDate] FROM #CEREligibility WHERE caseID = s.caseID AND [ExitInterviewDate] IS NOT NULL ORDER BY ModifiedOn)
AND [ExitInterviewDate].[ExitInterviewDate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [CreatedOn]
ON s.caseID = [CreatedOn].caseID
AND s.modifiedOn = [CreatedOn].modifiedOn
AND [CreatedOn].[CreatedOn] = (SELECT TOP 1 [CreatedOn] FROM #CEREligibility WHERE caseID = s.caseID AND [CreatedOn] IS NOT NULL ORDER BY ModifiedOn)
AND [CreatedOn].[CreatedOn] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [CreatedBy]
ON s.caseID = [CreatedBy].caseID
AND s.modifiedOn = [CreatedBy].modifiedOn
AND [CreatedBy].[CreatedBy] = (SELECT TOP 1 [CreatedBy] FROM #CEREligibility WHERE caseID = s.caseID AND [CreatedBy] IS NOT NULL ORDER BY ModifiedOn)
AND [CreatedBy].[CreatedBy] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [ModifiedOn]
ON s.caseID = [ModifiedOn].caseID
AND s.modifiedOn = [ModifiedOn].modifiedOn
AND [ModifiedOn].[ModifiedOn] = (SELECT TOP 1 [ModifiedOn] FROM #CEREligibility WHERE caseID = s.caseID AND [ModifiedOn] IS NOT NULL ORDER BY ModifiedOn)
AND [ModifiedOn].[ModifiedOn] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [ModifiedBy]
ON s.caseID = [ModifiedBy].caseID
AND s.modifiedOn = [ModifiedBy].modifiedOn
AND [ModifiedBy].[ModifiedBy] = (SELECT TOP 1 [ModifiedBy] FROM #CEREligibility WHERE caseID = s.caseID AND [ModifiedBy] IS NOT NULL ORDER BY ModifiedOn)
AND [ModifiedBy].[ModifiedBy] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [BCPermInEligibility]
ON s.caseID = [BCPermInEligibility].caseID
AND s.modifiedOn = [BCPermInEligibility].modifiedOn
AND [BCPermInEligibility].[BCPermInEligibility] = (SELECT TOP 1 [BCPermInEligibility] FROM #CEREligibility WHERE caseID = s.caseID AND [BCPermInEligibility] IS NOT NULL ORDER BY ModifiedOn)
AND [BCPermInEligibility].[BCPermInEligibility] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [CCPermInEligibility]
ON s.caseID = [CCPermInEligibility].caseID
AND s.modifiedOn = [CCPermInEligibility].modifiedOn
AND [CCPermInEligibility].[CCPermInEligibility] = (SELECT TOP 1 [CCPermInEligibility] FROM #CEREligibility WHERE caseID = s.caseID AND [CCPermInEligibility] IS NOT NULL ORDER BY ModifiedOn)
AND [CCPermInEligibility].[CCPermInEligibility] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [M2EligibilityDate]
ON s.caseID = [M2EligibilityDate].caseID
AND s.modifiedOn = [M2EligibilityDate].modifiedOn
AND [M2EligibilityDate].[M2EligibilityDate] = (SELECT TOP 1 [M2EligibilityDate] FROM #CEREligibility WHERE caseID = s.caseID AND [M2EligibilityDate] IS NOT NULL ORDER BY ModifiedOn)
AND [M2EligibilityDate].[M2EligibilityDate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [CDREligibilityDate]
ON s.caseID = [CDREligibilityDate].caseID
AND s.modifiedOn = [CDREligibilityDate].modifiedOn
AND [CDREligibilityDate].[CDREligibilityDate] = (SELECT TOP 1 [CDREligibilityDate] FROM #CEREligibility WHERE caseID = s.caseID AND [CDREligibilityDate] IS NOT NULL ORDER BY ModifiedOn)
AND [CDREligibilityDate].[CDREligibilityDate] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [ExitSeparationCode]
ON s.caseID = [ExitSeparationCode].caseID
AND s.modifiedOn = [ExitSeparationCode].modifiedOn
AND [ExitSeparationCode].[ExitSeparationCode] = (SELECT TOP 1 [ExitSeparationCode] FROM #CEREligibility WHERE caseID = s.caseID AND [ExitSeparationCode] IS NOT NULL ORDER BY ModifiedOn)
AND [ExitSeparationCode].[ExitSeparationCode] IS NOT NULL
LEFT OUTER JOIN #CEREligibility [ExitSeparationSubCode]
ON s.caseID = [ExitSeparationSubCode].caseID
AND s.modifiedOn = [ExitSeparationSubCode].modifiedOn
AND [ExitSeparationSubCode].[ExitSeparationSubCode] = (SELECT TOP 1 [ExitSeparationSubCode] FROM #CEREligibility WHERE caseID = s.caseID AND [ExitSeparationSubCode] IS NOT NULL ORDER BY ModifiedOn)
AND [ExitSeparationSubCode].[ExitSeparationSubCode] IS NOT NULL
GROUP BY s.caseID
When I ran this against your test data, It produced a single row for each of the two case ID's.
To keep performance in line, I also created a covering index on the temp table:
CREATE INDEX idx_caseID_ModifiedOn ON #CEREligibility (caseID, modifiedOn) INCLUDE ([CEREligibilityId], [M2Eligibility], [CDREligibility], [M2Comments], [M2CommentsRtf], [CDRComments], [CDRCommentsRtf], [RIAWA], [RIAWADate], [EducationAndTraining], [EducationAndTrainingDate], [Internship], [InternshipDate], [Apprenticeship], [ApprenticeshipDate], [Entrepreneurship], [EntrepreneurshipDate], [EmploymentPrep], [EmploymentPrepDate], [OTPrep], [OTPrepDate], [ExitInterview], [ExitInterviewDate], [CreatedOn], [CreatedBy], [ModifiedBy], [BCPermInEligibility], [CCPermInEligibility], [M2EligibilityDate], [CDREligibilityDate], [ExitSeparationCode], [ExitSeparationSubCode])
Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question. -
select OPRCLASS,BUSINESS_UNIT from ps_sec_bu_cls where oprclass like 'OHRL_ADA01%';
OPRCLASS BUSINESS_UNIT
OHRL_ADA01 ADA01
OHRL_ADA01 PRT01
OHRL_ADA01 STATE
How can i merge all the BUSINESS_UNIT rows into one row with a delimiter?
OPRCLASS BUSINESS_UNIT
OHRL_ADA01 ADA01, PRT01, STATEOne possible solution: STRAGG
-
Need help with turning multiple rows into a single row
Hello.
I've come across a situation that is somewhat beyond my knowledge base. I could use a little help with figuring this out.
My situation:
I am attempting to do some reporting from a JIRA database. What I am doing is getting the dates and times for specific step points of a ticket. This is resulting in many rows per ticket. What I need to do is return one row per ticket with a calculation of time between each step. But one issue is that if a ticket is re-opened, I want to ignore all data beyond the first close date. Also, not all tickets are in a closed state. I am attaching code and a sample list of the results. If I am not quite clear, please ask for information and I will attempt to provide more. The database is 10.2.0.4
select jiraissue.id, pkey, reporter, summary
,changegroup.created change_dt
,dbms_lob.substr(changeitem.newstring,15,1) change_type
,row_number() OVER ( PARTITION BY jiraissue.id ORDER BY changegroup.created ASC ) AS order_row
from jiraissue
,changeitem, changegroup
,(select * from customfieldvalue where customfield = 10591 and stringvalue = 'Support') phaseinfo
where jiraissue.project = 10110
and jiraissue.issuetype = 51
and dbms_lob.substr(changeitem.newstring,15,1) in ('Blocked','Closed','Testing','Open')
and phaseinfo.issue = jiraissue.id
and changeitem.groupid = changegroup.id
and changegroup.issueid = jiraissue.id
order by jiraissue.id,change_dt
Results:
1 21191 QCS-91 Error running the Earliest-deadlines flight interface request/response message 2008-07-16 9:30:38 AM Open 1
2 21191 QCS-91 Error running the Earliest-deadlines flight interface request/response message 2008-07-16 11:37:02 AM Testing 2
3 21191 QCS-91 Error running the Earliest-deadlines flight interface request/response message 2010-06-08 9:14:52 AM Closed 3
4 21191 QCS-91 Error running the Earliest-deadlines flight interface request/response message 2010-09-02 11:29:37 AM Open 4
5 21191 QCS-91 Error running the Earliest-deadlines flight interface request/response message 2010-09-02 11:29:42 AM Open 5
6 21191 QCS-91 Error running the Earliest-deadlines flight interface request/response message 2010-09-02 11:29:50 AM Testing 6
7 21191 QCS-91 Error running the Earliest-deadlines flight interface request/response message 2010-09-02 11:29:53 AM Closed 7
8 23234 QCS-208 System Baseline - OK button does not show up in the Defer Faults page for the System Engineer role 2008-10-03 10:26:21 AM Open 1
9 23234 QCS-208 System Baseline - OK button does not show up in the Defer Faults page for the System Engineer role 2008-11-17 9:39:39 AM Testing 2
10 23234 QCS-208 System Baseline - OK button does not show up in the Defer Faults page for the System Engineer role 2011-02-02 6:18:02 AM Closed 3
11 23977 QCS-311 Tally Sheet - Reason Not Done fails to provide reason for unassigned tasks 2008-09-29 2:44:54 PM Open 1
12 23977 QCS-311 Tally Sheet - Reason Not Done fails to provide reason for unassigned tasks 2010-05-29 4:47:37 PM Blocked 2
13 23977 QCS-311 Tally Sheet - Reason Not Done fails to provide reason for unassigned tasks 2011-02-02 6:14:57 AM Open 3
14 23977 QCS-311 Tally Sheet - Reason Not Done fails to provide reason for unassigned tasks 2011-02-02 6:15:32 AM Testing 4
15 23977 QCS-311 Tally Sheet - Reason Not Done fails to provide reason for unassigned tasks 2011-02-02 6:15:47 AM Closed 5Hi,
Welcome to the forum!
StblJmpr wrote:
... I am attempting to do some reporting from a JIRA database. What is a JIRA database?
I am attaching code and a sample list of the results. If I am not quite clear, please ask for information and I will attempt to provide more. Whenever you have a question, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and the results you want from that data.
Simplify the problem as much as possible. For example, if the part you don't know how to do only involves 2 tables, then jsut post a question involving those 2 tables. So you might just post this much data:
CREATE TABLE changegroup
( issueid NUMBER
, created DATE
, id NUMBER
INSERT INTO changegroup (issueid, created, id) VALUES (21191, TO_DATE ('2008-07-16 09:30:38 AM', 'YYYY-MM-DD HH:MI:SS AM'), 10);
INSERT INTO changegroup (issueid, created, id) VALUES (21191, TO_DATE ('2008-07-16 11:37:02 AM', 'YYYY-MM-DD HH:MI:SS AM'), 20);
INSERT INTO changegroup (issueid, created, id) VALUES (21191, TO_DATE ('2010-06-08 09:14:52 AM', 'YYYY-MM-DD HH:MI:SS AM'), 90);
INSERT INTO changegroup (issueid, created, id) VALUES (21191, TO_DATE ('2010-09-02 11:29:37 AM', 'YYYY-MM-DD HH:MI:SS AM'), 10);
INSERT INTO changegroup (issueid, created, id) VALUES (21191, TO_DATE ('2010-09-02 11:29:42 AM', 'YYYY-MM-DD HH:MI:SS AM'), 10);
INSERT INTO changegroup (issueid, created, id) VALUES (21191, TO_DATE ('2010-09-02 11:29:50 AM', 'YYYY-MM-DD HH:MI:SS AM'), 20);
INSERT INTO changegroup (issueid, created, id) VALUES (21191, TO_DATE ('2010-09-02 11:29:53 AM', 'YYYY-MM-DD HH:MI:SS AM'), 90);
INSERT INTO changegroup (issueid, created, id) VALUES (23234, TO_DATE ('2008-10-03 10:26:21 AM', 'YYYY-MM-DD HH:MI:SS AM'), 10);
INSERT INTO changegroup (issueid, created, id) VALUES (23234, TO_DATE ('2008-11-17 09:39:39 AM', 'YYYY-MM-DD HH:MI:SS AM'), 20);
INSERT INTO changegroup (issueid, created, id) VALUES (23234, TO_DATE ('2011-02-02 06:18:02 AM', 'YYYY-MM-DD HH:MI:SS AM'), 90);
INSERT INTO changegroup (issueid, created, id) VALUES (23977, TO_DATE ('2008-09-29 02:44:54 PM', 'YYYY-MM-DD HH:MI:SS AM'), 10);
INSERT INTO changegroup (issueid, created, id) VALUES (23977, TO_DATE ('2010-05-29 04:47:37 PM', 'YYYY-MM-DD HH:MI:SS AM'), 30);
INSERT INTO changegroup (issueid, created, id) VALUES (23977, TO_DATE ('2011-02-02 06:14:57 AM', 'YYYY-MM-DD HH:MI:SS AM'), 10);
INSERT INTO changegroup (issueid, created, id) VALUES (23977, TO_DATE ('2011-02-02 06:15:32 AM', 'YYYY-MM-DD HH:MI:SS AM'), 20);
INSERT INTO changegroup (issueid, created, id) VALUES (23977, TO_DATE ('2011-02-02 06:15:47 AM', 'YYYY-MM-DD HH:MI:SS AM'), 90);
CREATE TABLE changeitem
( groupid NUMBER
, newstring VARCHAR2 (10)
INSERT INTO changeitem (groupid, newstring) VALUES (10, 'Open');
INSERT INTO changeitem (groupid, newstring) VALUES (20, 'Testing');
INSERT INTO changeitem (groupid, newstring) VALUES (30, 'Blocked');
INSERT INTO changeitem (groupid, newstring) VALUES (90, 'Closed');Then post the results you want to get from that data, like this:
ISSUEID HISTORY
21191 Open (0) >> Testing (692) >> Closed
23234 Open (45) >> Testing (807) >> Closed
23977 Open (607) >> Blocked (249) >> Open (0) >> Testing (0) >> ClosedExplain how you get those results from that data. For example:
"The output contains one row per issueid. The HISTORY coloumn shows the different states that the issue went through, in order by created, starting with the earliest one and continuing up until the first 'Closed' state, if there is one. Take the first row, issueid=21191, for example. It started as 'Open' on July 16, 2008, then, on the same day (that is, 0 days later) changed to 'Testing', and then, on June 8, 2010, (692 days later), it became 'Closed'. That same issue opened again later, on September 2, 2010, but I don't want to see any activity after the first 'Closed'."
The database is 10.2.0.4That's very important. Always post your version, like you did.
Here's one way to get those results from that data:
WITH got_order_row AS
SELECT cg.issueid
, LEAD (cg.created) OVER ( PARTITION BY cg.issueid
ORDER BY cg.created
- cg.created AS days_in_stage
, ROW_NUMBER () OVER ( PARTITION BY cg.issueid
ORDER BY cg.created
) AS order_row
, ci.newstring AS change_type
FROM changegroup cg
JOIN changeitem ci ON cg.id = ci.groupid
WHERE ci.newstring IN ( 'Blocked'
, 'Closed'
, 'Testing'
, 'Open'
-- AND ... -- any other filtering goes here
SELECT issueid
, SUBSTR ( SYS_CONNECT_BY_PATH ( change_type || CASE
WHEN CONNECT_BY_ISLEAF = 0
THEN ' ('
|| ROUND (days_in_stage)
|| ')'
END
, ' >> '
, 5
) AS history
FROM got_order_row
WHERE CONNECT_BY_ISLEAF = 1
START WITH order_row = 1
CONNECT BY order_row = PRIOR order_row + 1
AND issueid = PRIOR issueid
AND PRIOR change_type != 'Closed'
ORDER BY issueid
;Combining data from several rows into one big delimited VARCHAR2 column on one row is call String Aggregation .
I hope this answers your question, but I guessed at so many things, I won't be surprised if it doesn't. If that's the case, point out where this is wrong, post what the results should be in those places, and explain how you get those results. Post new data, if necessary. -
Insert data from two rows into a single row in a new table
Hi
i have a table like the following
Deptno Dname Salary
10 Computer 2000
10 Computer 4000
10 Computer 3000
10 Science 6000
10 Science 1000
10 Science 4000
10 Science 10000
I want to insert data into a new table like the following
Deptno MaxSalCom Minsalcom MinSalSci MaxSaSci
10 2000 4000 1000 10000
Deptno--As in Table1
MaxSalCom--Maximum salary for Dname " Computer"
Minsalcom--Minimum salary for Dname " Computer"
MaxSalSci--Maximum salary for Dname " Science"
MinsalSci--Minimum salary for Dname " Science"
Please help me how to go about itwith data as
(select 10 dno, 'Computer' dname, 2000 sal FROM dual
union all
select 10, 'Computer', 4000 FROM dual
union all
select 10, 'Computer', 3000 FROM dual
UNION all
select 10, 'Science', 6000 FROM dual
union all
select 10, 'Science', 1000 FROM dual
union all
select 10, 'Science', 4000 FROM dual
union all
select 10, 'Science', 10000 FROM dual
select dno, min(decode(dname,'Computer',sal)) min_sal_comp , max(decode(dname,'Computer',sal)) max_sal_comp,
min(decode(dname,'Science',sal))min_sal_sci , max(decode(dname,'Science',sal)) max_sal_sci
from data
group by dno; -
Multiple rows into a single row.
Table structure:
CREATE TABLE JOB_ERROR_MESSAGE_TMP
ERROR_LOG VARCHAR2(1000 BYTE)
This table gets populated with multiple records (approx 20) from the log file of my application.
Example of Data (12 rows below):
ERROR_LOG
XXXXXXXX(Error)-ORA-01756-ZZZZZZZZZZZZZ-quoted string not properly terminated
line1 of error1
line2 of error1
line3 of error1
XXXXXXXX(Error)-DBS-0000-ZZZZZZZZZZZZZ-SOME ERROR
line1 of error2
line2 of error2
XXXXXXXX(Error)-DBS-1111-ZZZZZZZZZZZZZ-SOME NEW ERROR
line1 of error3
line2 of error3
line2 of error4
line4 of error3
I am interested in only the first error. That is: all the rows until the second occurrence of word "(ERROR)"
Desired output:
ORA-01756,quoted string not properly terminated,line1 of error1,line2 of error1,line3 of error1
That is:
The 9 digit code which exists after the 1st encounter of word '(Error)'||','||trim(First line after error)||','||trim(second line after error)||','||
trim of 3rd line <all the lines untill the line containing '(ERROR)' is encountered>
Any help on this is appreciated.
Thanks in advance
CoolguyExample...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 as ord, 'XXXXXXXX(Error)-ORA-01756-ZZZZZZZZZZZZZ-quoted string not properly terminated' as txt from dual union all
2 select 2, 'line1 of error1' from dual union all
3 select 3, 'line2 of error1' from dual union all
4 select 4, 'line3 of error1' from dual union all
5 select 5, 'XXXXXXXX(Error)-DBS-0000-ZZZZZZZZZZZZZ-SOME ERROR' from dual union all
6 select 6, 'line1 of error2' from dual union all
7 select 7, 'line2 of error2' from dual union all
8 select 8, 'XXXXXXXX(Error)-DBS-1111-ZZZZZZZZZZZZZ-SOME NEW ERROR' from dual union all
9 select 9, 'line1 of error3' from dual union all
10 select 10, 'line2 of error3' from dual union all
11 select 11, 'line2 of error4' from dual union all
12 select 12, 'line4 of error3' from dual)
13 -- end of test data
14 select txt
15 from (
16 select ord, txt, starter, lag(starter) over (order by ord) as prev
17 from (
18 select ord, txt, decode(instr(txt,'(Error)'),0,null,1) as starter
19 from t
20 order by ord
21 )
22 )
23 where coalesce(starter, prev) = 1
24* order by ord
SQL> /
TXT
XXXXXXXX(Error)-ORA-01756-ZZZZZZZZZZZZZ-quoted string not properly terminated
line1 of error1
XXXXXXXX(Error)-DBS-0000-ZZZZZZZZZZZZZ-SOME ERROR
line1 of error2
XXXXXXXX(Error)-DBS-1111-ZZZZZZZZZZZZZ-SOME NEW ERROR
line1 of error3
6 rows selected.
SQL>Your problem is going to be that you haven't got anything in your table which specifies the order of the rows. As such you have an unordered set of data in a table and there is nothing to say which rows of data are associated with other rows. If you want it to work you're going to have to update your table to include a sequence number or a date/timestamp. -
Combining multiple rows into a single row
Hi all,
I have a tricky situation in a HR select.
Imagine I have a dataset as below, simplified of course.
Name Start Date End date Job Title Salary
Tom 01/01/07 02/03/08 Gopher £500
Tom 03/03/08 jobsworth £600
Rick 04/05/09 Painter £500
Harry 02/06/07 02/06/08 Gardener £300
Harry 03/06/08 03/06/09 Runner £200
Harry 04/06/09 Cook £400
now, I need to select from above and return 3 rows so it looks as below
name start date enddate title salary start date enddate title salary start date enddate title salary etc etc
tom 01/01/07 02/03/08 gopher £500 03/03/08 blah 600
Rick 04/05/09 painter £500
harry etc etc etc
Now, I know how to select onto one row ok, asumming that each employee has a fixed number of roles but the problem is that each employee has a different number of jobs, one could have had 5 while another 50 and I do not know the maximum at this time.
Anyone have any ideas on how to appraoch this?
tia,
dw
Edited by: derrywriter on Oct 2, 2009 3:50 PM
Edited by: derrywriter on Oct 2, 2009 3:54 PMIdeally this should be done in a suitable reporting tool.
Standard SQL requires a deterministic number of columns to be known at parse time i.e. before the data is fetched it needs to know how many columns are being returned.
If you know there is a fixed maximum to the number of columns that can be returned you can use one of the various pivot methods (search the forum) which differ depending on whether you're using 9i, 10g or 11g database.
If you can't determine a maximum number of columns you're pretty much stuck, unless you want to write some clever interfacing to the oracle ODCI as demonstrated in this thread:
How to pipeline a function with a dynamic number of columns?
Personally, I believe such reporting styles should be reserved for reporting tools. -
Put more than number of rows into a single row
i have to insert data frm source table to destination table ,bellow i mention the source table and in wich format i want the output also mention it,i want some plsql procedure or function to insert data right format in destination table .
one thing u can not apply appno in destination table.
thanx in advance .
for example appno 3 used 2 times in source thatwhy in destination table i hav 2 put
share1,rate1,share2and rate2,
for application (appno 5) column it used 3 times so
i have 2 put first one share1,rate1,second one share2 ,rate2 , thrid one share3 and rate 3
hope problem u people understand very well.
source table
appno share ammt
1 10 12
2 12 15
3 15 34
3 10 45
4 12 55
5 12 56
5 11 56
5 13 24
Destination table
appno share1 rate 1 share2 rate2 share3 rate3
1 10 12
2 12 15
3 15 34 10 45
4 12 55
5 12 56 11 56 13 24
u cannot apply appno as a primary key in destination table.
regards
amitHere's one way (modify to your needs):
SQL> col d1 format a20
SQL> col d2 format a20
SQL> col d3 format a20
SQL> select location_id
2 , max(decode(rn,1,department_name)) d1
3 , max(decode(rn,2,department_name)) d2
4 , max(decode(rn,3,department_name)) d3
5 from ( select row_number() over ( partition by location_id order by department_id ) rn
6 , location_id
7 , department_name
8 from departments
9 )
10 group by location_id
11 /
LOCATION_ID D1 D2 D3
1400 IT
1500 Shipping
1700 Administration Purchasing Executive
1800 Marketing
2400 Human Resources
2500 Sales
2700 Public Relations
7 rows selected.MHE -
Convert multiple rows into a single row
Hi friends..
I have a table with the following information
SQL> select * from tsting;
A B C D E
10
10
10
29-MAY-09
TESTBut I need the following result..
A B C D E
10 10 10 29-MAY-09 TESTHello,
Try:
WITH test_data AS (
SELECT 10 A, NULL B, NULL C, NULL D, NULL E FROM DUAL UNION ALL
SELECT NULL A, 10 B, NULL C, NULL D, NULL E FROM DUAL UNION ALL
SELECT NULL A, NULL B, 10 C, NULL D, NULL E FROM DUAL UNION ALL
SELECT NULL A, NULL B, NULL C, TO_DATE('29-MAY-09','DD-MON-YY') D, NULL E FROM DUAL UNION ALL
SELECT NULL A, NULL B, NULL C, NULL D, 'TEST' E FROM DUAL)
-- end test data
SELECT MAX(A) A, MAX(B) B, MAX(C) C, MAX(D) D, MAX(E) E
FROM test_data;
A B C D E
10 10 10 29-MAY-09 TEST -
Merging two records in a single record for the same matnr,werks and bwart.
Hi I have a requirement to merge two records into one single record using the quantity field from MSEG table.
I am selecting two records from the table MSEG. Now for these two records i have to add the quantity values into one variable.
Then there should be only one record for the same matnr,werks,bwart.
Any suggestion would be appreciated.
Regards,
Amithi,
You can COLLECT statement only if all non key fields are numeric.
This statement Adds all such fields and give us a single record.
Regards
Sumit Agarwal -
Need help converting multipe rows in a single row
I want to convert multiple rows into a single row. Oracle version is 10.2
SQL> with abc
2 as
3 (select 1 a, 2 b, 3 c from dual union
4 select 4, 5, 6 from dual)
5 select * from abc;
A B C
1 2 3
4 5 6I want the output as
A B C
1,4 2,5 3,6Thanks,
Gangadhar
Edited by: GR on Feb 6, 2012 9:35 PMGR wrote:
I want to convert multiple rows into a single row. Oracle version is 10.2
SQL> with abc
2 as
3 (select 1 a, 2 b, 3 c from dual union
4 select 4, 5, 6 from dual)
5 select * from abc;
A B C
1 2 3
4 5 6I want the output as
A B C
1,4 2,5 3,6
You haven't specified if there's a particular order to the data, so I've assumed one...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 a, 2 b, 3 c from dual union
2 select 4, 5, 6 from dual)
3 --
4 -- end of test data
5 --
6 select trim(',' from sys_connect_by_path(a,',')) as a
7 ,trim(',' from sys_connect_by_path(b,',')) as b
8 ,trim(',' from sys_connect_by_path(c,',')) as c
9 from (select a,b,c,row_number() over (order by a) as rn from t)
10 where connect_by_isleaf = 1
11 connect by rn = prior rn+1
12* start with rn = 1
SQL> /
A B C
1,4 2,5 3,6
SQL> -
Merge multiple rows to get single row result
Hi,
Below is the sample data from my table USERINFO-
personid userid username orgid
1234 1235 TOM 1
2345 2346 HARRY 1
1111 2222 BOND 2
3333 4444 BERY 2
Now I want a query which can give me the following result-
personid userid username personid userid username orgid
1234 1235 TOM 2345 2346 HARRY 1
1111 2222 BOND 3333 4444 BERY 2
i.e., two records belonging to the same orgid should be merged into a single row resulset.
Please let me know the SQL query to get the desired result.
ThanksSQL> select * from test_otn;
PERSONID USERID USERNAME ORGID
1234 1235 TOM 1
2345 2346 HARRY 1
1111 2222 BOND 2
3333 4444 BERY 2
SQL> select max(decode(rn, 1, personid, null)) person_id1,
2 max(decode(rn, 2, personid, null)) person_id2,
3 max(decode(rn, 1, userid, null)) user_id1,
4 max(decode(rn, 2, userid, null)) user_id2,
5 max(decode(rn, 1, username, null)) username1,
6 max(decode(rn, 2, username, null)) username2,
7 orgid
8 from
9 (select row_number() over (partition by orgid order by orgid) rn,
10 personid, userid, username, orgid from test_otn)
11 group by orgid
12 /
PERSON_ID1 PERSON_ID2 USER_ID1 USER_ID2 USERNAME1 USERNAME2 ORGID
1234 2345 1235 2346 TOM HARRY 1
1111 3333 2222 4444 BOND BERY 2
Thx,
SriDHAR
Maybe you are looking for
-
Full screen caller image display
Full Screen Caller image was one of the best feature of an iPhone that made look iPhone simpler and fantastic to use.. This particular feature seems to have been last with the upgrade to iOS 7.1.. Please fix this issue.. Full screen caller image disp
-
TEM: Relationship between External Person and Business Event
Hi, The requirment is to have External Person to be related to Business Events. Please let me know whether it will be ideal to relate External Person to Business Events or External Person to Business Event Type? NOTE:As per the standard the relations
-
French quotation marks (« and ») and iPhone 4?
How can I get French quotation marks (« and ») on the iPhone 4?
-
Error detail in data migration?
Hi all, I'm migrating an Access database to Oracle using SQL Developer and the migration workbench. When I run the data migration, it almost all works, but one table has one error. I get a list of tables, with rows, total rows, errors and total error
-
Proocess flow for solution manager
hi friends, could you please provide pictorial representation for solution manager tool in sap(used in support project) would be appriciated, Regards, divya