Concatenate records from same column? - postgresql

I have written a query with the following purpose: there are four visits (visname) in my "DAT_PAGS" dataset:
Pre-procedure,
Procedure,
Post-Procedure,
End of Study
If for any of these visits in the table, there are both records with statusid ='0' and statusid = '5' (i.e. entered or no data), I want the page names (pagename) to appear in my output (see below).
My question is twofold:
-Is there a way to concatenate all the pagenames per visit in my output? So that I have only one row per subnum?
-Is there a better way to do this? I am still getting the hang of SQL, so if this is a ridiculous approach, feedback is much appreciated!
My query:
SELECT
DISTINCT "DAT_SUB".SUBNUM as "Subject ID",
nodatapreproc.pagename as "Pre-Procedure",
nodataproc.pagename as "Procedure",
nodatapostproc.pagename as "Post-Procedure",
nodataEOS as "End Of Study"
FROM
"Disco_Radial"."DAT_SUB"
LEFT JOIN (SELECT subnum, visname, pagename, statusid FROM "Disco_Radial"."DAT_PAGS" where
("Disco_Radial"."DAT_PAGS".statusid = '5' AND "DAT_PAGS".visname = 'Post-Procedure')) postprocentered
on "DAT_SUB".subnum = postprocentered.subnum
LEFT JOIN (SELECT subnum, visname, pagename, statusid FROM "Disco_Radial"."DAT_PAGS" where
("Disco_Radial"."DAT_PAGS".statusid = '0' AND "DAT_PAGS".visname = 'Post-Procedure')) nodatapostproc
ON nodatapostproc.subnum = postprocentered.subnum
LEFT JOIN (SELECT subnum, visname, pagename, statusid FROM "Disco_Radial"."DAT_PAGS" where
("Disco_Radial"."DAT_PAGS".statusid = '5' AND "DAT_PAGS".visname = 'Pre-Procedure')) preprocentered
on "DAT_SUB".subnum = preprocentered.subnum
LEFT JOIN (SELECT subnum, visname, pagename, statusid FROM "Disco_Radial"."DAT_PAGS" where
("Disco_Radial"."DAT_PAGS".statusid = '0' AND "DAT_PAGS".visname = 'Pre-Procedure')) nodatapreproc ON
nodatapreproc.subnum = preprocentered.subnum
LEFT JOIN (SELECT subnum, visname, pagename, statusid FROM "Disco_Radial"."DAT_PAGS" where
("Disco_Radial"."DAT_PAGS".statusid = '5' AND "DAT_PAGS".visname = 'Procedure')) procentered on
"DAT_SUB".subnum = procentered.subnum
LEFT JOIN (SELECT subnum, visname, pagename, statusid FROM "Disco_Radial"."DAT_PAGS" where
("Disco_Radial"."DAT_PAGS".statusid = '0' AND "DAT_PAGS".visname = 'Procedure')) nodataproc ON
nodataproc.subnum = procentered.subnum
LEFT JOIN (SELECT subnum, visname, pagename, statusid FROM "Disco_Radial"."DAT_PAGS" where
("Disco_Radial"."DAT_PAGS".statusid = '5' AND "DAT_PAGS".visname = 'End Of Study')) EOSentered on
"DAT_SUB".subnum = EOSentered.subnum
LEFT JOIN (SELECT subnum, visname, pagename, statusid FROM "Disco_Radial"."DAT_PAGS" where
("Disco_Radial"."DAT_PAGS".statusid = '0' AND "DAT_PAGS".visname = 'End Of Study')) nodataEOS ON
nodataEOS.subnum = EOSentered.subnum
ORDER BY "DAT_SUB".subnum;
Part of my output:

I think you can do that with conditional aggregation
SELECT ds.subnum as "Subject ID",
string_agg(pagename, ',') filter (where dp.statusid = '0' AND dp.visname = and 'Pre-Procedure') as "Pre-Procedure",
string_agg(pagename, ',') filter (where dp.statusid = '5' AND dp.visname = and 'Pre-Procedure') as "???",
string_agg(pagename, ',') filter (where dp.statusid = '0' AND dp.visname = and 'Procedure') as "Procedure",
string_agg(pagename, ',') filter (where dp.statusid = '0' AND dp.visname = and 'Post-Procedure') as "Post-Procedure",
string_agg(pagename, ',') filter (where dp.statusid = '5' AND dp.visname = and 'Post-Procedure') as "?????",
string_agg(pagename, ',') filter (where dp.statusid = '0' AND dp.visname = and 'End Of Study') as "End Of Study"
FROM "Disco_Radial"."DAT_SUB" ds
LEFT JOIN "Disco_Radial"."DAT_PAGS" dp ON ds.subnum = dp.subnum
GROUP BY ds.subnum;

Related

how to use case with min and mx with group by function?

i try to getting report for using below query, getting result but status showing only approved for all rows need actual status
''''
SELECT 'LOAN NO','BRANCH NAME','STATE','PRODUCT','SCHEME','LOAN AMOUNT','APPROVAL DATE'
,'LOAN_DISBURSAL_DATE','DISBURSAL_NO','STATUS','MAKER ID','AUTHOR ID',
'OPERATION_DATE','SYSTEM DATE','REMARKS'
UNION (
SELECT LON.LOAN_NO,BR.BRANCH_DESC,ST.STATE_DESC,PR.PRODUCT_DESC,SC.SCHEME_DESC,LON.LOAN_LOAN_AMOUNT,LON.LOAN_APPROVAL_DATE,
LON.LOAN_DISBURSAL_DATE,HST.DISBURSAL_NO,
(select
case
when OPERATION_STATUS = min('P') THEN 'INITIATED'
when OPERATION_STATUS = min('S') THEN 'SEND BACK'
when OPERATION_STATUS = max('A') THEN 'APPROVED'
when OPERATION_STATUS = 'D' THEN 'DELETED'
when OPERATION_STATUS = 'F' THEN 'FORWARDED'
when OPERATION_STATUS = 'X' THEN 'REJECTED'
END AS STATUS from CR_LOAN_DISBURSAL_DTL_HST) ,
USER_ID,HST.AUTHOR_ID,HST.OPERATION_BUSINESS_DATE,HST.OPERATION_SYSTEM_DATE,
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace
(replace(replace(concat
(ifnull(HST.REMARKS,''),' '),'!',''),'%',''),'(',''),')',''),'-',''),'{',''),'[',''),'}',''),']',''),':',''),';',''),'/',''),'?',''),'*',''),',',''),'&','AND'),' ',' '),'""',''),'" "',''),'\n',''),'"',''),'''','') REMARKS
FROM CR_LOAN_DISBURSAL_DTL_HST HST
JOIN CR_LOAN_DTL LON ON LON.LOAN_ID=HST.LOAN_ID
JOIN COM_BRANCH_M BR ON LON.LOAN_BRANCH=BR.BRANCH_ID
JOIN COM_STATE_M ST ON ST.STATE_ID=BR.STATE_ID
JOIN CR_PRODUCT_M PR ON PR.PRODUCT_ID=LON.LOAN_PRODUCT
JOIN CR_SCHEME_M SC ON SC.SCHEME_ID=LON.LOAN_SCHEME
where
DATE(HST.OPERATION_BUSINESS_DATE)>=DATE('2022-08-01')
AND DATE(HST.OPERATION_BUSINESS_DATE)<=DATE('2022-08-31')
)

Refine data elements using having clause tsql

I'm trying to pull a dataset that returns records ONLY when there are two QUALIFERs present. I've tried left joins, populating data in temp tables then manipulating something, then numerous having clauses (resulting in subquery selects, and additional groups). I would appreciate any assistance on what I can do further.
Query:
Select E, CASE WHEN QUALIFER = '1' THEN 'NAME1' WHEN QUALIFER = '2' then 'NAME2' ELSE 'FINALNAME' END AS TYPE, count(rt.ID) 'Number '
from TABLE_ONE co (nolock)
join TABLE_TWO rt (nolock)
on co.ID = rt.ID
where co.E in (select * from #tempEmail)
AND convert(date,co.INSERTED_TIMESTAMP)between '1/1/2020' and '8/15/2020'
AND TRANS_STATUS = 'APPROVED'
group by E, QUALIFER
order by E, QUALIFER
Current resultset:
E TYPE Number
FAKEEMAIL1#Gmail NAME1 1
FAKEEMAIL1#Gmail NAME2 1
otheremailj#gmail.com Name1 21
Desired resultset:
E TYPE Number
FAKEEMAIL1#Gmail NAME1 1
FAKEEMAIL1#Gmail NAME2 1
Thank you.
Let's try the below query. I used a temp table to make things more simple for my mind.
if object_id('tempdb.dbo.#email') is not null drop table #email
create table #email
(
email varchar(50),
typeValue varchar(15),
Number int
)
insert into #email(email, typeValue, Number)
Select
E,
CASE WHEN QUALIFER = '1' THEN 'NAME1' WHEN QUALIFER = '2' then 'NAME2' ELSE 'FINALNAME' END AS TYPE,
count(rt.ID) 'Number '
from TABLE_ONE co (nolock)
join TABLE_TWO rt (nolock)
on co.ID = rt.ID
where co.E in (select * from #tempEmail)
AND convert(date,co.INSERTED_TIMESTAMP)between '1/1/2020' and '8/15/2020'
AND TRANS_STATUS = 'APPROVED'
group by E, QUALIFER
select a.email, a.typeValue
from #email a
inner join
(
select email, typeValue, rank() over (partition by email order by typeValue) as typeCount
from #email t
) as b
on b.email = a.email
and b.typeCount > 1

Prompts not working correctly in Crystal Reports

I have a report which I would like to create prompts for both VENUE and DATE RANGE, and after having these prompts bring back the incorrect data, I have deleted both and tried to start again from the beginning.
To begin with, I have a Crystal Report based upon the below SQL:
To summarise the code - it uses a common table expression broken separating the query out into revenue, space and event, finally joining this together at the end.
I had to convert the data type of the 'booking date' to DATE, because the value is stored as 2018-01-01 12:00:00:00.
WITH Revenue as
(
SELECT EV200_EVENT_MASTER.EV200_EVT_ID as [Event],
Revenue =
SUM(Case
When Orddtl.ER101_PHASE = '1' and ORDDTL.ER101_COMPL_STS = 'N'
then ORDDTL.ER101_EXT_CHRG
When ORDDTL.ER101_PHASE = '5' and ORDDTL.ER101_COMPL_STS = 'N'
then ORDDTL.ER101_EXT_CHRG
Else 0
End),
SM.EV800_SPACE_DESC,
SM.EV800_SPACE_CODE
FROM EV200_EVENT_MASTER WITH (NOLOCK)
LEFT OUTER JOIN EV870_ACCT_MASTER PrimCoord WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = PrimCoord.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_1 = PrimCoord.EV870_ACCT_CODE -- Event manager
LEFT OUTER JOIN EV870_ACCT_MASTER FloorMgr WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = FloorMgr.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_2 = FloorMgr.EV870_ACCT_CODE -- Floor manager
LEFT OUTER JOIN EV870_ACCT_MASTER EventAccount WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EventAccount.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_CUST_NBR = EventAccount.EV870_ACCT_CODE -- The person who made the booking
LEFT OUTER JOIN EV215_EVT_TYPE WITH (NOLOCK) -- get the event type
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV215_EVT_TYPE.EV215_ORG_CODE
AND EV200_EVENT_MASTER.EV200_EVT_TYPE = EV215_EVT_TYPE.EV215_EVT_TYPE
LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK) -- this might not be necessary. Only if we want to show status = completed, etc
ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE
LEFT OUTER JOIN EV802_SPACE_BKD SPBK
ON SPBK.EV802_ORG_CODE = EV200_ORG_CODE AND SPBK.EV802_EVT_ID = EV200_EVT_ID
LEFT OUTER JOIN ER101_ACCT_ORDER_DTL ORDDTL
ON ORDDTL.ER101_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE
AND ORDDTL.ER101_EVT_ID = EV200_EVENT_MASTER.EV200_EVT_ID
LEFT OUTER JOIN EV800_SPACE_MASTER SM
ON Sm.EV800_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE
AND SM.EV800_SPACE_CODE = SPBK.EV802_BKD_SPACE
WHERE EV200_EVENT_MASTER.EV200_ORG_CODE = '10'
AND EV200_EVENT_MASTER.EV200_EVT_STATUS >= 30 /* only confirmed bookings */
AND EV200_EVENT_MASTER.EV200_EVT_STATUS <= 52
AND Not(EV200_EVENT_MASTER.EV200_EVT_TYPE = 'GB') -- exclude group bookings
GROUP BY EV200_EVENT_MASTER.EV200_EVT_ID,SM.EV800_SPACE_DESC,EV800_SPACE_CODE
), eventdetails as
(
SELECT EV200_EVENT_MASTER.EV200_EVT_ID as [Event],
EV215_EVT_TYP_DESC as [Event Type],
EV200_event_master.EV200_EVT_DESC,
EV200_EVENT_MASTER.EV200_PLN_ATTEND,
--EventAccount.EV870_NAME AS [Account],
PrimCoord.EV870_FIRST_NAME + ' ' + PrimCoord.EV870_LAST_NAME AS [Event Manager],
--FloorMgr.EV870_FIRST_NAME + ' ' + FloorMgr.EV870_LAST_NAME AS [Floor Manager]
EV130_STATUS_MASTER.EV130_STATUS_DESC AS 'Status'
FROM EV200_EVENT_MASTER WITH (NOLOCK)
LEFT OUTER JOIN EV870_ACCT_MASTER PrimCoord WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = PrimCoord.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_1 = PrimCoord.EV870_ACCT_CODE -- Event manager
LEFT OUTER JOIN EV870_ACCT_MASTER FloorMgr WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = FloorMgr.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_COORD_2 = FloorMgr.EV870_ACCT_CODE -- Floor manager
LEFT OUTER JOIN EV870_ACCT_MASTER EventAccount WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EventAccount.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_CUST_NBR = EventAccount.EV870_ACCT_CODE -- The person who made the booking
LEFT OUTER JOIN EV215_EVT_TYPE WITH (NOLOCK) -- get the event type
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV215_EVT_TYPE.EV215_ORG_CODE
AND EV200_EVENT_MASTER.EV200_EVT_TYPE = EV215_EVT_TYPE.EV215_EVT_TYPE
LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK) -- this might not be necessary. Only if we want to show status = completed, etc
ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE
WHERE EV200_EVENT_MASTER.EV200_ORG_CODE = '10'
AND EV200_EVENT_MASTER.EV200_EVT_STATUS >= 30 /* only confirmed bookings */
AND EV200_EVENT_MASTER.EV200_EVT_STATUS <= 52
AND Not(EV200_EVENT_MASTER.EV200_EVT_TYPE = 'GB') -- exclude group bookings
), spacedetails as
(
SELECT distinct
SPDTL.EV803_BKD_SPACE,
SPDTL.EV803_EVT_ID,
SM.EV800_SPACE_DESC,
CONVERT(DATE,SPDTL.EV803_BKG_DATE) as bkg_date,
spdtl.EV803_START_TIME,
spdtl.EV803_END_TIME,
SPDTL.EV803_BKG_START_TIME,
SPDTL.EV803_BKG_END_TIME,
SPDTL.EV803_USAGE,
EV800_NOTE_1 AS [VENUE]
FROM EV803_SPACE_BKD_DTL SPDTL
INNER JOIN EV800_SPACE_MASTER SM ON SPDTL.EV803_BKD_SPACE = SM.EV800_SPACE_CODE
)
select distinct eventdetails.Event,
[Event Type],
eventdetails.EV200_EVT_DESC as 'Description',
eventdetails.EV200_PLN_ATTEND as 'PAX',
eventdetails.[Event Manager],
ss.EV800_SPACE_DESC as 'Space',
ss.bkg_date as 'Booking Date',
ss.VENUE,
eventdetails.Status,
/*spacedetails.
(CASE
WHEN spacedetails.EV803_USAGE = 'IN' THEN ''
WHEN spacedetails.EV803_USAGE = 'ET' THEN 'EVENT'
WHEN spacedetails.EV803_USAGE = 'OUT' THEN 'BUMP OUT'
END) as 'Booking Type',
*/
/*this doesnt work because it will be evaluated one row at a time, and one row will not satisfy in,et,out
CAST((CASE WHEN spacedetails.ev803_usage = 'IN' THEN spacedetails.EV803_BKG_START_TIME END) as time(0)) as 'Bump in Start Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'IN' THEN spacedetails.EV803_BKG_END_TIME END) as time(0)) as 'Bump in End Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'ET' THEN spacedetails.EV803_BKG_START_TIME END) as time(0)) as 'Event Start Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'ET' THEN spacedetails.EV803_BKG_END_TIME END) as time(0)) as 'Event End Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'OUT' THEN spacedetails.EV803_BKG_START_TIME END) as time(0)) as 'Bump Out Start Time',
CAST((CASE WHEN spacedetails.ev803_usage = 'OUT' THEN spacedetails.EV803_BKG_END_TIME END) as time(0)) as 'Bump Out End Time',
*/
(
SELECT TOP(1) CAST(s.EV803_BKG_START_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'IN'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-in Start Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_END_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'IN'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-in End Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_START_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'ET'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Event Start Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_END_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'ET'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Event End Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_START_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'OUT'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-out Start Time',
(
SELECT TOP(1) CAST(s.EV803_BKG_END_TIME AS time(0))
FROM spacedetails s
WHERE s.EV803_EVT_ID = ss.EV803_EVT_ID
AND s.EV803_USAGE = 'OUT'
AND s.EV803_BKD_SPACE = ss.EV803_BKD_SPACE
AND s.bkg_date = ss.bkg_date
) AS 'Bump-out End Time',
revenue.revenue
from eventdetails
inner join spacedetails ss on eventdetails.Event = ss.EV803_EVT_ID
inner join Revenue on ss.EV803_EVT_ID = revenue.Event and ss.EV803_BKD_SPACE = Revenue.EV800_SPACE_CODE
I created a Dynamic Parameter on the 'Booking date' setting allow for 'range values',
but when i try to run the report (it is deployed in Ungerboeck Event Management) I only get the prompt for a non range value.
I was able to resolve this issue.
In my original query, I referenced two datetime fields, and converted one to a date (shown below) but failed to explicitly convert the second.
FIRST > CONVERT(DATE,SPDTL.EV803_BKG_DATE) as bkg_date,
SECOND > ss.bkg_date as 'Booking Date',
After making the change to convert the second to date, the prompt worked as it should have.

i am geting error: PLS-00402: alias required in SELECT list of cursor to avoid duplicate column names

I have created this procedure with select data prom different tables
and populate it into one table. i am getting error:"PLS-00402: alias
required in SELECT list of cursor to avoid duplicate column names"
while compling the code
create or replace procedure hrps.Employeeall
is
cursor EMA_FILL_EMPL_ALL
IS
SELECT DISTINCT base.eme_perno " EMPL BASE PNO",
CAR.CAR_COMP_CD "COMP CODE",
PER.EPR_SALUTATION,
PER.EPR_FST_NAME,
PER.EPR_MID_NAME,
PER.EPR_LST_NAME,
TRIM(per.EPR_SALUTATION) || ' '||TRIM(per.EPR_FST_NAME) || ' ' || TRIM(per.EPR_MID_NAME) || ' ' || TRIM(per.EPR_LST_NAME) "Name",
BASE.EME_DOB,
per.epr_gender "GENDER",
NULL z1,
PER.EPR_DIFF_ABLE,
PER.EPR_DIFF_ABLE_DESC,
BASE.EME_BLOOD_GRP,
PER.EPR_AFF_ACT,
DOJ.idt_dt,
BASE.EME_CADRE,
C3.IRC_DESC cd3,
car.car_emp_class,
CAR.CAR_EMP_TYP,
BASE.EME_EMPL_STATUS,
BASE.EME_SOURCE_EMPLMNT,
CAR.CAR_PYRL,
PYRL.IPY_PYRL_DESC,
car.car_dept_cd "DEPT CODE",
org_dept.oro_desc "DEPT DESC",
car.car_sec_cd "SECTION CODE",
org_sec.oro_desc "SECTION DESC",
car.car_desgn_cd "DESGN CODE",
DES.IDS_DESGN_DESC "DESGN DESC",
CAR.CAR_PER_GRADE_CD,
CAR.CAR_SUB_GRADE_CD,
NULL z2,
NULL z3,
NULL z4,
NULL z5,
CAR.CAR_PYRL_AREA,
C1.IRC_DESC cd1,
CAR.CAR_COST_CENT_CD,
C2.IRC_DESC cd2,
NULL z6,
NULL z7,
DOS.IDT_DT,
CAR_SEP.CAR_ACTION_RSN,
C4.IRC_DESC cd4,
PDT.IDT_DT,
EDR.IDT_DT,
NULL z8,
PROM.IDT_DT,
INC.IDT_DT,
NULL z9,
NULL z10,
C5.IRC_DESC cd5,
car.car_exec_cd "EXEC HEAD",
org_exe.oro_desc "EXEC HEAD DESC",
car.car_grp_cd "GROUP CODE",
org_grp.oro_desc "GROUP DESC",
car.car_pu_cd " PU CODE",
org_pu.idp_pu_desc "PU DESC",
car.car_position_id "POSITION ID",
NULL z11,
NULL z12,
NULL z13,
NULL z14,
FATHER.FML_FSTNAME || ' ' || FATHER.FML_MIDNAME || ' ' || FATHER.FML_LSTNAME F_NAME,
SPOUCE.FML_FSTNAME || ' ' || SPOUCE.FML_MIDNAME || ' ' || SPOUCE.FML_LSTNAME S_NAME,
TRGSD.IDT_DT,
TRGED.IDT_DT,
NULL z15,
NULL z16,
NULL z17,
NULL z18,
PAN.EID_ID,
PHONE.EMC_CONTACT,
EMAIL.EMC_CONTACT,
PER.EPR_RELIGION,
PER.EPR_NATIVE_STATE,
PER.EPR_NATIONALITY,
NULL z19,
NULL z20,
NULL z21,
SYSDATE date1,
'157068' num1,
SYSDATE date2,
'157068' num2
fROM t_empl_base base
LEFT OUTER JOIN
t_ir_em_career_dtls car
ON base.eme_perno = car.car_eme_perno
AND car.car_end_dt = '31-dec-9999'
LEFT OUTER JOIN
t_ir_em_career_dtls car_SEP
ON base.eme_perno = car_SEP.car_eme_perno
AND CAR_SEP.CAR_ACTION_TYPE ='SP'
LEFT OUTER JOIN
t_ir_em_personal per
ON base.eme_perno = per.epr_eme_perno
AND per.epr_end_dt = '31-dec-9999'
LEFT OUTER JOIN
T_IR_DESGN DES
ON DES.IDS_DESGN_CODE= CAR.CAR_DESGN_CD
AND DES.IDS_LOCATION = CAR.CAR_LOC_CD
AND DES.IDS_END_DT='31-DEC-9999'
LEFT OUTER JOIN
t_ir_em_impdt pdt
ON base.eme_perno = pdt.idt_eme_perno
AND pdt.idt_type = '03'
LEFT OUTER JOIN
t_ir_em_impdt DOJ
ON base.eme_perno = DOJ.idt_eme_perno
AND DOJ.idt_type = '02'
LEFT OUTER JOIN
t_ir_em_impdt DOS
ON base.eme_perno = DOS.idt_eme_perno
AND DOS.idt_type = '04'
LEFT OUTER JOIN
t_ir_em_impdt EDR
ON base.eme_perno = EDR.idt_eme_perno
AND EDR.idt_type = '07'
LEFT OUTER JOIN
t_ir_em_impdt PROM
ON base.eme_perno = PROM.idt_eme_perno
AND PROM.idt_type = '08'
LEFT OUTER JOIN
t_ir_em_impdt INC
ON base.eme_perno = INC.idt_eme_perno
AND INC.idt_type = '09'
LEFT OUTER JOIN
t_ir_em_impdt TRGSD
ON base.eme_perno = TRGSD.idt_eme_perno
AND TRGSD.idt_type = '12'
LEFT OUTER JOIN
t_ir_em_impdt TRGED
ON base.eme_perno = TRGSD.idt_eme_perno
AND TRGSD.idt_type = '13'
LEFT OUTER JOIN
T_IR_EM_FAMILY FATHER
ON FATHER.FML_EME_PERNO= BASE.EME_PERNO
AND FATHER.FML_TYPE ='06'
LEFT OUTER JOIN
T_IR_EM_FAMILY SPOUCE
ON SPOUCE.FML_EME_PERNO= BASE.EME_PERNO
AND SPOUCE.FML_TYPE IN ('01','02')
LEFT OUTER JOIN
T_IR_PYRL PYRL
ON CAR.CAR_PYRL = PYRL.IPY_PYRL_NO
AND PYRL.IPY_END_DATE = '31-DEC-9999'
AND PYRL.IPY_LOCN_CD = CAR.CAR_LOC_CD
AND PYRL.IPY_VALID_TAG ='Y'
LEFT OUTER JOIN
t_ir_codes c2
ON TRIM (CAR.CAR_COST_CENT_CD ) = c2.irc_code
AND c2.irc_type = 'COSTC'
LEFT OUTER JOIN
t_ir_codes c1
ON TRIM (CAR.CAR_PYRL_AREA ) = c1.irc_code
AND c1.irc_type = 'PYARA'
LEFT OUTER JOIN
t_ir_codes c3
ON BASE.EME_CADRE = c3.irc_code
AND c3.irc_type = 'CADRE'
LEFT OUTER JOIN
t_ir_codes c4
ON CAR_SEP.CAR_ACTION_RSN = c4.irc_code
AND c4.irc_type = 'SEPAR'
LEFT OUTER JOIN
t_ir_codes c5
ON CAR.CAR_COMP_CD = c5.irc_code
AND c5.irc_type = 'COMP'
LEFT OUTER JOIN
T_IR_EM_ID PAN
ON PAN.EID_EME_PERNO=BASE.EME_PERNO
AND PAN.EID_ID_CODE= '02'
LEFT OUTER JOIN
T_IR_EM_CONTACT PHONE
ON PHONE.EMC_EME_PERNO =BASE.EME_PERNO
AND PHONE.EMC_CONTACT_TYPE= '04'
AND PHONE.EMC_END_DATE='31-DEC-9999'
LEFT OUTER JOIN
T_IR_EM_CONTACT EMAIL
ON EMAIL.EMC_EME_PERNO =BASE.EME_PERNO
AND EMAIL.EMC_CONTACT_TYPE= '03'
AND EMAIL.EMC_END_DATE='31-DEC-9999'
LEFT OUTER JOIN
(SELECT DISTINCT oro_object_id, oro_desc
FROM t_org_object o
WHERE o.oro_str_type = 'IRSTR'
AND o.oro_end_date = '31-DEC-9999'
AND o.oro_type = 'EX') org_exe
ON org_exe.oro_object_id = car.car_exec_cd
LEFT OUTER JOIN
(SELECT DISTINCT oro_object_id, oro_desc
FROM t_org_object o
WHERE o.oro_str_type = 'IRSTR'
AND o.oro_end_date = '31-DEC-9999'
AND o.oro_type = 'GR') org_grp
ON org_grp.oro_object_id = car.car_grp_cd
LEFT OUTER JOIN
(SELECT DISTINCT oro_object_id, oro_desc
FROM t_org_object o
WHERE o.oro_str_type = 'IRSTR'
AND o.oro_end_date = '31-DEC-9999'
AND o.oro_type = 'DP') org_dept
ON org_dept.oro_object_id = car.car_dept_cd
LEFT OUTER JOIN
(SELECT DISTINCT oro_object_id, oro_desc
FROM t_org_object o
WHERE o.oro_str_type = 'IRSTR'
AND o.oro_end_date = '31-DEC-9999'
AND o.oro_type = 'SE') org_sec
ON org_sec.oro_object_id = car.car_sec_cd
LEFT OUTER JOIN
(SELECT DISTINCT idp_pu_code, idp_pu_desc
FROM t_ir_dept_pu o
WHERE o.idp_end_dt = '31-DEC-9999') org_pu
ON org_pu.idp_pu_code = car.car_pu_cd
WHERE base.eme_empl_status = '1';
ct EMA_FILL_EMPL_ALL%ROWTYPE;
BEGIN
FOR CT IN EMA_FILL_EMPL_ALL
LOOP
INSERT INTO T_EMPL_COMN (
EMA_PERNO,
EMA_COMP_CD,
EMA_SALUTATION,
EMA_FST_NAME,
EMA_MID_NAME,
EMA_LST_NAME,
EMA_ENAME,
EMA_BIRTH_DT,
EMA_GENDER,
EMA_MARITAL_STATUS,
EMA_DIFF_ABLE_CD,
EMA_DIFF_ABLE_DESC,
EMA_BLOODGRP,
EMA_AFFIRMATIVE_ACTION_CD,
EMA_JOINING_DT,
EMA_CADRE,
EMA_CADRE_DESC,
EMA_EMP_CLASS,
EMA_EMP_TYPE,
EMA_EMPL_STATUS,
EMA_SOURCE_EMPLMNT,
EMA_PYRL,
EMA_PYRL_DESC,
EMA_DEPT_CD,
EMA_DEPT_DESC,
EMA_SECTION_CD,
EMA_SECTION_DESC,
EMA_DESGN_CD,
EMA_DESGN_DESC,
EMA_EMPL_PGRADE,
EMA_EMPL_SGRADE,
EMA_PERS_AREA,
EMA_PERS_AREA_DESC,
EMA_PERS_SUBAREA,
EMA_PERS_SUBAREA_DESC,
EMA_PYRL_AREA,
EMA_PYRL_AREA_DESC,
EMA_COST_CENTER_CD,
EMA_COST_CENTER_DESC,
EMA_PROFIT_CENTER_CD,
EMA_PROFIT_CENTER_DESC,
EMA_DISCH_DT,
EMA_DISCH_RSN_CD,
EMA_DISCH_RSN_DESC,
EMA_CS_DATE,
EMA_EXP_RETIRE_DT,
EMA_TRF_DT,
EMA_PROM_DT,
EMA_INCR_DT,
EMA_UNION_IND,
EMA_UNION_DESC,
EMA_COMP_DESC,
EMA_EXEC_HEAD,
EMA_EXEC_HEAD_DESC,
EMA_GROUP,
EMA_GROUP_DESC,
EMA_PU,
EMA_PU_DESC,
EMA_POSITION_ID,
EMA_REPORTING_TO_PNO,
EMA_DEPT_HEAD_PNO,
EMA_PERS_EXEC_PNO,
EMA_REF_PNO,
EMA_FATHERS_NAME,
EMA_SPOUSE_NAME,
EMA_TRG_ST_DT,
EMA_TRG_EN_DT,
EMA_FPF_ACNO,
EMA_EFBS_IND,
EMA_BANK_ACNO,
EMA_BANK_CD,
EMA_PANNO,
EMA_PHONE_NO,
EMA_EMAIL_ID,
EMA_RELIGION,
EMA_NATIVE_STATE,
EMA_NATIONALITY,
EMA_CHK_HOUSE,
EMA_WRK_CHK_HOUSE,
EMA_PYRL_STOP_DATE,
EMA_ENTRY_DATE,
EMA_ENTRY_USER,
EMA_LAST_UPDATE_DATE,
EMA_LAST_UPDATE_USER
)
VALUES( CT.base.eme_perno ,
CT.CAR.CAR_COMP_CD,
CT.PER.EPR_SALUTATION,
CT.PER.EPR_FST_NAME,
CT.PER.EPR_MID_NAME,
CT.PER.EPR_LST_NAME,
CT.HRPS.POP_NAME_FETCH(BASE.EME_PERNO),
CT.BASE.EME_DOB,
CT.per.epr_gender,
NULL,
CT.PER.EPR_DIFF_ABLE,
CT.PER.EPR_DIFF_ABLE_DESC,
CT.BASE.EME_BLOOD_GRP,
CT.PER.EPR_AFF_ACT,
CT.DOJ.idt_dt,
CT.BASE.EME_CADRE,
CT.C3.IRC_DESC,
CT.car.car_emp_class,
CT.CAR.CAR_EMP_TYP,
CT.BASE.EME_EMPL_STATUS,
CT.BASE.EME_SOURCE_EMPLMNT,
CT.CAR.CAR_PYRL,
CT.PYRL.IPY_PYRL_DESC,
CT.car.car_dept_cd ,
CT.org_dept.oro_desc ,
CT.car.car_sec_cd ,
CT.org_sec.oro_desc ,
CT.car.car_desgn_cd ,
CT.DES.IDS_DESGN_DESC ,
CT.CAR.CAR_PER_GRADE_CD,
CT.CAR.CAR_SUB_GRADE_CD,
NULL,
NULL,
NULL,
NULL,
CT.CAR.CAR_PYRL_AREA,
CT.C1.IRC_DESC,
CT.CAR.CAR_COST_CENT_CD,
CT.C2.IRC_DESC,
NULL,
NULL,
CT.DOS.IDT_DT,
CT.CAR_SEP.CAR_ACTION_RSN,
CT.C4.IRC_DESC,
CT.PDT.IDT_DT,
CT.EDR.IDT_DT,
NULL,
CT.PROM.IDT_DT,
CT.INC.IDT_DT,
NULL,
NULL,
CT.C5.IRC_DESC,
CT.car.car_exec_cd,
CT.org_exe.oro_desc ,
CT.car.car_grp_cd ,
CT.org_grp.oro_desc ,
CT.car.car_pu_cd ,
CT.org_pu.idp_pu_desc ,
CT.car.car_position_id,
NULL,
NULL,
NULL,
NULL,
CT.F_NAME,
CT.S_NAME,
CT.TRGSD.IDT_DT,
CT.TRGED.IDT_DT,
NULL,
NULL,
NULL,
NULL,
CT.PAN.EID_ID,
CT.PHONE.EMC_CONTACT,
CT.EMAIL.EMC_CONTACT,
CT.PER.EPR_RELIGION,
CT.PER.EPR_NATIVE_STATE,
CT.PER.EPR_NATIONALITY,
NULL,
NULL,
NULL,
SYSDATE,
'157068',
SYSDATE,
'157068');
END LOOP;
END Employeeall;
you have to make sure each column has a different alias when its in a stored proc.
ie i see duplicates:
DOJ.idt_dt,
DOS.IDT_DT,
PDT.IDT_DT,
EDR.IDT_DT,
PROM.IDT_DT,
INC.IDT_DT,
TRGSD.IDT_DT,
TRGED.IDT_DT,
just add a unique alias for each one like you've done for some of the other columns.

T-SQL Nested Subquery

I want to place this working code within a SQL Statement, OR do I need to perform a UDF.
The result set is a one line concatenation, and I want it to be place in every one of the overall result set lines.
----
MAIN QUERY
SELECT
H.CONNECTION_ID,
H.SEQUENTIAL_NO,
H.INVOICE_NUMBER,
H.INVOICE_DATE,
H.LAST_INVOICE_NUMBER,
H.LAST_INVOICE_DATE,
CAST(CASE
WHEN H.COLLECT_DEPOSIT = 1 THEN '-'
ELSE CAST(H.PAYMENT_DUE_DATE AS NVARCHAR(20))
END AS SMALLDATETIME) AS PAYMENT_DUE,
H.JOB_NUMBER,
H.CUST_JOB_NUMBER,
HDR.SALES_PERSON,
H.INSIDE_SALES_PERSON,
H.IS_LAST_INVOICE,
CASE
WHEN H.COLLECT_DEPOSIT = 1 THEN 'CASH'
ELSE H.PAYMENT_TERMS_DESCRIPTION
END AS PAYMENT_TERMS,
H.PRINTED,
H.NOTES,
CUR.ID,
CUR.CODE,
CASE CUR.CODE
WHEN 'USD' THEN '001-106624-211'
WHEN 'EUR' THEN '001-106624-101'
WHEN 'GBP' THEN '001-106624-100'
ELSE '001-106624-001'
END AS BANK_ACCT,
CUR.EXCHANGE_RATE,
H.BILL_CONTACT,
H.CUST_ACCOUNT,
H.CUST_NAME,
H.CUST_ADDR1,
H.CUST_ADDR2,
H.CUST_CITY,
H.CUST_STATE,
H.CUST_ZIP,
H.CONTACT_PHONE_NUMBER,
H.CONTACT_PHONE_NUMBER2,
H.ORDERED_BY_CONTACT,
H.SHIP_TO_NAME,
H.SHIP_TO_ADDR1,
H.SHIP_TO_ADDR2,
H.SHIP_TO_CITY,
H.SHIP_TO_STATE,
H.SHIP_TO_ZIP,
H.SITE_PHONE_NUMBER,
H.SITE_PHONE_NUMBER2,
H.OFFICE_NAME,
H.OFFICE_ADDR1,
H.OFFICE_ADDR2,
H.OFFICE_CITY,
H.OFFICE_STATE,
H.OFFICE_ZIP,
H.OFFICE_PHONE_NUMBER,
H.OFFICE_FAX_NUMBER,
H.DELIVERY_TICKET_NUMBER,
H.PO_NUMBER,
H.DUMMY_INVOICE_TEXT,
(SELECT MESSAGE FROM REPORT_MESSAGES WHERE CODE = 'INVOICE') ADVERT_MESSAGE,
(SELECT MAX(DISCOUNT_PERCENTAGE) FROM PRTINVITEM I2 WHERE I2.CONNECTION_ID = H.CONNECTION_ID AND I2.INVOICE_NUMBER = H.INVOICE_NUMBER) AS MAX_DISCOUNT,
I.ITEM,
I.DESCRIPTION,
I.QUANTITY,
I.UNIT_OF_MEASURE,
I.MINIMUM_CHARGE,
I.WEEKLY_CHARGE,
I.MONTHLY_CHARGE,
I.START_OF_BILLING_PERIOD,
I.END_OF_BILLING_PERIOD,
I.DAYS_USED,
I.WEEKS_USED,
I.DISCOUNT_PERCENTAGE,
I.TAX_CODE_FOR_ITEM,
I.INVENTORY_TYPE,
I.BILLING_LOGIC_TYPE,
I.ACTUAL_WEEKLY_CHARGE_USED,
I.DAYS_IN_ACTUAL_WEEKLY_CHARGE,
II.CHARGEABLE_DAYS,
II.CHARGEABLE_WEEKS,
II.CHARGEABLE_MONTHS,
II.FREE_DAYS_THIS_INVOICE,
CNV.TOTAL_NET_VALUE,
CNV.TOTAL_TAX_VALUE,
CNV.TOTAL_GROSS_VALUE,
CNV.TOTAL_GROSS_VALUE_NS,
CNV.NET_LINE_VALUE,
CMP.EMAIL_ADDRESS
FROM (PRTINVHDR H INNER JOIN PRTINVITEM I ON H.CONNECTION_ID = I.CONNECTION_ID AND H.INVOICE_NUMBER = I.INVOICE_NUMBER)
INNER JOIN INVOICEHDR HDR ON I.INVOICE_NUMBER = HDR.INVNO
INNER JOIN CUSTOMERS CST ON H.CUST_ACCOUNT = CST.CUSTNUM
INNER JOIN JOB JOB ON H.JOB_NUMBER = JOB.JOBNUM
INNER JOIN CURRENCY CUR ON HDR.CURRENCY_ID = CUR.ID
INNER JOIN VWCURRENCYCONVERSION CNV ON I.CONNECTION_ID = CNV.CONNECTION_ID AND I.INVC_UCOUNTER = CNV.INVC_UCOUNTER
INNER JOIN COMPANY CMP ON H.OFFICE_CODE = CMP.OFFICE
INNER JOIN INVOICEITEM II ON I.INVOICE_NUMBER = II.INVNO AND I.INVC_UCOUNTER = II.INVC_UCOUNTER
ORDER BY
H.SEQUENTIAL_NO,
I.PRINT_SEQUENCE
ASC
----
COALESCE QUERY
DECLARE
#DTICKET NVARCHAR(20),
#PUMPCATEGORYNAME NVARCHAR(3999)
SET #DTICKET = ''
SET #PUMPCATEGORYNAME = NULL
(SELECT
#DTICKET = DTICKET,
#PUMPCATEGORYNAME = COALESCE(#PUMPCATEGORYNAME + ', ', '' ) + PUMPCATEGORYNAME
FROM (SELECT
BHDR.DTICKET,
SCD.PUMPCATEGORYNAME
FROM PRTTICKHDR PHDR
INNER JOIN BIDHDR BHDR ON PHDR.DELIV_TICKET_NUMBER = BHDR.DTICKET
INNER JOIN PRTTICKITEM PITM ON PHDR.CONNECTION_ID = PITM.CONNECTION_ID AND PHDR.DELIV_TICKET_NUMBER = PITM.DELIV_TICKET_NUMBER
LEFT JOIN SUBCATEGORYDESCRIPTION SCD ON PITM.ITEM = SCD.PUMPCATEGORY
WHERE SCD.PUMPCATEGORYNAME IS NOT NULL)
SUBCATEGORYDESCRIPTION)
SELECT #DTICKET, #PUMPCATEGORYNAME
Not really sure what you are asking for but you can doing something along the lines of
Select col1 + ', ' + col2 + ', ' + col3 etc....