This question already has answers here:
SQL Oracle LEFT JOIN and SUBQUERY error: ORA-00905: missing keyword
(2 answers)
Closed 5 years ago.
select buses.bus_no,
buses.bus_name
from buses
join
(
select sc1.bus_no
from schedule as sc1
join schedule as sc2 on sc1.source = sc2.destination
and sc1.destination = sc2.source
) as s1 on buses.bus_no = s1.bus_no;
Oracle does not support as for table aliases. So, you can write this as:
select b.bus_no, b.bus_name
from buses b join
(select sc1.bus_no
from schedule sc1 join
schedule sc2
on sc1.source = sc2.destination and sc1.destination = sc2.source
) s1
on b.bus_no = s1.bus_no;
Related
This question already has answers here:
Incorrect syntax near ')' in SQL
(2 answers)
Closed 5 months ago.
I manage a scientific database that can use both MS Access and SQL Server. I wish to select data into a third table from a select query joining two tables in the database. The following query works for Access but fails for SQL Server. Can anyone suggest a solution? Note that I do not wish to select duplicates so have used a LEFT JOIN and a RIGHT JOIN, rather than a FULL OUTER JOIN.
SELECT *
INTO AA
FROM (
SELECT P.Well, P.Type, P.Depth, P.Temperature, S.Rotation
FROM PT P
LEFT JOIN SPIN S
ON S.Well = P.Well AND S.Depth = P.Depth
WHERE P.Type = 'PT-S'
UNION
SELECT S.Well, S.Type, S.Depth, P.Temperature, S.Rotation
FROM PT P
RIGHT JOIN SPIN S
ON S.[Well] = P.[Well] AND S.Depth = P.Depth
WHERE S.Type = 'SPIN-PT'
)
you should name your query like this:
SELECT *
INTO AA
FROM (
SELECT P.Well, P.Type, P.Depth, P.Temperature, S.Rotation
FROM PT P
LEFT JOIN SPIN S ON S.Well = P.Well AND S.Depth = P.Depth
WHERE P.Type = 'PT-S'
UNION
SELECT S.Well, S.Type, S.Depth, P.Temperature, S.Rotation
FROM PT P
RIGHT JOIN SPIN S ON S.[Well] = P.[Well] AND S.Depth = P.Depth
WHERE S.Type = 'SPIN-PT'
) As QueryName
This question already has answers here:
How do I create a table based on another table [duplicate]
(2 answers)
Closed 6 years ago.
everyone! I would like to save the table after I used INNER JOIN.
CREATE TABLE result AS (
SELECT measures.Day,
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure)
But I have an error "Invalid syntax next to ( ". Why?
Thank you in advance :)
You need to use the INTO clause, see example below:
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit INTO result
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit
INTO result
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure
This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 5 years ago.
Here is my query:
select
A.school_dim_id,
A.platform_dim_id,
A.month_dim_id,
CASE WHEN B.SCHOOLTYPE NOT IN ('International') THEN B.ROLLSIZE
WHEN B.SCHOOLTYPE IN ('International') THEN MAX(D.ROLE_SIZE)
END ROLL_SIZE
from
rag_raw_event_detail_fact A inner join rag_school_dim B on (A.school_dim_id = B.school_dim_id)
inner join rag_subscription_detail_fact C on (A.school_dim_id = C.school_dim_id)
inner join rag_intl_school_roll_static D on (D.isbn = c.isbn)
inner join rag_platform_dim E on (E.platform_dim_id = A.platform_dim_id)
where
E.PLATFORM_EVENT = 'alp_wordsmith_resources_opened'
and C.service in ('PriHubsWordsmith','PriHubsWordsmithGlobal')
and C.enddate > sysdate
group by
A.school_dim_id,
A.platform_dim_id,
A.month_dim_id
While running the above SQL, I'm getting the error as mentioned below:
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:
Error at Line: 5 Column: 13
I need help to overcome the above error.
You must place all columns from SELECT which is not in group function in GROUP BY section. So it will be:
select
A.school_dim_id,
A.platform_dim_id,
A.month_dim_id,
CASE WHEN B.SCHOOLTYPE NOT IN ('International') THEN B.ROLLSIZE
WHEN B.SCHOOLTYPE IN ('International') THEN MAX(D.ROLE_SIZE)
END ROLL_SIZE
from
rag_raw_event_detail_fact A inner join rag_school_dim B on (A.school_dim_id = B.school_dim_id)
inner join rag_subscription_detail_fact C on (A.school_dim_id = C.school_dim_id)
inner join rag_intl_school_roll_static D on (D.isbn = c.isbn)
inner join rag_platform_dim E on (E.platform_dim_id = A.platform_dim_id)
where
E.PLATFORM_EVENT = 'alp_wordsmith_resources_opened'
and C.service in ('PriHubsWordsmith','PriHubsWordsmithGlobal')
and C.enddate > sysdate
group by
A.school_dim_id,
A.platform_dim_id,
A.month_dim_id,
B.SCHOOLTYPE,
B.ROLLSIZE
But after that you may get not expected result. In that case you will have to rewrite your query.
This question already has answers here:
Oracle "(+)" Operator
(4 answers)
Closed 9 years ago.
We tried to migrate from Oracle to Postgres. We use ora2pg , but we have an error with this code:
SELECT DISTINCT UPU.USUA_C_USUARIO
FROM GN_USUARIOS U,TR_USUARIOS_X_PERFILES_USUARIO UPU,TR_V_PERFILES_USUARIOS PU
WHERE (U.C_USUARIO = UPU.USUA_C_USUARIO(+))
AND (UPU.PEUS_X_PEUS = PU.X_PEUS)
AND U.C_USUARIO = USU.C_USUARIO))
OR NOT EXISTS (
SELECT UPU2.USUA_C_USUARIO
FROM TR_USUARIOS_X_PERFILES_USUARIO UPU2
WHERE UPU2.USUA_C_USUARIO = USU.C_USUARIO)
OR USER = (
SELECT V_CONSTANTE
FROM GN_CONSTANTES
WHERE C_CONSTANTE = 'TRUSUPROP')
We have an error with PU.USUA_C_USUARIO(+). We dont have enough experience in this kind of conversions. How can we transform the code with LEFT OUTER JOIN ?
Thanks!
Probably like so:
FROM GN_USUARIOS U
LEFT JOIN TR_USUARIOS_X_PERFILES_USUARIO UPU ON U.C_USUARIO = UPU.USUA_C_USUARIO
LEFT JOIN TR_V_PERFILES_USUARIOS PU ON UPU.PEUS_X_PEUS = PU.X_PEUS
WHERE U.C_USUARIO = USU.C_USUARIO)
Or possibly:
FROM GN_USUARIOS U
LEFT JOIN ( SELECT UPU.USUA_C_USUARIO, ...
FROM TTR_USUARIOS_X_PERFILES_USUARIO UPU
JOIN TR_V_PERFILES_USUARIOS PU ON UPU.PEUS_X_PEUS = PU.X_PEUS
) UPU_PU ON U.C_USUARIO = UPU_PU.USUA_C_USUARIO
WHERE U.C_USUARIO = USU.C_USUARIO)
In either case, be wary that there's a USU table that is defined nowhere and mismatched parenthesis in your query.
This question already has answers here:
ORA-00918: column ambiguously defined in SELECT *
(4 answers)
Closed 8 years ago.
Here is the SQL:
SELECT alloc.oa_id
FROM qdod.qtran_owner_allocation alloc
INNER JOIN
(SELECT h.oa_id, h.div_ord_no, h.process_queue_id, h.from_ba_no,
h.from_ba_suf, h.from_interest_type_cd, h.from_interest_type_cd, h.from_div_ord_grp,
h.transfer_percent, h2.original_net_amount, h2.new_net_amount
FROM qdod.qtran_fund_transfer_hist h
INNER JOIN
(SELECT DISTINCT h0.oa_id, h0.original_net_amount, h1.new_net_amount
FROM qdod.qtran_fund_transfer_hist h0
INNER JOIN
(SELECT h4.oa_id, SUM (h4.new_net_amount) AS new_net_amount
FROM qdod.qtran_fund_transfer_hist h4
GROUP BY h4.oa_id) h1
ON h0.oa_id = h1.oa_id
WHERE h0.original_net_amount <> h1.new_net_amount AND h0.oa_id >= 100000000) h2
ON h.oa_id = h2.oa_id) h3
ON alloc.oa_id = h3.oa_id;
Every column has it's table defined. The main inner join (the one after the alloc table) runs fine when ran by itself. Any ideas why this is not working? This is being executed against an Oracle 10.2.0.4 database (I have also tried it against an 11.2.0.1 database thinking if it was an Oracle bug it would be resolved in 11.2, but it failed there as well).
Field duplicated in the statement, might have something to do with it
h.from_interest_type_cd, h.from_interest_type_cd,
You seem to be selecting a lot of columns you don't really need as you're not using them anywhere. The query could probably be simplified to:
SELECT alloc.oa_id
FROM qdod.qtran_owner_allocation alloc
INNER JOIN
(SELECT h.oa_id
FROM qdod.qtran_fund_transfer_hist h
INNER JOIN
(SELECT DISTINCT h0.oa_id
FROM qdod.qtran_fund_transfer_hist h0
INNER JOIN
(SELECT h4.oa_id, SUM (h4.new_net_amount) AS new_net_amount
FROM qdod.qtran_fund_transfer_hist h4
GROUP BY h4.oa_id) h1
ON h0.oa_id = h1.oa_id
WHERE h0.original_net_amount <> h1.new_net_amount AND h0.oa_id >= 100000000) h2
ON h.oa_id = h2.oa_id) h3
ON alloc.oa_id = h3.oa_id;