SQL query columns spreading - sql

I have the following SQL query and as we see in the screenshot, there are two repeating row with the same constr_id(2015) value but with different assigned_insurance_packages.
Query:
select
ac.constr_id,
AIP.NAME as ASSIGNED_INSURANCE_PACKAGES,
ac.code,ac.constr_name,
ac.offer_name,
ac.repay_freq,
ac.min_amt,
ac.max_amt,
ac.min_downpay_percent,
ac.max_downpay_percent,
ac.downpay_amount,
ac.min_term,
(select
listagg(AF.NAME, '; ') within group(order by ACF.CONSTR_ID)
from
CREDILOGIC.ACQ_CONSTR_FEE acf, CREDILOGIC.ACQ_FEE af
where
ACF.CONSTR_ID = AC.CONSTR_ID and AF.FEE_ID = ACF.FEE_ID) as CONSTRUCTION_FEES,
INS_COMPANY.SHORT_NAME,
(select listagg(x.DURATION_MIN || '-' || TO_CHAR(x.RATE_SHIFT, '90.99') || ',' || x.DURATION_MAX || '-' || TO_CHAR(x.RATE_SHIFT, '90.99'), '; ') within group (order by X.CONSTRUCTION_ID)
from credilogic.ACQ_CONSTRUCTION_BASERATE x
where X.CONSTRUCTION_ID = AC.CONSTR_ID) as RATE
from
CREDILOGIC.ACQ_CONSTRUCTION ac
left join
CREDILOGIC.ACQ_CONSTR_INSPACKAGE aci on ACI.CONSTR_ID = AC.CONSTR_ID
and ACI.DELETED_TSTAMP is null
left join
CREDILOGIC.ACQ_INSURANCE_PACKAGE aip on AIP.INSURANCE_PACKAGE_ID = ACI.INSURANCE_PACKAGE_ID
left join
credilogic.ath_party ins_company ON INS_COMPANY.PARTY_ID = aip.PARTY_ID
left join
credilogic.ACQ_CONSTRUCTION_DUEDATE acd on ac.constr_id = acd.constr_id
left join
credilogic.ACQ_APPLICATION acp on ac.constr_id = acp.construction_id
I am just going to create two additional columns like 0.3% insurance pack and 0.5% insurance pack to put ASSIGNED_INSURANCE_PACKAGES values into different columns like in the picture below

Related

Union all not displaying all queries

I'm making a query with multiple UNION ALL statements and it's not showing all the results it should show. The SELECT statements all have the same amount of columns, the same aliases and same data types (I've already tried to use CAST, just to be sure). There's only a single SELECT statement that's using a CASE in two different columns and that is all that's different from the others. I've tried to derive that to a sub-select, but to no avail. If I run them by themselves they work fine, matter of fact. If I remove this SELECT statement that has those two CASE's and leave all the rest, it runs fine. There's a lot of unions, so I'll just post the problematic SELECT and one of the other UNION's, since they're all pretty much the same.
All I'm doing in these is pulling values from different events, like employee's taxes, employee's salaries and different adjustments, just to give a clearer view of what I'm doing. I can't provide the real data due to legal reasons. What 's making it not run along the other SELECTs?
Here's the one that's giving me issues
SELECT
CASE
WHEN TCL.CONTADEB IS NOT NULL THEN TCL.CONTADEB
WHEN TCL.CONTACRED IS NOT NULL THEN TCL.CONTACRED
END AS CONTA_CTB
, CASE
WHEN TCL.CONTADEB IS NOT NULL THEN CR.VALOREVENTO
END AS DEBITO
, CASE
WHEN TCL.CONTACRED IS NOT NULL THEN CR.VALOREVENTO
END AS CREDITO
, FP.NOMEFUNC AS FUNCIONARIO
, EV.DESCREVENTO || ' - ' || EXTRACT(MONTH FROM PC.COMPET) || '/' || EXTRACT(YEAR FROM PC.COMPET) || ' ' || FP.NOMEFUNC AS OBSERVACAO
, LCTB.DESCRLOCALCTB AS LOCAL_CONTABIL
, CC.DESCRCENTROCUSTO AS CENTRO_CUSTO
, EXTRACT(YEAR FROM PC.DATAFINALFOLHA) || lpad(EXTRACT(MONTH FROM PC.DATAFINALFOLHA), '2', '0') || EXTRACT(DAY FROM PC.DATAFINALFOLHA) AS VENCIMENTO
FROM CALCULORATEIO CR
LEFT JOIN CENTROCUSTO CC
ON CC.CODIGOEMPRESA = CR.CODIGOEMPRESA AND CC.CODIGOCENTROCUSTO = CR.CODIGOCENTROCUSTO
LEFT JOIN EVENTO EV
ON EV.CODIGOEVENTO = CR.CODIGOEVENTO
LEFT JOIN PERIODOCALCULO PC
ON PC.CODIGOEMPRESA = CR.CODIGOEMPRESA AND PC.CODIGOPERCALCULO = CR.CODIGOPERCALCULO
LEFT JOIN FUNCCONTRATO FC
ON FC.CODIGOEMPRESA = CR.CODIGOEMPRESA AND FC.CODIGOFUNCCONTR = CR.CODIGOFUNCCONTR
LEFT JOIN FUNCPESSOA FP
ON FP.CODIGOFUNCPESSOA = FC.CODIGOFUNCPESSOA
LEFT JOIN FUNCLOCALCTB FLC
ON FLC.CODIGOEMPRESA = CR.CODIGOEMPRESA AND FLC.CODIGOFUNCCONTR = CR.CODIGOFUNCCONTR
AND FLC.DATAOPCAO = (SELECT MAX(FLC1.DATAOPCAO) FROM FUNCLOCALCTB FLC1 WHERE FLC1.CODIGOEMPRESA = FLC.CODIGOEMPRESA AND
FLC1.CODIGOFUNCCONTR = FLC.CODIGOFUNCCONTR AND FLC1.DATAOPCAO <= PC.DATAINICIALFOLHA)
LEFT JOIN LOCALCONTABIL LCTB
ON LCTB.CODIGOEMPRESA = FLC.CODIGOEMPRESA AND LCTB.CODIGOLOCALCTB = FLC.CODIGOLOCALCTB
LEFT JOIN EVENTOTABELACONTABIL EVT
ON EVT.CODIGOEVENTO = CR.CODIGOEVENTO AND EVT.CODIGOGRUPOTABELACONTABIL = 0
LEFT JOIN TABCTBLOCALCTB TCL
ON TCL.CODIGOEMPRESA = FLC.CODIGOEMPRESA AND TCL.CODIGOTABCTB = EVT.CODIGOTABCTB AND TCL.CODIGOLOCALCTB = FLC.CODIGOLOCALCTB
WHERE
CR.CODIGOEMPRESA = :CodigoEmpresa.n AND
CR.CODIGOPERCALCULO = :CodigoPerCalculo.n
And here's one of the others:
SELECT
CASE
WHEN PFR.DIFERPGTO > 0 THEN '1608'
ELSE '1612'
END AS CONTA_CTB
, PFR.DIFERPGTO AS DEBITO
, PFR.DIFERPGTO AS CREDITO
, FP.NOMEFUNC AS FUNCIONARIO
, 'Diferença Pgto 13' || ' - ' || EXTRACT(MONTH FROM PC.COMPET) || '/' || EXTRACT(YEAR FROM PC.COMPET) || ' ' || FP.NOMEFUNC AS OBSERVACAO
, LCTB.DESCRLOCALCTB AS LOCAL_CONTABIL
, CC.DESCRCENTROCUSTO AS CENTRO_CUSTO
, EXTRACT(YEAR FROM PC.DATAFINALFOLHA) || lpad(EXTRACT(MONTH FROM PC.DATAFINALFOLHA), '2', '0') || EXTRACT(DAY FROM PC.DATAFINALFOLHA) AS VENCIMENTO
FROM PROVISAO13RAT PFR
LEFT JOIN FUNCCONTRATO FC
ON FC.CODIGOEMPRESA = PFR.CODIGOEMPRESA AND FC.CODIGOFUNCCONTR = PFR.CODIGOFUNCCONTR
LEFT JOIN FUNCPESSOA FP
ON FP.CODIGOFUNCPESSOA = FC.CODIGOFUNCPESSOA
LEFT JOIN PERIODOCALCULO PC
ON PC.CODIGOEMPRESA = PFR.CODIGOEMPRESA AND PC.COMPET = PFR.COMPET
LEFT JOIN CENTROCUSTO CC
ON CC.CODIGOEMPRESA = PFR.CODIGOEMPRESA AND CC.CODIGOCENTROCUSTO = PFR.CODIGOCENTROCUSTO
LEFT JOIN FUNCLOCALCTB FLC
ON FLC.CODIGOEMPRESA = PFR.CODIGOEMPRESA AND FLC.CODIGOFUNCCONTR = PFR.CODIGOFUNCCONTR
AND FLC.DATAOPCAO = (SELECT MAX(FLC1.DATAOPCAO) FROM FUNCLOCALCTB FLC1 WHERE FLC1.CODIGOEMPRESA = FLC.CODIGOEMPRESA AND
FLC1.CODIGOFUNCCONTR = FLC.CODIGOFUNCCONTR AND FLC1.DATAOPCAO <= PC.DATAINICIALFOLHA)
LEFT JOIN LOCALCONTABIL LCTB
ON LCTB.CODIGOEMPRESA = FLC.CODIGOEMPRESA AND LCTB.CODIGOLOCALCTB = FLC.CODIGOLOCALCTB
WHERE
PFR.CODIGOEMPRESA = :CODIGOEMPRESA.n AND
PFR.COMPET = :DATAINICIAL.d AND
PFR.DIFERPGTO <> 0
The main tables I'm using in these all have the same structures, with exception of that one (the first code I've pasted here), could this be part of the problem?
The WHERE clauses all filter through company code and billing date, that's the same for all of them. If any information is lacking, I'll provide them the best way I can.
Ok, so it turns out it was because of the table's structures that were different, doesn't really make much sense for me, but it's working now

Unable to retrieve Data Oracle Fusion BI Publisher

1.Main Code:
SELECT
iodv.organization_id,
iodv.organization_name,
mil.SUBINVENTORY_CODE subinventory_name,
mil.inventory_location_id locator_id,
mil.segment11 || '.' || mil.segment12 || '.' || mil.segment13 locator_name
FROM
inv_item_locations mil,
inv_organization_definitions_v iodv
WHERE
1 = 1
and iodv.organization_id = mil.organization_id
and iodv.Organization_Code = ':Organizations'
and mil.SUBINVENTORY_CODE = ':p_Subinventories'
Parameter p_SubInventories:
select
distinct
iil.Subinventory_code
from
inv_item_locations iil,
INV_ORG_PARAMETERS iop
where
1=1
and iil.ORGANIZATION_ID=iop.ORGANIZATION_ID
and iop.ORGANIZATION_CODE= :Organizations
Parameter for Organizations:
select distinct organization_code from inv_org_parameters
Unable to retrieve data with the above query in Data Model # Oracle BI Publisher......
LEFT JOIN here
SELECT
iodv.organization_id,
iodv.organization_name,
mil.SUBINVENTORY_CODE subinventory_name,
mil.inventory_location_id locator_id,
mil.segment11 || '.' || mil.segment12 || '.' || mil.segment13 locator_name
FROM
inv_item_locations mil
LEFT JOIN
inv_organization_definitions_v iodv
ON
iodv.organization_id = mil.organization_id
WHERE 1 = 1
AND iodv.Organization_Code = ':Organizations'
AND mil.SUBINVENTORY_CODE = ':p_Subinventories'
I would have thought the single apostrophe was problem, but maybe removing the inner join also helps.
It's worth noting the LEFT JOIN could be applied using using PL SQL Outer Join Operator (+). See adjustment to Ops main code below
SELECT
iodv.organization_id,
iodv.organization_name,
mil.SUBINVENTORY_CODE subinventory_name,
mil.inventory_location_id locator_id,
mil.segment11 || '.' || mil.segment12 || '.' || mil.segment13 locator_name
FROM
inv_item_locations mil,
inv_organization_definitions_v iodv
WHERE
1 = 1
and iodv.organization_id (+) = mil.organization_id ----
and iodv.Organization_Code = ':Organizations'
and mil.SUBINVENTORY_CODE = ':p_Subinventories'

Unsure why ORA-00918 column ambiguously defined is appearing

I'm unsure as to why I'm getting the ORA-00918 error message appearing when I type in the following code.
I can't see which column is ambiguously defined.
What I want to do is create a table that pulls in the b.site_code value based on the work_header_no, work_version_no and site_numbers matching in queries A & B matching.
Code is below
SELECT
a.statement.statement_date,
a.sw_header.organise_code,
a.organisation.organise_name,
a.PermitRef,
a.actual_inspection.logged_time,
a.insp_category.insp_category_name,
a.actual_inspection.insp_number,
a.actual_inspection.site_number,
a.inspection_outcome.insp_outcome_name,
a.insp_category.insp_charge,
a.actual_inspection.insp_notes,
a.actual_inspection.work_header_no,
a.actual_inspection.insp_time,
b.site_code
FROM
(select
statement.statement_date,
sw_header.organise_code,
organisation.organise_name,
CAST(
organisation.external_ref_2 ||''||
sw_header.works_ref||'.'||
sw_notice_header.app_seq_no||'.'||
sw_notice_header.ext_version_no
as VARCHAR (40)) as PermitRef,
actual_inspection.logged_time,
insp_category.insp_category_name,
actual_inspection.insp_number,
actual_inspection.site_number,
inspection_outcome.insp_outcome_name,
insp_category.insp_charge,
actual_inspection.insp_notes,
actual_inspection.work_header_no,
actual_inspection.insp_time,
sw_notice_header.work_header_no,
sw_notice_header.work_version_no,
actual_inspection.site_number
from
actual_inspection
inner join sw_header on
actual_inspection.work_header_no = sw_header.work_header_no
inner join sw_notice_header on
sw_header.work_header_no = sw_notice_header.work_header_no
and sw_header.work_version_no = sw_notice_header.work_version_no
inner join insp_category on
actual_inspection.insp_category_code = insp_category.insp_category_code
inner join inspection_outcome on
actual_inspection.insp_outcome_code = inspection_outcome.insp_outcome_code
inner join organisation on
sw_header.organise_code = organisation.organise_code
inner join statement on
organisation.organise_code = statement.organise_code
and organisation.statement_number = statement.statement_no
where
actual_inspection.notice_type_code = '2600' and
actual_inspection.insp_outcome_code != 'O40'
order by
actual_inspection.logged_time)
a
JOIN
(
select
sns.work_header_no,
sns.work_version_no,
sns.site_number,
sns.site_code
from
sw_notice_site sns
)
b
ON a.work_header_no = b.work_header_no and
a.work_version_no = b.work_version_no and
a.site_number = b.site_number
You have duplicate actual_inspection.site_number and work_header_no remove the duplicate rows
actual_inspection.site_number,
inspection_outcome.insp_outcome_name,
insp_category.insp_charge,
actual_inspection.insp_notes,
actual_inspection.work_header_no,
actual_inspection.insp_time,
sw_notice_header.work_header_no,
sw_notice_header.work_version_no,
actual_inspection.site_number
No need to use a.statement.statement_date. You can use a.statement_date.
Likewise change all other columns for a..
Your whole query should look like this:
SELECT -- removed table names from all the columns
A.STATEMENT_DATE,
A.ORGANISE_CODE,
A.ORGANISE_NAME,
A.PERMITREF,
A.LOGGED_TIME,
A.INSP_CATEGORY_NAME,
A.INSP_NUMBER,
A.SITE_NUMBER,
A.INSP_OUTCOME_NAME,
A.INSP_CHARGE,
A.INSP_NOTES,
A.WORK_HEADER_NO,
A.INSP_TIME,
B.SITE_CODE
FROM
(
SELECT
STATEMENT.STATEMENT_DATE,
SW_HEADER.ORGANISE_CODE,
ORGANISATION.ORGANISE_NAME,
CAST(ORGANISATION.EXTERNAL_REF_2
|| ''
|| SW_HEADER.WORKS_REF
|| '.'
|| SW_NOTICE_HEADER.APP_SEQ_NO
|| '.'
|| SW_NOTICE_HEADER.EXT_VERSION_NO AS VARCHAR(40)) AS PERMITREF,
ACTUAL_INSPECTION.LOGGED_TIME,
INSP_CATEGORY.INSP_CATEGORY_NAME,
ACTUAL_INSPECTION.INSP_NUMBER,
--ACTUAL_INSPECTION.SITE_NUMBER, -- commented this as it is there in statement twice
INSPECTION_OUTCOME.INSP_OUTCOME_NAME,
INSP_CATEGORY.INSP_CHARGE,
ACTUAL_INSPECTION.INSP_NOTES,
ACTUAL_INSPECTION.WORK_HEADER_NO,
ACTUAL_INSPECTION.INSP_TIME,
--SW_NOTICE_HEADER.WORK_HEADER_NO, -- commented this as it is there in statement twice
SW_NOTICE_HEADER.WORK_VERSION_NO,
ACTUAL_INSPECTION.SITE_NUMBER
FROM
ACTUAL_INSPECTION
INNER JOIN SW_HEADER ON ACTUAL_INSPECTION.WORK_HEADER_NO = SW_HEADER.WORK_HEADER_NO
INNER JOIN SW_NOTICE_HEADER ON SW_HEADER.WORK_HEADER_NO = SW_NOTICE_HEADER.WORK_HEADER_NO
AND SW_HEADER.WORK_VERSION_NO = SW_NOTICE_HEADER.WORK_VERSION_NO
INNER JOIN INSP_CATEGORY ON ACTUAL_INSPECTION.INSP_CATEGORY_CODE = INSP_CATEGORY.INSP_CATEGORY_CODE
INNER JOIN INSPECTION_OUTCOME ON ACTUAL_INSPECTION.INSP_OUTCOME_CODE = INSPECTION_OUTCOME.INSP_OUTCOME_CODE
INNER JOIN ORGANISATION ON SW_HEADER.ORGANISE_CODE = ORGANISATION.ORGANISE_CODE
INNER JOIN STATEMENT ON ORGANISATION.ORGANISE_CODE = STATEMENT.ORGANISE_CODE
AND ORGANISATION.STATEMENT_NUMBER = STATEMENT.STATEMENT_NO
WHERE
ACTUAL_INSPECTION.NOTICE_TYPE_CODE = '2600'
AND ACTUAL_INSPECTION.INSP_OUTCOME_CODE != 'O40'
ORDER BY
ACTUAL_INSPECTION.LOGGED_TIME
) A
JOIN (
SELECT
SNS.WORK_HEADER_NO,
SNS.WORK_VERSION_NO,
SNS.SITE_NUMBER,
SNS.SITE_CODE
FROM
SW_NOTICE_SITE SNS
) B ON A.WORK_HEADER_NO = B.WORK_HEADER_NO
AND A.WORK_VERSION_NO = B.WORK_VERSION_NO
AND A.SITE_NUMBER = B.SITE_NUMBER;
Cheers!!

How to count employees who are involved in a proct and list there names in one cell?

I try to write a query which shows me the number of employees who are involved in a project and also list there names in just one cell.
select p.projectname, count(main.projectnr) "Involved employees" from projects_karl p
join mainpro_karl main
on main.projectnr = p.projectnr
join employees_karl m
on m.employeenr = main.employeenr
group by p.beschreibung
;
This query counts the amount of employees who are working in a certain project.
But I also want to list the names of the involved employees.
select p.projectname, count(main.projectnr) "Anzahl beteiligter MAs", m.firstname || ' ' || m.surname "Name" from projects_karl p
join mainpro_karl main
on main.projectnr = p.projectnr
join Employees_karl m
on m.employeenr = main.employeenr
group by p.projectname, m.firstname || ' ' || m.surname
;
This query list involved employees but not in a cell but in a own row. And the count function does not works anymore. It just counts '1' in every row.
use listagg()
select p.projectname, count(main.projectnr) "Anzahl beteiligter MAs",
listagg(m.firstname || ' ' || m.surname, ',') within group (order by p.projectname) "Name"
from projects_karl p join mainpro_karl main on main.projectnr = p.projectnr
join Employees_karl m on m.employeenr = main.employeenr
group by p.projectname

issues combining two columns using concat

I am having issues with combining two columns into the one using mssql
table 1 format:
|| WJCPrefix || WJCNo ||
|| UK-R/SWJC/14/ || 1234 ||
|| UK-R/CUWJC/14/ || 2345 ||
|| UK-R/CUWJC/14/ || 3456 ||
|| UK-R/SWJC/14/ || 4567 ||
|| UK-R/CUWJC/14/ || 5678 ||
The desired out would be:
UK-R/CUWJC/14/3456
UK-R/CUWJC/14/5678
the sql statement i am using is:
SELECT tblWJCItem.AddedDescription, concat(tblWJC.WJCPrefix, tblWJC.WJCNo) AS OurRef
FROM tblWJC
INNER JOIN tblWJCItem ON tblWJC.WJCID = tblWJCItem.WJCID;
I've also used:
SELECT tblWJCItem.AddedDescription, tblWJC.WJCPrefix + '' + tblWJC.WJCNo AS OurRef
FROM tblWJC
INNER JOIN tblWJCItem ON tblWJC.WJCID = tblWJCItem.WJCID;
I can't seem to connect these two columns could anyone point out what I am doing wrong here?
Thanks
Your first query (Concat() function) should work if you are using SQL Server 2012 or later.
For other versions, you may need to Convert()/Cast() WJCNo to a string type
SELECT t2.AddedDescription,
t1.WJCPrefix + CONVERT(Varchar(10),t1.WJCNo) AS OurRef
FROM tblWJC t1
INNER JOIN tblWJCItem t2 ON t1.WJCID = t2.WJCID;
Your first query should be fine. But you might try:
SELECT tblWJCItem.AddedDescription, tblWJC.Prefix + cast(tblWJC.WJCNo as varchar(255)) AS OurRef
FROM tblWJC INNER JOIN
tblWJCItem
ON tblWJC.WJCID = tblWJCItem.WJCID;
You might get an error in the second version if WJCno is numeric rather than a string.
I think WJCNo is numeric or int field so Convert this field to Varchar first then concat:-
SELECT tblWJCItem.AddedDescription,
tblWJC.WJCPrefix + '' + CONVERT(Varchar(10),tblWJC.WJCNo) AS OurRef
FROM tblWJC
INNER JOIN tblWJCItem ON tblWJC.WJCID = tblWJCItem.WJCID;