PostgreSQL - I have a Syntax error in my SUBSTRING query - sql

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;

Related

Oracle group by Alias

Orale is returning an error in the group by using alias column.
SELECT
CONCAT( CONCAT( a.air_location, ' ,' ), a.air_code ) AS "Departs From",
CONCAT( CONCAT( ad.air_location, ' ,' ), ad.air_code ) AS "Arrives At",
MIN(f.fl_fare) AS "Minimun Fare"
FROM flight f
INNER JOIN airport a ON a.air_code = f.fl_dest
INNER JOIN airport ad ON ad.air_code = f.fl_orig
GROUP BY "Departs From", "Arrives At";
It is returning:
ORA-00904: "Arrives At": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 10 Column: 26
You can't use a column alias in the GROUP BY clause in Oracle - and positional parameters are not supported either. You need to repeat the expression, or use a subquery or cte. So:
select
ao.air_location || ' ,' || ao.air_code as departs_from,
ad.air_location || ' ,' || ad.air_code as arrives_at,
min(f.fl_fare) as minimun_fare
from flight f
inner join airport ad on ad.air_code = f.fl_dest
inner join airport ao on ao.air_code = f.fl_orig
group by
ao.air_location || ' ,' || ao.air_code
ad.air_location || ' ,' || ad.air_code
Other changes to your query:
changed table aliases to names that seem more meaningful to me (and I think you had the origin and destination inverted in the result)
used standard ANSI string concatenation operator || instead of CONCAT(), which Oracle supports
used column aliases that do not require quoting (without embedded spaces), because it makes the query easier to write - you can revert that change if you really want otherwise.
As a final thought: here is an alternative that uses a lateral join to generate the aliases:
select x.departs_from, x.arrives_at, min(f.fl_fare) as minimun_fare
from flight f
inner join airport ad on ad.air_code = f.fl_dest
inner join airport ao on ao.air_code = f.fl_orig
cross apply (
select
ao.air_location || ' ,' || ao.air_code as departs_from,
ad.air_location || ' ,' || ad.air_code as arrives_at
from dual
) x
group by x.departs_from, x.arrives_at
Wrap your CONCAT query up in a derived table, GROUP BY its result:
SELECT "Departs From", "Arrives At", MIN(fl_fare) as minimun_fare
FROM
(
SELECT
CONCAT( CONCAT( a.air_location, ' ,' ), a.air_code ) AS "Departs From",
CONCAT( CONCAT( ad.air_location, ' ,' ), ad.air_code ) AS "Arrives At",
f.fl_fare
FROM flight f
INNER JOIN airport a ON a.air_code = f.fl_dest
INNER JOIN airport ad ON ad.air_code = f.fl_orig
) dt
GROUP BY "Departs From", "Arrives At";
(The GROUP BY is evaluated before the SELECT list. So the column aliases can't be used in the GROUP BY since they don't yet exist.)

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'

SQL query columns spreading

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

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;

Parsing error when creating view

I have a view which takes multiple rows and Comma Separated Values (CSV) from table columns with data.
It was working well and I got the results that I needed. Now the view has some parsing error like:
Error in WHERE clause near '('. Unable to parse query text.
Can someone help? Here is my code:
SELECT dbo.table1.title,
Stuff((SELECT ', ' + CONVERT(NVARCHAR(4000), dbo.table3.uid) AS [text()]
FROM dbo.table2
INNER JOIN dbo.table3
ON dbo.table3.uid = dbo.table2.FK_Group
WHERE dbo.table3.uid = dbo.table2.FK_Group
AND dbo.table3.company = dbo.main.company
AND dbo.table2.FK_Version = dbo.table1.fk_Version
AND dbo.main_version.uid = dbo.table1.fk_Version
FOR XML PATH('')), 1, 1, '') AS groupName
FROM dbo.main_version
INNER JOIN dbo.[main]
ON dbo.version.fk_main = dbo.[main].uid
INNER JOIN dbo.main_schema
ON dbo.[main].fk_SCHEMA = dbo.main_schema.uid
INNER JOIN dbo.table1
ON dbo.version.uid = dbo.table1.fk_Version
WHERE ( dbo.main_version.active = 1 )