Subquery returns more than 1 row - Mysql with Visual Studio 2010 - subquery

I have the next code in mysql:
("SELECT id_viaje,
(SELECT nombre
FROM unidades,
viaje
WHERE id_unidad = id_unidades),
(SELECT nombre
FROM empleados,
viaje
WHERE id_empleado = id_conductor),
(SELECT nombre
FROM empleados,
viaje
WHERE id_empleado = id_guarda),
(SELECT nombre
FROM ciudad,
viaje
WHERE id_ciudad = id_salida),
(SELECT nombre
FROM ciudad,
viaje
WHERE id_ciudad = id_llegada),
fecha_salida,
fecha_llegada
FROM viaje; ")
I have tried LIMIT 1 at the end of each one, I have replaced = with IN and i have no idea why is showing me this error:
22:30:30 SELECT ID_Viaje, (select Nombre from unidades, viaje where ID_Unidad IN (ID_Unidades)) , (select Nombre from empleados, viaje where ID_Empleado IN (ID_Conductor)) ,(select Nombre from empleados, viaje where ID_Empleado IN (ID_Guarda)) , (select Nombre from ciudad, viaje where ID_Ciudad IN (ID_Salida)) , (select Nombre from ciudad, viaje where ID_Ciudad IN (ID_Llegada)) , Fecha_Salida, Fecha_Llegada FROM viaje limit 1
Error Code: 1242 Subquery returns more than 1 row
Please I need help very quickly!

you need to use in operator when subQuery returns more than one row.But in your case the sub query is also a problem.Please try to use what suggested by Rachcha

This error is returned when your subquery returns multiple rows when it is supposed to return a single row.
Your subquery logic is completely wrong. When you put a subquery inside a SELECT statement, you must be sure that the subquery will always return zero or one row.
What you can do now is putting a LEFT JOIN and use a few aliases in your query, like this:
SELECT id_viaje,
u.nombre AS unidade_nombre,
e_con.nombre AS conductor_nombre,
e_grd.nombre AS guarda_nombre,
c_salida.nombre AS salida_nombre,
c_llegada.nombre AS llegada_nombre,
fecha_salida,
fecha_llegada
FROM viaje v
LEFT JOIN unidades u ON v.id_unidad = u.id_unidades
LEFT JOIN empleados e_con ON e.id_empleado = v.id_conductor
LEFT JOIN empleados e_grd ON e.id_empleado = v.id_guarda
LEFT JOIN ciudad c_salida ON e.id_salida = c_salida.id_ciudad
LEFT JOIN ciudad c_llegada ON e.id_llegada = c_llegada.id_ciudad
;
Try running this in MySQL Workbench first and only if it runs successfully there then you should put it into your application code.
Also note that my code is not supposed to run in the first attempt. Please make any applicable corrections before running this code.
Read more about MySQL joins and MySQL subquery

Related

SQL Oracle Sub-query

I am having a issue getting this Sub-query to run. I am using Toad Data Point -Oracle. I get syntax error. I have tried several different ways with no luck. I am knew to sub-query's
Select *
from FINC.VNDR_ITEM_M as M
where M.ACCT_DOC_NBR = A.ACCT_DOC_NBR
(SELECT A.CLIENT_ID,
A.SRC_SYS_ID,
A.CO_CD,
A.ACCT_NBR,
A.CLR_DT,
A.ASGN_NBR,
A.FISCAL_YR,
A.ACCT_DOC_NBR,
A.LINE_ITEM_NBR,
A.MFR_PART_NBR,
A.POST_DT,
A.DRCR_IND,
A.DOC_CRNCY_AMT,
A.CRNCY_CD,
A.BSL_DT
FROM FINC.VNDR_ITEM_F A
WHERE A.CLR_DT IN (SELECT MAX(B.CLR_DT)
FROM FINC.VNDR_ITEM_F AS B
where (B.ACCT_DOC_NBR = A.ACCT_DOC_NBR and B.FISCAL_YR=A.FISCAL_YR and B.LINE_ITEM_NBR = A.LINE_ITEM_NBR and B.SRC_SYS_ID =A.SRC_SYS_ID and B.POST_DT=A.POST_DT and B.CO_CD=A.CO_CD)
and (B.CO_CD >='1000' and B.CO_CD <= '3000' or B.CO_CD ='7090') and (B.POST_DT Between to_date ('08/01/2018','mm/dd/yyyy')
AND to_date ('08/31/2018', 'mm/dd/yyyy')) and (B.SRC_SYS_ID ='15399') and (B.FISCAL_YR ='2018'))
GROUP BY
A.CLIENT_ID,
A.SRC_SYS_ID,
A.CO_CD,
A.ACCT_NBR,
A.CLR_DT,
A.ASGN_NBR,
A.FISCAL_YR,
A.ACCT_DOC_NBR,
A.LINE_ITEM_NBR,
A.MFR_PART_NBR,
A.POST_DT,
A.DRCR_IND,
A.DOC_CRNCY_AMT,
A.CRNCY_CD,
A.BSL_DT)
Your syntax is broken, you put subquery just at the end. Now it looks like:
select *
from dual as m
where a.dummy = m.dummy
(select dummy from dual)
It is in incorrect place, not joined, not aliased. What you should probably do is:
select *
from dual m
join (select dummy from dual) a on a.dummy = m.dummy
You also have some redundant, unnecessary brackets, but that's minor flaw. Full code (I cannot test it without data access):
select *
from FINC.VNDR_ITEM_M M
join (SELECT A.CLIENT_ID, A.SRC_SYS_ID, A.CO_CD, A.ACCT_NBR, A.CLR_DT, A.ASGN_NBR,
A.FISCAL_YR, A.ACCT_DOC_NBR, A.LINE_ITEM_NBR, A.MFR_PART_NBR, A.POST_DT,
A.DRCR_IND, A.DOC_CRNCY_AMT, A.CRNCY_CD, A.BSL_DT
FROM FINC.VNDR_ITEM_F A
WHERE A.CLR_DT IN (SELECT MAX(B.CLR_DT)
FROM FINC.VNDR_ITEM_F AS B
where B.ACCT_DOC_NBR = A.ACCT_DOC_NBR
and B.FISCAL_YR=A.FISCAL_YR
and B.LINE_ITEM_NBR = A.LINE_ITEM_NBR
and B.SRC_SYS_ID =A.SRC_SYS_ID
and B.POST_DT=A.POST_DT
and B.CO_CD=A.CO_CD
and (('1000'<=B.CO_CD and B.CO_CD<='3000') or B.CO_CD='7090')
and B.POST_DT Between to_date ('08/01/2018', 'mm/dd/yyyy')
AND to_date ('08/31/2018', 'mm/dd/yyyy')
and B.SRC_SYS_ID ='15399' and B.FISCAL_YR ='2018')
GROUP BY A.CLIENT_ID, A.SRC_SYS_ID, A.CO_CD, A.ACCT_NBR, A.CLR_DT, A.ASGN_NBR,
A.FISCAL_YR, A.ACCT_DOC_NBR, A.LINE_ITEM_NBR, A.MFR_PART_NBR, A.POST_DT,
A.DRCR_IND, A.DOC_CRNCY_AMT, A.CRNCY_CD, A.BSL_DT) A
on M.ACCT_DOC_NBR = A.ACCT_DOC_NBR and M.CO_CD=A.CO_CD;
You need to add an alias to the SubSelect (or Derived Table in Standard SQL):
select *
from
( select .......
) AS dt
join ....

Getting error while running an sql script in ADW

Am getting an error that goes like this:
Insert values statement can contain only constant literal values or variable references.
these are the statements in which I am getting the errors:
INSERT INTO val.summary_numbers (metric_name, metric_val, dt_create) VALUES ('Total IP Enconters',
(SELECT
count(DISTINCT encounter_id)
FROM prod.encounter
WHERE encounter_type = 'Inpatient')
,
(SELECT min(mod_loadidentifier)
FROM ccsm.stg_demographics_baseline)
);
INSERT INTO val.summary_numbers (metric_name, metric_val, dt_create) VALUES ('Total 30d Readmits',
(SELECT
count(DISTINCT encounter_id)
FROM prod.encounter_attr
WHERE
attr_name = 'day_30_readmit' AND attr_value = 1)
,
(SELECT min(mod_loadidentifier)
FROM ccsm.stg_demographics_baseline));
Change your query like this:
insert into val.summary_numbers
select
'Total IP Enconters',
(select count(distinct encounter_id)
from prod.encounter
where encounter_type = 'Inpatient'),
(select min(mod_loadidentifier)
from ccsm.stg_demographics_baseline)
When using the ADW service, I would recommend that you consider using the CTAS operation possibly combined with a RENAME. The RENAME is a metadata operation so it is fast and the CTAS is parallel where the INSERT INTO will be row by row.
You may still have a data related issue that can be hard to determine with out the create table statement.
Thanks

Spark sql when joining two or more tables using two select statements

This is my statement:
val Porders = sqlContext.sql(
"""SELECT count(STATUS_CD)
FROM s_order
WHERE STATUS_CD = 'pending' AND ROW_ID IN
( SELECT so.ROW_ID FROM s_order so
JOIN s_order_item soi
ON so.ROW_ID = soi.ORDER_ID
JOIN s_order_type sot
ON so.ORDER_TYPE_ID = sot.ROW_ID
JOIN s_product sp
ON soi.PROD_ID = sp.ROW_ID
WHERE (sp.NAME like '%VIP%' OR sp.NAME like '%BIZ%' OR sp.NAME like '%UniFi%')
AND LOWER(sot.NAME) = 'new install')
""")
I receive the following error:
ERROR : java.lang.RuntimeException: [3.3] failure: identifier expected
( SELECT so.ROW_ID FROM s_order so JOIN s_order_item soi
^
What could be the reason?
The reason, why this happens, is that subqueries are not supported: see Spark-4226.
Even a query like
sqlContext.sql(
"""SELECT count(STATUS_CD)
FROM s_order
WHERE STATUS_CD = 'pending' AND ROW_ID IN
(SELECT * FROM s_order)
""")
does not work currently (speaking of Spark SQL 1.5.1)
Try to replace your subquery by a join, e.g. https://dev.mysql.com/doc/refman/5.1/en/rewriting-subqueries.html

Ingres nested query

I try this:
select fielda from tableA A
left join (select
fieldB
from tableB
) B where A.fielda = B.fieldB
I have this error :
Une erreur s'est produite lors de l'exécution de la requête.
ERROR [42500] [CA][Ingres ODBC Driver][Ingres]Table 'select' does not exist or is not owned by you.
INFORMATIONS SUPPLÉMENTAIRES :
ERROR [42500] [CA][Ingres ODBC Driver][Ingres]Table 'select' does not exist or is not owned by you. (CAIIOD35.DLL)
How can I do?
Thank you !!
The correct syntax would use ON rather than WHERE following the subselect.
SELECT a.fielda from tableA a
LEFT JOIN (
SELECT fieldB
FROM tableB
) b ON b.fielda = b.fieldB

JPQL JOINS with nested SELECT

Can I do something like this on JPQL?
SELECT NEW com.MyDTO(p.a, p.b, q.c, q.d)
FROM
(SELECT r.* FROM MyDTO1 r ) p
LEFT OUTER JOIN
(SELECT s.* FROM MyDTO2 s ) q
ON p.x = q.y
or similar?
(Above query has mixed with native and JPQL, so don't misunderstand)
I'm having a problem with this part I think.
FROM
(SELECT r.* FROM MyDTO1 r ) p
When I'm trying to execute I'm getting this error.
Exception Description: Syntax error parsing the query [.....], unexpected token [(]
Thank you!
No, you can't. Quote from the documentation:
Note that HQL subqueries can occur only in the select or where
clauses.
Yes you can!
You have to use native queries. Here is an example:
emf = Persistence.createEntityManagerFactory("TEST")
EntityManager em = emf.createEntityManager();
String queryString = "SELECT ID FROM ( SELECT * FROM ADDRESS WHERE ID < 0)";
Query query = em.createNativeQuery(queryString);
List<BigDecimal> result = query.getResultList();

Resources