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;
Related
for some reasons I don't have, a table, although referenced in the query, is not referenced according to the error.
The query :
WITH RECURSIVE temptable AS (
SELECT part_id, tracability_component1, tracability_component2, tracability_component3, tracking_id
FROM kpitr.subop_result SR
LEFT JOIN kpitr.part_produce PP ON PP.supplier_id || PP.product_id LIKE SR.tracability_component1 OR PP.supplier_id || PP.product_id LIKE SR.tracability_component2 OR PP.supplier_id || PP.product_id LIKE SR.tracability_component3
WHERE part_id LIKE '%0005001723A031T'
UNION ALL
SELECT SR2.part_id, SR2.tracability_component1, SR2.tracability_component2, SR2.tracability_component3, PP2.tracking_id
FROM kpitr.subop_result SR2, temptable
LEFT JOIN kpitr.part_produce PP2 ON PP2.supplier_id || PP2.product_id LIKE SR2.tracability_component1 OR PP2.supplier_id || PP2.product_id LIKE SR2.tracability_component2 OR PP2.supplier_id || PP2.product_id LIKE SR2.tracability_component3 OR SR2.part_id LIKE temptable.tracking_id
)
SELECT * FROM temptable
The error :
ERROR: invalid reference to FROM-clause entry for table "sr2"
LINE 9: ...uce PP2 ON PP2.supplier_id || PP2.product_id LIKE SR2.tracab...
^
HINT: There is an entry for table "sr2", but it cannot be referenced from this part of the query.
SQL state: 42P01
Character: 678
I believe I place in the wrong order something, or I missed sometinh, but after some tests, it didn't change anything.
Could you help me ?
Got the solution, after some tries :
WITH RECURSIVE temptable AS (
SELECT SR1.part_id, SR1.tracability_component1, SR1.tracability_component2, SR1.tracability_component3, PP1.tracking_id
FROM kpitr.subop_result SR1
LEFT JOIN kpitr.part_produce PP1 ON UPPER(PP1.reference) || UPPER(PP1.product_id) = UPPER(SR1.tracability_component1) OR UPPER(PP1.reference) || UPPER(PP1.product_id) = UPPER(SR1.tracability_component2) OR UPPER(PP1.reference) || UPPER(PP1.product_id) = UPPER(SR1.tracability_component3)
WHERE part_id LIKE '%0005001723A031T'
UNION ALL
SELECT sr.part_id, sr.tracability_component1, sr.tracability_component2, sr.tracability_component3, pp.tracking_id
FROM kpitr.subop_result sr
JOIN temptable ON UPPER(sr.part_id) = UPPER(temptable.tracability_component1) OR UPPER(sr.part_id) = UPPER(temptable.tracability_component2) OR UPPER(sr.part_id) = UPPER(temptable.tracability_component3) OR UPPER(sr.part_id) = UPPER(temptable.tracking_id)
LEFT JOIN kpitr.part_produce pp ON UPPER(pp.reference) || UPPER(pp.product_id) = UPPER(sr.tracability_component1) OR UPPER(pp.reference) || UPPER(pp.product_id) = UPPER(sr.tracability_component2) OR UPPER(pp.reference) || UPPER(pp.product_id) = UPPER(sr.tracability_component3)
)
SELECT * FROM temptable
I'm trying to make use of the SUBSTRING() function to extract a substring from vm.location_path, starting at the second character and ending at the position of the ']' character, minus two.
I want to extract the text between the square brackets ([]) in vm.location_path but I'm hitting a syntax error.
SELECT
'UPDATE vm SET dstore_moref = ''' || datastore_inv.moref || ''' WHERE id = ''' || vm.id || ''';'
FROM
vm
INNER JOIN vapp_vm ON vapp_vm.svm_id = vm.id
INNER JOIN vm_inv ON vm_inv.moref = vm.moref
INNER JOIN datastore_inv ON datastore_inv.vc_display_name =(
SUBSTRING(
vm.location_path,
2,
POSITION(']',
vm.location_path) - 2
)
)
WHERE
vm.dstore_moref IS NULL AND vm_inv.is_deleted IS FALSE
GROUP BY datastore_inv.moref, vm.id;
SQL Error [42601]: ERROR: syntax error at or near ","
Position: 370
Error position: line: 11 pos: 369
It's between the comma at the end of
POSITION(']',
and
vm.location_path) - 2
What am I not seeing?
This is for vCloud Director. I am trying to get a print out of all VMs that are NULL.
The syntax is POSITION(search_string in main_string) with IN Keyowrd instead of ,
SELECT
'UPDATE vm SET dstore_moref = ''' || datastore_inv.moref || ''' WHERE id = ''' || vm.id || ''';'
FROM
vm
INNER JOIN vapp_vm ON vapp_vm.svm_id = vm.id
INNER JOIN vm_inv ON vm_inv.moref = vm.moref
INNER JOIN datastore_inv ON datastore_inv.vc_display_name =(
SUBSTRING(
vm.location_path,
2,
POSITION(']' IN vm.location_path) - 2
)
)
WHERE
vm.dstore_moref IS NULL AND vm_inv.is_deleted IS FALSE
GROUP BY datastore_inv.moref, vm.id;
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'
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
Consider this table structure
people{id,firstName,lastName,preferredName}
list{id,person1_id,person2_id}
I want to get all data from list but with the names of the persons instead of IDs and with a twist: if preferredName is set, use that, otherwise use firstName+SPACE+lastName.
The query if I only needed one name would be this:
SELECT list.id,
CASE WHEN people.preferredName IS NULL OR people.preferredName="" THEN people.firstName||' '||people.lastName ELSE people.preferredName END AS preferredName
FROM list LEFT JOIN people ON list.person1_id=people.id;
Thank you.
COALESCE() returns its first non-null argument.
COALESCE(preferredName, firstName || ' ' || lastName) as person_name
I don't think you need left joins, but I could be wrong. (If you have a foreign key constraint such that person1_id and person2_id must exist in people, you don't need a left join.)
SELECT list.id,
COALESCE( p1.preferredName, p1.firstName || ' ' || p1.lastName) as person1_name,
COALESCE( p2.preferredName, p2.firstName || ' ' || p2.lastName) as person2_name,
FROM list
INNER JOIN people p1 ON list.person1_id= p1.id
INNER JOIN people p2 on list.person2_id = p2.id
Use table aliases to let you join the same table twice.
SELECT list.id,
CASE WHEN people1.preferredName IS NULL OR people1.preferredName = ""
THEN people1.firstName || ' ' || people1.lastName
ELSE people1.preferredName
END AS preferredName1,
CASE WHEN people2.preferredName IS NULL OR people2.preferredName = ""
THEN people2.firstName || ' ' || people2.lastName
ELSE people2.preferredName
END AS preferredName2,
FROM list
LEFT JOIN people people1 ON list.person1_id = people1.id
LEFT JOIN people people2 ON list.person2_id = people2.id