Why keeps showing this error: ORA-00936: missing expression - sql

CREATE OR REPLACE FORCE EDITIONABLE VIEW "VU_REPORT5" ("Selling Report") AS
SELECT a2.FIRST_NAME || a2.SUR_NAME ||', living in ' || a4.COUNTRY ||
', ' || a4.CITY || ' ' || a4.LINE_1 || a4.LINE_2 ||
a4.LINE_3 || a4.LINE_4 || ', bought an ' || a1.MAKE || ' ' ||
a1.MODEL || ' from employee ' || a3.FIRST_NAME || ' ' ||
a3.SUR_NAME || ' at ' || a1.SOLD_DATE || ', Making a profit of ' ||
to_char(a1.SOLD_PRICE - a1.PURCHASE_PRICE) ||' pounds.' AS "Selling Report",
to_char(SELECT sum(a5.SOLD_PRICE)-sum(a5.PURCHASE_PRICE)
FROM CAR a5
where (to_date(a5.SOLD_DATE,'mm-dd-yyyy') <= to_date(a1.SOLD_DATE,'mm-dd-yyyy'))
ORDER BY a5.SOLD_DATE
GROUP BY a5.SOLD_DATE) AS "OVERALL Report"
FROM CAR a1,
CUSTOMER a2,
staff a3,
ADDRESS a4
WHERE a1.BOUGHT_BY_CUSTOMER_NO = a2.CUSTOMER_NO and
a1.SOLD_BY_STAFF_NO = a3.STAFF_NO and
a4.ADDRESS_NO = a2.ADDRESS_NO
ORDER BY a1.SOLD_DATE

One issue is that the view is defined as returning a single column ("Selling Report"), but the query actually returns two columns ("Selling Report" and "OVERALL Report").
Another issue is that you can't put a sub-select into a function call; in this case
to_char(SELECT sum(a5.SOLD_PRICE)-sum(a5.PURCHASE_PRICE)
FROM CAR a5
where (to_date(a5.SOLD_DATE,'mm-dd-yyyy') <= to_date(a1.SOLD_DATE,'mm-dd-yyyy'))
ORDER BY a5.SOLD_DATE
GROUP BY a5.SOLD_DATE)
simply isn't valid. I'm not sure what you're trying to do with this sub-query so I can't really advise you on how to correct the problem.
Best of luck.

Related

Adding spaces to a piped concatenation

How do I change my select statement so that the following will return spaces between the concatenated columns?
The current query is similar to:
SELECT
address_line1 || address_line2 || address_line3 || address_line4 || city || state || county || province || country || zip as address
FROM
table
Many thanks
Just add space characters in between the fields:
SELECT
address_line1 || ' ' || address_line2 || ' ' || address_line3 || ' ' ||
address_line4 || ' ' || city || ' ' || state || ' ' || county || ' ' ||
province || ' ' || country || ' ' || zip AS address
FROM yourTable;

String Replace a joined SQL SELECT statement

I'm wondering if it is possibly to complete a string replace on a joined SQL statement. For example, my query is:
SELECT (SITE_NAME || ', ' || LOT_NUMBER || ' ' || UNIT_TYPE || ' ' || LEVEL_NUMBER
|| ' ' || UNIT_NUMBER || ' ' || ROAD_NUMBER_1 || ' ' || ROAD_NUMBER_2 || ' ' || ROAD_NAME
|| ' ' || ROAD_TYPE || ' ' || ROAD_SUFFIX || ', ' || SUBURB || ', ' || STATE) AS address
FROM ADDRESS_LOOKUP_TOOL
WHERE ADD_ID = :P1_ADD_ID;
This statement works perfectly.... providing every single address field is populated. If only some sections are populated (i.e. there is no site name, or road suffix), there are additional commas or spaces.
Here is an example of a good select:
House of Dom, Lot 1 Suite 4 4D 119 Fake St South, Domtopia, QLD
Here is an example of a flawed select:
, Lot 1 Suite 4 4D 119 Fake St , Domtopia, QLD
Is it possibly to do a string replace on the alias where I could say, for example replace(address, ' ,', ',') (Where "space comma" just becomes "comma"), or is there a better way I should be structuring my select to pick this up in one go?
An additional note: This is all being completed with in Oracle's Application Express (ApEx) if this makes a difference.
I am very new to SQL, so I apologise in advance if I ask any basic follow up questions!
Thank you!
Dominic
You don't need to do it on the alias. Just do it on the expression itself.
SELECT REPLACE(
(SITE_NAME || ', ' || LOT_NUMBER || ' ' || UNIT_TYPE || ' ' || LEVEL_NUMBER
|| ' ' || UNIT_NUMBER || ' ' || ROAD_NUMBER_1 || ' ' || ROAD_NUMBER_2 || ' ' || ROAD_NAME
|| ' ' || ROAD_TYPE || ' ' || ROAD_SUFFIX || ', ' || SUBURB || ', ' || STATE),
' ,', ',') AS address
FROM ADDRESS_LOOKUP_TOOL
WHERE ADD_ID = :P1_ADD_ID;

Query causing values to not be included

In the following I am looking up a value in a legacy database my company has.
SELECT DISTINCT DLR.CIRCUIT_DESIGN_ID AS "CID",
DLR.ECCKT AS "CIRCUIT",
CI.LOCATION_ID_2 AS "SITE ID",
CI.EXCHANGE_CARRIER_CIRCUIT_ID AS "ID",
CI.RATE_CODE as "Rate",
DLR.ACCESS_CUSTOMER_NAME AS "CUSTOMER SITE NAME",
ADR.HOUSE_NBR || ' ' || ADR.STREET_NM || ' ' || ADR.STREET_SUF || ' ' || ADR.CITY_NAME || ' ' || ADR.STATE_CODE || ' ' || ADR.ZIP_CODE AS "CUSTOMER ADDRESS"
FROM DESIGN_LAYOUT_REPORT DLR, CIRCUIT CI, MSAG_ADDR_LOC ADR
WHERE CI.CIRCUIT_DESIGN_ID = DLR.CIRCUIT_DESIGN_ID
AND CI.LOCATION_ID_2 = ADR.LOCATION_ID
AND CI.CIRCUIT_DESIGN_ID IN (
SELECT DISTINCT CIRCUIT_DESIGN_ID
FROM DLR_CIRCUIT_DESIGN_LINE
WHERE LOCATION LIKE '% <some value from other code> %'
)
My problem comes from the fact that ADR only has info for some of the values I search which is causing this query to not include all the values I need. DLR and CI always have values. How would I make this query return everything and just give me blank returns for the values that do not match an ADR entry?
You would just need an outer join:
SELECT DISTINCT DLR.CIRCUIT_DESIGN_ID AS "CID",
DLR.ECCKT AS "CIRCUIT",
CI.LOCATION_ID_2 AS "SITE ID",
CI.EXCHANGE_CARRIER_CIRCUIT_ID AS "ID",
CI.RATE_CODE as "Rate",
DLR.ACCESS_CUSTOMER_NAME AS "CUSTOMER SITE NAME",
ADR.HOUSE_NBR || ' ' || ADR.STREET_NM || ' ' || ADR.STREET_SUF || ' ' || ADR.CITY_NAME || ' ' || ADR.STATE_CODE || ' ' || ADR.ZIP_CODE AS "CUSTOMER ADDRESS"
FROM DESIGN_LAYOUT_REPORT DLR, CIRCUIT CI, MSAG_ADDR_LOC ADR
WHERE CI.CIRCUIT_DESIGN_ID = DLR.CIRCUIT_DESIGN_ID
AND CI.LOCATION_ID_2 = ADR.LOCATION_ID (+)
AND CI.CIRCUIT_DESIGN_ID IN (
SELECT DISTINCT CIRCUIT_DESIGN_ID
FROM DLR_CIRCUIT_DESIGN_LINE
WHERE LOCATION LIKE '% <some value from other code> %')
Note the extra (+). This is old-style join, you should now use ANSI joins instead.

Formatted Input in Table in plsql

I want to input the following things -
ACCEPT p_cname PROMPT 'Enter Customer Name: '
ACCEPT p_cyear PROMPT 'Enter Car Year: '
ACCEPT p_color PROMPT 'Enter Car Color: '
ACCEPT p_make PROMPT 'Enter Car Make: '
ACCEPT p_model PROMPT 'Enter Car Model: '
ACCEPT p_trim PROMPT 'Enter Car Trim: '
ACCEPT p_enginetype PROMPT 'Enter Car Engine Type: '
ACCEPT p_option PROMPT 'Enter Option Name: '
ACCEPT p_ocode PROMPT 'Enter Option Code: '
then add it in table as
NAME - WANT
Name - year color model trim enginetype 'w/' Option ( ocode )
I tried to format it using -
INSERT INTO table
VALUES ('&p_cname', '&p_cyear' || ' ' || '&p_color' || ' ' || '&p_make' || ' ' || '&p_model' ||
|| ' ' || '&p_trim' || ' ' || '&p_enginetype' || ' ' || '&p_option' || '(' || '&p_ocode' || ')');
BUT IT DOED NOT WORK.
The error you pasted into comments doesn't correspond to the code in your question. If I execute the code I get:
SQL> #test
Enter Customer Name: a
Enter Car Year: b
Enter Car Color: c
Enter Car Make: d
Enter Car Model: e
Enter Car Trim: f
Enter Car Engine Type: g
Enter Option Name: h
Enter Option Code: i
old 2: VALUES ('&p_cname', '&p_cyear' || ' ' || '&p_color' || ' ' || '&p_make' || ' ' || '&p_model' ||
new 2: VALUES ('a', 'b' || ' ' || 'c' || ' ' || 'd' || ' ' || 'e' ||
old 3: || ' ' || '&p_trim' || ' ' || '&p_enginetype' || ' ' || '&p_option' || '(' || '&p_ocode' || ')')
new 3: || ' ' || 'f' || ' ' || 'g' || ' ' || 'h' || '(' || 'i' || ')')
|| ' ' || 'f' || ' ' || 'g' || ' ' || 'h' || '(' || 'i' || ')')
*
ERROR at line 3:
ORA-00936: missing expression
SQL>
That's because you have two concatenation operators missing it's second operand:
'&p_model' || || ' ' || '&p_trim'
Eliminate the extra operator and the insert works:
'&p_model' || ' ' || '&p_trim'

How would I return a salary + a 5% raise?

I am trying to return a salary + a 5% raise. I thought the following code would work but it gives me an error. Can anyone help?
SELECT last_name || ' ' || salary || ' ' ||salary*.05+salary || ' ' FROM f_staffs;
The trick is this:
SELECT last_name || ' ' || salary || ' ' || (1.05*salary) || ' ' FROM f_staffs;
are you need all in one field? if yes assuming oracle:
SELECT last_name || ' ' || to_char(salary) || ' ' ||to_char(salary*.05+salary) || ' ' FROM f_staffs;