Postgresql "subquery in FROM must have an alias" error - sql

I'm doing a quick query using Postgresql 8.2 and I've done queries like this a thousand times before, but I can't figure out why I'm getting this error. I probably am missing something obvious, but it's saying my "subquery in FROM must have an alias". I do have an alias for my subquery "inner", and I can't figure out why else I would be getting the error.
SELECT "Branch", "Zip_5", "CountofStops", avg("EarlyTime") As
"Average_Arrival_Time"
FROM
(SELECT branch_id as "Branch", substring(stop_zip_postal_code, 1, 5) as
"Zip_5", count(stop_name) as "CountofStops", min(actual_arrival_time) as
"EarlyTime"
FROM distribution_stop_information
WHERE company_no = '001' AND route_date > '3/13/2017'
GROUP BY branch_id, stop_zip_postal_code)
inner
GROUP BY "Branch", "Zip_5"
ORDER BY Zip_5
********** Error **********
ERROR: subquery in FROM must have an alias
SQL state: 42601
Hint: For example, FROM (SELECT ...) [AS] foo.

inner is a reserved keyword. Use another name as alias.

inner . . . think "inner join". You need a better alias than that.
SELECT Branch, Zip_5, CountofStops, avg(EarlyTime) As Average_Arrival_Time
FROM (SELECT branch_id as Branch, left(stop_zip_postal_code, 5) as Zip_5,
count(stop_name) as CountofStops,
min(actual_arrival_time) as EarlyTime
FROM distribution_stop_information
WHERE company_no = '001' AND route_date > '2017-03-13'
GROUP BY branch_id, stop_zip_postal_code
) b
GROUP BY Branch, Zip_5
ORDER BY Zip_5;
Notes:
Don't wrap column names in double quotes unless needed. They are just superfluous.
Use standard formats for date constants.
LEFT() is a convenient shorthand for substring( . . ., 1, . . .)

Related

What is the syntax problem here using this subquery inside where clause

SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.

How to fix ORA-00918: column ambiguously defined

I am new to oracle sql, this code would have worked in tsql, but does not seem to work in oracle sql. I get a
ORA-00918: column ambiguously defined
. I cant spot where the issue is, can you help ?
the problem seem to be in the join, because when i replace the join with a simple where clause, the error goes away
WITH cte1 AS
(select t1.CONTAINER_NUMBER as t1_CONTAINER_NUMBER,
t1.LAST_UPDATE_TIME as t1_LAST_UPDATE_TIME,
t1.TRANSACTION_TIME as t1_TRANSACTION_TIME,
t1.EVENT,
t1.LINE_OPERATOR,
t1.BOOKING_NUMBER,
t1.ISO_SIZE_TYPE,
t1.GROSS_WEIGHT,
t1.CATEGORY,
t1.STATUS,
t1.POS_MODE,
t1.ARR_MODE,
t1.ARR_CARRIER,
t1.ARR_VOYAGE,
t1.DEP_CARRIER,
t1.DEP_MODE,
t1.DEP_CARRIER,
t1.LOAD_PORT,
t1.DESTINATION_PORT,
t1.TRUCKER_LICENSE,
t1.FROM_RAILCAR_NAME,
t1.TO_RAILCAR_NAME,
t1.YARDARRIVAL_TIME,
t1.YARDDEPARTURE_TIME,
t1.CUSTOMER_REFERENCE,
t1.CONTAINER_LENGTH,
t1.CONTAINER_WIDTH,
t1.CONTAINER_HEIGHT,
t1.TERMINAL_ARRIVAL_TIME,
t1.TERMINAL_DEPARTURE_TIME
from CONTAINER_HISTORY_MVW t1
where t1.CONTAINER_NUMBER = 'BEAU2105501'
and t1.EVENT is not null),
cte2 AS
(select t2.CONTAINER_NUMBER as t2_CONTAINER_NUMBER,
t2.LAST_UPDATE_TIME as t2_LAST_UPDATE_TIME,
t2.TRANSACTION_TIME as t2_TRANSACTION_TIME,
t2.CREATION_TIME,
t2.EQUIPMENT_MOVE_TYPE,
t2.FROM_EQUIPMENT_NUMBER
from CONTAINER_HISTORY_MVW t2
where t2.CONTAINER_NUMBER = 'BEAU2105501'
and t2.EVENT is null)
SELECT cte1.t1_TRANSACTION_TIME,
cte1.t1_CONTAINER_NUMBER,
cte1.t1_LAST_UPDATE_TIME,
cte1.EVENT,
cte1.BOOKING_NUMBER,
cte2.t2_TRANSACTION_TIME,
cte2.CREATION_TIME,
cte2.EQUIPMENT_MOVE_TYPE,
cte2.FROM_EQUIPMENT_NUMBER
from cte1
inner join cte2
on cte1.t1_CONTAINER_NUMBER = cte2.t2_CONTAINER_NUMBER
and cte1.t1_LAST_UPDATE_TIME = cte2.t2_LAST_UPDATE_TIME
order by cte1.t1_TRANSACTION_TIME, cte2.t2_TRANSACTION_TIME
Most likely a cut and paste error but only select a column once or you have to alias the second to distinguish it from the first.
t1.DEP_CARRIER,
t1.DEP_MODE,
t1.DEP_CARRIER, -- duplicate column name in cte1
I would say it's because your columns you have put "as .."
try to format the column outside the WITH query.

ORA-00904: Invalid column name on a correlated subquery

I'm having a problem correlating a subquery on Oracle 8i
That subquery gives me an ORA-00904: Invalid column name, I don't understand why. Shouldn't it work?
SELECT
HIST_FA.HIFA_PLAN_CODIGO AS FE_CODIGO_PLANTEL,
HIST_FA.HIFA_NUMERO AS FE_NUMERO_CONTROL,
HIST_FA.HIFA_FECHA AS FE_FECHA_HORA_EMISION,
HIST_FA.HIFA_DEST_CLIE_CODIGO AS FE_CODIGO_CLIENTE,
-- . . . a bunch of other columns . . .
FROM
VFA_HIST_FA HIST_FA,
VFA_HIST_ITEMS_FA HIST_ITEMS_FA,
(
SELECT
HIRF_HIIF_HIFA_NUMERO AS FE_NUMERO_CONTROL,
SUM(DECODE(HIRF_RETE_CODIGO, 0, HIRF_MONTO)) AS FE_MONTO_ENTREGADO,
SUM(DECODE(HIRF_RETE_CODIGO, 5, HIRF_MONTO)) AS FE_IMPUESTO_UNICO,
SUM(DECODE(HIRF_RETE_CODIGO, 6, HIRF_MONTO)) AS FE_MARGEN_COMERCIALIZACION
FROM
VFA_HIST_ITEMS_RETENC_FA
WHERE
HIRF_HIIF_HIFA_NUMERO = HIST_FA.HIFA_NUMERO
GROUP BY
HIRF_HIIF_HIFA_NUMERO
) PP
WHERE
HIST_FA.HIFA_NUMERO = HIST_ITEMS_FA.HIIF_HIFA_NUMERO;
Of course, I can do this at the main WHERE, but then it scans all rows in the inner one.
HIST_FA.HIFA_NUMERO = PP.FE_NUMERO_CONTROL
So, not an option, since the query becomes everlasting.
Best regards!
Change the Alias Name for the column name
HIRF_HIIF_HIFA_NUMERO AS FE_NUMERO_CONTROL
to some other name and try again.

Error when using UNPIVOT in SQL Server

I have the following query:
SELECT STDEV(Value) as Value, TimeOfTest as Date
FROM myTable
unpivot
(
value
for col in (WS1, WS2, WS3, WS4, WS5, WS6, WS7, WS8, WS9, WS10, WS11,
WS12, WS13, WS14, WS15, WS16, WS17, WS18, WS19, WS20)
) un
GROUP BY TimeOfTest
ORDER BY TimeOfTest DESC
but I get an SQL server 2008 error: Incorrect syntax near the keyword 'FOR'
Would anyone know the reason why? The syntax looks correct.
Just use APPLY.
SELECT t.TimeOfTest, STDEV(ws) as Value
FROM myTable t CROSS APPLY(
(VALUES (WS1), (WS2), . . . (WS20)) v(ws)
GROUP BY t.TimeOfTest
ORDER BY t.TimeOfTest DESC;
APPLY implements something called a "lateral join". This is a very powerful (and ANSI-standard) construct, that can be used for many things beyond unpivoting. On the other hand, unpivot is very specific syntax that is used for only one purpose . . . and as you have found, may not work well other constructs such as GROUP BY (I think you could resolve your issue by using a subquery).

Error in select statement, with union all in a subquery

In Oracle 11g, I came across an error for a query and cannot figure why it is erroring on me. Here is the query:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
p1.payment_date,
p1.media_number,
p1.payment_amount,
p1.issue_number,
p1.advice_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
p1.output_tx_number_prin,
p1.output_tx_number_int,
'' as "transaction_number",
p1header.check_account_number
from
p1
left join name on p1.name_address_number = name.name_address_number
left join p1header on p1.issue_number = p1header.issue_number
UNION ALL
select
check.date_of_payment,
check.media_number,
check.payment_amount,
check.issue_number,
check.payee_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
'' as "output_tx_number_prin",
'' as "output_tx_number_int",
check.transaction_number,
check.dda_number as "check_account_number"
from check
left join name on check.payee_na_number = name.name_address_number
) main_data
Selecting individual fields like above will give me an "invalid identifier error". If I do select * then it gives me back the data without any error. What am I doing wrong here? Thank you.
The old quoted identifier problem... see point 9 in the database object naming documentation, and note that Oracle does not recommend using quoted identifiers.
You've put your column alias as lower case inside double-quotes. That means that any references to it also have to be quoted and exactly match the case. So this would work:
select
main_data.issue_number,
main_data."transaction_number"
from
...
But unless you have a burning need to have that alias like that - and I doubt you do as all the identifier names from the actual table columns are not quoted - it would be simpler to remove the double quotes from the inner selects:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
...
'' as transaction_number,
p1header.check_account_number
...
UNION ALL
select
...
'' as output_tx_number_prin,
'' as output_tx_number_int,
check.transaction_number,
check.dda_number as check_account_number
...
You don't actually need to alias the columns in the second branch of the union; the column identifiers will all be taken from the first branch.