Sub query sql compact 3.5
I have the following query in sql server compact that returns me the following error token number = 3, Token line offset = 28, Token in error = select] can someone help me with this problem? follows my query
created:
SELECT lm.cod_local, lm.des_local, es.cod_material, mat.des_material, mat.ni_material, es.qtde_estoque_atual, es.qtde_estoque_maximo,
es.qtde_estoque_minimo, CASE WHEN
(SELECT MAX(dth_alteracao)
FROM lig_movimentacao
WHERE num_turno = 1 AND cod_tipo_movimento = 2 AND dth_movimento = GETDATE() AND cod_local_origem = es.cod_local AND
cod_material = es.cod_material) IS NOT NULL THEN 'S' ELSE 'N' END AS 'conferido'
FROM lig_estocagem AS es INNER JOIN
lig_material AS mat ON mat.cod_material = es.cod_material INNER JOIN
lig_local_material AS lm ON lm.cod_local = es.cod_local
WHERE (lm.des_local = 'Galpão 29')
ORDER BY des_material
SQL Compact does not support this kind of nested SQL and does not support CASE WHEN.
You will have to split into several queries, and the pass the result from the first one as a parameter to the next one.
Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com
Similar Messages
-
Sub-Select SQL query in Oracle BI Answers
Hi
What's the proper usage of Sub-Select SQL query in Oracle BI Answers Version Oracle Business Intelligence 10.1.3.2.1?
I get [SQL_STATE: HY000] [nQSError: 10058] A general error has occured when trying to Sub Select query like:
itemno = (SELECT MIN(orders.itemno) FROM mydatabase where rownum < 2 order by orders.itemno)Maybe the best is to create a new physical and logical object for your sub-select and join this with your current objects.
-
How to get all rows that are returned in inner sub query of select statemen
If a sub query in select statement returns more than one row than how to get all those returned rows in the final
output of the query .It will be all right if all column's value repeat and that multiple output of inner query comes
in another column .
How to get that ?As Frank said, you likely want a join, and likely an outer join to replicate the select in the projection. Something like:
SELECT id,stat, section, USER_ID concerned_person
FROM table_all,
left join table2
on room_id = sectoion and
sur_role = 'r001'
WHERE section IN (SELECT code
FROM t_area
WHERE dept= 'p002')An alternative, depending on where and how you are using the statement would be something like:
SQL> WITH t AS (
2 select 1 id from dual union all
3 select 2 id from dual),
4 t1 as (
5 select 1 id, 'One' descr from dual union all
6 select 1, 'Un' from dual union all
7 select 1, 'Une' from dual)
8 SELECT t.id, CURSOR(SELECT t1.id, t1.descr from t1
9 WHERE t1.id = t.id)
10 FROM t;
ID CURSOR(SELECTT1.ID,T
1 CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
ID DESCR
1 One
1 Un
1 Une
2 CURSOR STATEMENT : 2
CURSOR STATEMENT : 2
no rows selectedJohn -
Hi, I have two issues, here is my initial code:
select
cc.name_id_no
,cc.discover_date
,cc.cla_case_no
,max(rl.year_of_incident)Non_Loss_Past_5
,rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
Now a cla_case_no can map to several year_of_incident. I only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.
To get around this I did the following which is not very efficient and I'm hoping it can be improved:
select distinct z.cla_case_no from (
select
cc.name_id_no
,cc.discover_date
,cc.cla_case_no
,max(rl.year_of_incident)Non_MW_Loss_Past_5
,rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
) z
Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
SELECT
ie ,(select distinct z.cla_case_no from (
select cc.name_id_no, cc.discover_date ,cc.cla_case_no, max(rl.year_of_incident)Non_MW_Loss_Past_5, rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
) z
where z.cla_case_no = ccx.cla_case_no
) Non_MW_Loss_Past_5
FROM etc
Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries in the Non_MW_Loss_Past_5 column
Thanks!!!
Banner:
Oracle Database 11g Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
"CORE 11.2.0.2.0 Production"
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - ProductionHi,
It looks like you have another copy of this question:
Case Statement and sub query
That's probably not your fault, but you should mark the other copy as "Answered" right away, and then you'll only have to look for replies in one place.
885178 wrote:
... Now a cla_case_no can map to several year_of_incident. I only want the cla_case_no that maps to the max year_of_incident ie There should only be a single cla_case_no corresponding to the max year_of_incident.If you know there will only be one, then you can use LAST, and you don't need GrOUP BY
To get around this I did the following which is not very efficient and I'm hoping it can be improved:
select distinct z.cla_case_no from (
select
cc.name_id_no
,cc.discover_date
,cc.cla_case_no
,max(rl.year_of_incident)Non_MW_Loss_Past_5
,rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
) zHere's one way:
SELECT MIN (cla_case_no) KEEP (DENSE_RANK LAST ORDER BY r1.year_of_incident)
AS latest_cla_case_no
FROM cla_case cc
, rbn_loss rl
WHERE cc.name_id_no = rl.customer_no
AND rl.year_of_incident > TRUNC (cc.discover_date) - 1095
AND rl.year_of_incident < TRUNC (cc.discover_date)
AND rl.type_of_loss < 1000
AND rl.timestamp < TRUNC (cc.discover_date)
AND cc.question_class IN (20, 25)
;If you'd post some sample data (CREATE TABLE and INSERT statements) and the results you want from that data, then I could test this.
Now comes the second issue: The above is actually a subquery that will link to a bigger table via cla_case_no ccx
SELECT
ie ,(select distinct z.cla_case_no from (
select cc.name_id_no, cc.discover_date ,cc.cla_case_no, max(rl.year_of_incident)Non_MW_Loss_Past_5, rl.timestamp
from cla_case cc, rbn_loss rl
where cc.name_id_no = rl.customer_no
and rl.year_of_incident < trunc(cc.discover_date)
and rl.type_of_loss < 1000
and rl.timestamp < trunc(cc.discover_date)
and (cc.question_class = 20
or cc.question_class = 25)
and (trunc(cc.discover_date)- (rl.year_of_incident)) < 1095
--and (trunc(cc.discover_date) <> (rl.year_of_incident))
group by cc.cla_case_no,name_id_no, cc.discover_date,rl.timestamp
) z
where z.cla_case_no = ccx.cla_case_no
) Non_MW_Loss_Past_5
FROM etc
Now only certain cc.cla_case_no from the subquery will corresp to the ccx_cla_case_no from the main table and the other entries will be null.
What I require is that if the subquery returns a result that IS NOT NULL to return 'Y' ELSE 'N' instead of the varies cla_case_no's and (null) entries in the Non_MW_Loss_Past_5 column
NVL2 (x, 'Y', 'N')returns 'Y' if x is NULL, and it returns 'N' if x is not NULL. X can be a scalar sub-query:
NVL2 ((SELECT ...), 'Y', 'N')You could also use an EXISTS sub-query:
CASE
WHEN EXISTS (SELECT ...)
THEN 'Y'
ELSE 'N'
END -
Dear All,
Based on below image show that the error message, As i know is aggregation problem in sub query, May i know how to solve it ? thank you.Please have enough Netiquette to post DDL and not pictures.
Think about how absurd "MIN(AVG(AGE)))" on two levels. No, really, you can see that this is absurd if you have any concept of sets.
No competent programmer would put age in a column. It is a dynamic value! The constant is birith_date. Age is computed in the presentation layers.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Possibility of launch of a sub query in a query
Hi developers,
I have an urgent problem! I have created a query to calculate with a formula an indicator but if I effectue a drill down for customer I have a correct result while when I drill down for country the rows compact the value and the result change in wrong. Is There the possibility or a functionality who allows the launch of a query with selection on another query.
Have Someone a solution to this problem ?
Thanks,
Domeico
Message was edited by: Domenico MantiHi Domeico,
I'm not sure I understand your problem correctly, but instead of launching a sub query in a query I would recommend to check, whether a variable with replacement path (reference to characteristic (constant 1)) might solve your problem.
http://help.sap.com/saphelp_nw04/helpdata/en/03/6ba03cc24efd1de10000000a114084/frameset.htm
and
http://help.sap.com/saphelp_nw04/helpdata/en/ca/5f9ac61a205a459d0e7ef313d10321/frameset.htm
Heike -
Sub-query problem on Oracle 10g
The following query works on Oracle 10.2.0.1.0 on windows,but doesn't work on Oracle 10.2.0.2.0 on Linux.
Error report: SQL Error: ORA-00904: "T"."AUDIT_USECS": invalid identifier 00904. 00000 - "%s: invalid identifier"
It works after i remove the sub-query. I found that if use fields of T in sub-query,then error occurs. Is it saying that sub-query can't access the fields in main query?
What's the problem?
Is there any grammar erros?If so,what's the right likes?
Thanks!
CREATE TABLE AUDITHISTORY(
CASENUM numeric(20, 0) NOT NULL,
AUDIT_DATE date NOT NULL,
USER_NAME varchar(255) NULL,
AUDIT_USECS numeric(6, 0) NOT NULL,
TYPE_ID INT NOT NULL )
Query:
SELECT T.CASENUM,
T.USER_NAME,
T.AUDIT_DATE AS STARTED,
(SELECT *
FROM (SELECT S.AUDIT_DATE
FROM AUDITHISTORY S
WHERE S.CASENUM=T.CASENUM AND TYPE_ID=2
AND S.USER_NAME=T.USER_NAME
AND (S.AUDIT_DATE > T.AUDIT_DATE OR (S.AUDIT_DATE = T.AUDIT_DATE AND S.AUDIT_USECS > T.AUDIT_USECS))
ORDER BY S.AUDIT_DATE ASC,S.AUDIT_USECS ASC
) WHERE rownum <= 1) AS ENDED
FROM AUDITHISTORY T WHERE TYPE_ID=1
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - Prod PL/SQL Release 10.2.0.2.0 - Production
CORE 10.2.0.2.0 Production
TNS for Linux: Version 10.2.0.2.0 - Production
NLSRTL Version 10.2.0.2.0 - ProductionTry this way:
SQL> select * from AUDITHISTORY;
CASENUM AUDIT_DAT USER_NAME AUDIT_USECS TYPE_ID
10 12-MAR-10 USER 100 1
10 14-MAR-10 USER 100 2
10 16-MAR-10 USER 100 2
SQL> SELECT T.CASENUM,
2 T.USER_NAME,
3 T.AUDIT_DATE AS STARTED,
4 (SELECT max(S.AUDIT_DATE) keep (dense_rank first order by S.AUDIT_DATE ASC,S.AUDIT_USECS ASC)
5 from AUDITHISTORY S WHERE S.CASENUM=T.CASENUM AND TYPE_ID=2
6 AND S.USER_NAME=T.USER_NAME
7 AND (S.AUDIT_DATE > T.AUDIT_DATE OR
8 (S.AUDIT_DATE = T.AUDIT_DATE AND S.AUDIT_USECS > T.AUDIT_USECS))
9 ) as ended
10 FROM AUDITHISTORY T WHERE TYPE_ID=1;
CASENUM USER_NAME STARTED ENDED
10 USER 12-MAR-10 14-MAR-10Max
http://oracleitalia.wordpress.com -
I have a doubt with the use of sub-queries. I am using a query like as below & it is giving me expected result:-
SELECT ISS.item_type_id,count(1) + (select sum(invoiced_qty) from ppbs_invoice_detail where
trunc(created_date) between '01-jul-05' and '04-jul-05' and item_type_id=ISS.item_type_id) qty
FROM ppbs_INV_SIM_SERIAL ISS
WHERE INSTR('ROAAIT',STATUS)>0
AND INSTR('NU0NU1NU2NU3SC0',CITY_CODE) =0
and ISS.item_type_id in ('SM17')
group by ISS.item_type_id --
I will re-write above query & i have been using queries like :-
SELECT ISS.item_type_id,count(1) qty
FROM ppbs_INV_SIM_SERIAL ISS
WHERE INSTR('ROAAIT',STATUS)>0
AND INSTR('NU0NU1NU2NU3SC0',CITY_CODE) =0
and ISS.item_type_id in ('SM17') and item_type_id in
(select item_type_id from ppbs_invoice_detail where
trunc(created_date) between '01-jul-05' and '04-jul-05' and item_type_id=ISS.item_type_id)
group by ISS.item_type_id
my doubt here is that the sub-query in the first query is coming before i use the table 'ppbs_inv_sim_serial ISS' whereas in the second query, the table use 'ppbs_inv_sim_serial ISS' is coming before the sub-query use. I have been using queries where table name is defined before the sub-query.
I hope, my question is clear. Please help in solving the doubt.
Regards.I don't see a problem with the table aliasing that you seem to doubt, but your first query won't run. I boiled the thing down to basics: SQL>create table t1 (one number, val_1 number );
Table created.
SQL>create table t2 (one number, val_2 number );
Table created.
SQL>insert into t1 values (1,1);
1 row created.
SQL>insert into t2 values (1,2);
1 row created.
SQL>select a.one, count(1) + ( select sum(val_2) from t2 where t2.one = a.one) qty
2 from t1 a
3 group by one;
select a.one, count(1) + ( select sum(val_2) from t2 where t2.one = a.one) qty
ERROR at line 1:
ORA-00979: not a GROUP BY expression
SQL>
SQL>select a.one, count(1) qty
2 from t1 a
3 where a.one in ( select one from t2 where one = a.one)
4 group by one;
ONE QTY
1 1It's superfluous to "where a.one in ( select.. where = a.one) but that's your code basically. I would just do "=" -
How to make outer join in Sub Query?
Hi!
I'm facing one problem. Can anyone tell me - how to make outer join in sub query?
I'm pasting one sample code -
select e.empno, e.ename,e.job,e.sal,d.deptno,d.dname
from d_emp e, d_dept d
where e.deptno(+) = (
case
when d_dept.deptno = 10
then
select deptno
from d_dept
where dname = 'SALES'
else
d_dept.deptno
end
SQL>
ERROR at line 15:
ORA-01799: a column may not be outer-joined to a subqueryHow to resolve this issue?
Regards.And any luck with this?
SQL> with emp as
2 (select 100 empno, 'Abcd' ename, 1000 sal, 10 deptno from dual
3 union all
4 select 101 empno, 'RRR' ename, 2000 sal, 20 deptno from dual
5 union all
6 select 102 empno, 'KKK' ename, 3000 sal, 30 deptno from dual
7 union all
8 select 103 empno, 'PPP' ename, 4000 sal, 10 deptno from dual
9 )
10 ,dept as
11 (select 10 deptno, 'FINANCE' dname from dual
12 union all
13 select 20 deptno, 'SALES' dname from dual
14 union all
15 select 30 deptno, 'IT' dname from dual
16 union all
17 select 40 deptno, 'HR' dname from dual
18 )
19 select e.empno, e.ename, e.sal, d.deptno, d.dname
20 from emp e,
21 (select decode(a.deptno, 10, b.deptno, a.deptno) deptno, decode(a.deptno, 10, b.dname, a.dname) dname
22 from dept a, (select deptno, dname
23 from dept
24 where dname = 'SALES'
25 ) b
26 ) d
27 where e.deptno(+) = d.deptno
28 /
EMPNO ENAM SAL DEPTNO DNAME
101 RRR 2000 20 SALES
101 RRR 2000 20 SALES
102 KKK 3000 30 IT
40 HR
SQL> Cheers
Sarma. -
i am running these SQL lines but getting error but this DISCH_SUM_ID is there in my table
disch_sum_hdr
select disch_sum_hdr.*,
SELECT CASE NVL(Disch_sum_Dtl.Disch_Res_Col_1,0) WHEN 1 THEN 'DEATH SUMMARY'
ELSE 'DISCHARGE SUMMARY' END As DiscSummTitle
from Disch_sum_Dtl
LEFT JOIN Disch_sum_Dtl Disch_sum_Dtl_1
ON Disch_sum_Dtl_1.DISCH_SUM_ID = Disch_sum_Hdr.DISCH_SUM_ID
) as DiscTitle
from disch_sum_hdr
Error
ORA-00904: "DISCH_SUM_HDR"."DISCH_SUM_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 7 Column: 45Like Sylvie, I'm not sure what you are trying to do, but I think what you are looking for is more like:
select disch_sum_hdr.*,
(SELECT CASE NVL(Disch_sum_Dtl.Disch_Res_Col_1,0) WHEN 1 THEN 'DEATH SUMMARY'
ELSE 'DISCHARGE SUMMARY' END As DiscSummTitle
from Disch_sum_Dtl
where Disch_sum_Dtl.DISCH_SUM_ID = Disch_sum_Hdr.DISCH_SUM_ID) as DiscTitle
from disch_sum_hdrYou do not need a join in the scalar sub-query here. If that is what you want, then I would be inclined to write it as a straight outer join like:
select disch_sum_hdr.*,
CASE NVL(Disch_sum_Dtl.Disch_Res_Col_1,0)
WHEN 1 THEN 'DEATH SUMMARY'
ELSE 'DISCHARGE SUMMARY' END As DiscTitle
from disch_sum_hdr
left join Disch_sum_Dtl
on Disch_sum_Dtl.DISCH_SUM_ID = Disch_sum_Hdr.DISCH_SUM_IDwhich is likely to be more efficient.
John -
Hi ,
I have created a sub query with order by on a column.( i have cutomized the IKM control append to put order by).I can see the order by condition.If i use this subquery(yellow interface) in main query(is also yellow interface) i can't see the order by condition
Subquery(Q-yellow interface):
select
PID,
START_TIME,
ACTION_TYPE_CODE
FROM
select DISTINCT
SERVICE_TRACKING_S.PID PID,
TO_TIMESTAMP(TO_CHAR(SERVICE_TRACKING_S.ACTION_TIME,'DD-MON-YY HH24:MI:SS'),'DD-MON-YY HH24:MI:SS') START_TIME,
SERVICE_TRACKING_S.ACTION_TYPE_CODE ACTION_TYPE_CODE
from KSTGDB.SERVICE_TRACKING_S SERVICE_TRACKING_S
where (1=1)
ORDER BY-----------------------------------------------
PID
,START_TIME ASC
ODI_GET_FROM
Main query(Q1--yellow interface):
select
PID,
START_TIME,
ACTION_TYPE_CODE,
RN,
RN_MAX
FROM (
select
Q.PID PID,
Q.START_TIME START_TIME,
CASE WHEN Q.START_TIME-LAG(Q.START_TIME,1,Q.START_TIME) OVER (PARTITION BY Q. PID ORDER BY Q.START_TIME)> numtodsinterval(75,'minute')
or Q.PID!=LAG(Q.PID,1,0) OVER (PARTITION BY Q.PID ORDER BY Q.START_TIME) THEN 1 ELSE Q.ACTION_TYPE_CODE END ACTION_TYPE_CODE,
ROW_NUMBER() OVER(PARTITION BY Q.PID ORDER BY Q.START_TIME) RN,
count(*) over (PARTITION BY Q.PID ORDER BY Q.START_TIME ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING )
RN_MAX
from (
select DISTINCT
SERVICE_TRACKING_S.PID PID, TO_TIMESTAMP(TO_CHAR(SERVICE_TRACKING_S.ACTION_TIME,'DD-MON-YY HH24:MI:SS'),'DD-MON-YY HH24:MI:SS') START_TIME, SERVICE_TRACKING_S.ACTION_TYPE_CODE ACTION_TYPE_CODE
from KSTGDB.SERVICE_TRACKING_S SERVICE_TRACKING_S
where (1=1)
----------------- i don't see order by here--------------------------------
) Q
where (1=1)
) ODI_GET_FROM
thanks in advance
KHi,
Add a new KM step with the SQL you want to use for the sub query and select the +"Use Current Command for Derived-Table sub-select statement"+ checkbox. This new step can be the last one of your IKM.
Basically, you can copy the select statement of the "Insert new rows" step.
Regards,
JeromeFr -
Query is executed although sub-query is error?
Hi everyone,
I have a problem want to explained.
My sub-query show an error if it's executed alone: "ORA-00904: invalid column name"
SELECT id FROM table_A WHERE field1 = 'exam' (field1 is not exist)
But when execute with:
SELECT * FROM table_B WHERE id IN (SELECT id FROM table_A WHERE field1 = 'exam')
it's ok, although we don't have any data filtered. I don't known the reason why? Can anyone explain this?
Thanks a lot!I think you're looking at something like this...
SQL> select deptno from emp where dname = 'SALES';
select deptno from emp where dname = 'SALES'
ERROR at line 1:
ORA-00904: "DNAME": invalid identifier
SQL> select deptno from dept where deptno in (select deptno from emp where dname in 'SALES');
DEPTNO
30
SQL> select deptno from dept where deptno in (select deptno from emp where emp.dname in 'SALES');
select deptno from dept where deptno in (select deptno from emp where emp.dname in 'SALES')
ERROR at line 1:
ORA-00904: "EMP"."DNAME": invalid identifier
SQL> ed
Wrote file afiedt.buf
1* select deptno from dept where deptno in (select deptno from emp where dept.dname in 'SALES')
SQL> /
DEPTNO
30In this example, "DNAME" doesn't exist in the emp table, but in the second query it seems to work. However what it's really doing is referencing the "DNAME" column of the outer query. This is proved in the third and fourth query by prefixing the DNAME column with the EMP and DEPT table names to explicitly say where we are expecting the column to come from. -
Need to include duplicate records in sub query
Hi All,
I am using the following query and i am getting error message that your sub query return dupliate records and throwing error. Actually i need these duplicate records for my report. I want to get records for
whole year like
JAN FEB MARCH .. ... ...
and idea how i can achieve this task and my query is as follows
select pmnum
,SITEID,
(select description from locations where pm.location = locations.location and pm.siteid=locations.siteid) as site,
(select description from commodities where commodities.commodity= pm.commoditygroup) as workcategory,
description, (select wonum from workorder where workorder.pmnum = pm.pmnum
and targstartdate < '2013-02-01') as jan,
(select wonum from workorder where workorder.pmnum = pm.pmnum and
workorder.status<>'CAN' and targstartdate >= '2013-02-01' and
targstartdate < '2013-03-01') as feb,
(select wonum from workorder where workorder.pmnum = pm.pmnum and
workorder.status<>'CAN' and targstartdate >= '2013-03-01' and
targstartdate < '2013-04-01') as mar
(select name from companies where companies.company = pm.vendor) as contractor
from pm where ((PM.siteid = 'AAA'))Subqueries in the SELECT column list must return a scalar value (single row, single column). If you need multiple rows returned, use a join instead. But you need to consider what will happen when more than one row is returned by more than one
of the joins because these are correlated with the pm table row but not each other. For example, let's say you have a single row returned from "pm" matching 5 sites and 3 workcategories. This will result in 15 rows being returned for the single
pm row.
Below is an untested example.
SELECT
pmnum
,SITEID
,locations.description as site
,commodities.description as workcategory
,pm.description
,workorder_jan.wonum AS jan
,workorder_feb.wonum AS feb
,workorder_mar.wonum AS mar
,companies.name AS contractor
FROM dbo.pm
LEFT JOIN dbo.locations ON pm.location = locations.location
AND pm.siteid=locations.siteid
LEFT JOIN dbo.commodities ON commodities.commodity = pm.commoditygroup
LEFT JOIN dbo.workorder AS workorder_jan ON workorder_jan.pmnum = pm.pmnum
AND workorder_jan.targstartdate < '2013-02-01'
LEFT JOIN dbo.workorder AS workorder_feb ON workorder_feb.pmnum = pm.pmnum
AND workorder_feb.status <> 'CAN'
AND workorder_feb.targstartdate >= '2013-02-01'
AND workorder_feb.targstartdate < '2013-03-01'
LEFT JOIN dbo.workorder AS workorder_mar ON workorder_mar.pmnum = pm.pmnum
AND workorder_mar.status <> 'CAN'
AND workorder_mar.targstartdate >= '2013-03-01'
AND workorder_mar.targstartdate < '2013-04-01'
LEFT JOIN dbo.companies ON companies.company = pm.vendor
WHERE pm.siteid = 'AAA';
Dan Guzman, SQL Server MVP, http://www.dbdelta.com -
Too many values issues in sub query
Hi ALL,
I have a sql query like below :
SELECT A,B,C,(SELECT D,E,F,G FROM ABC,DEF,... WHERE ABC.ID=DEF.ID......) NAME FROM TEST1,TEST2 WHERE .......
So my subquery is throwing an error as too many values so how can i get column values in my subquery through single select statement.
Please help me on thisuser13424229 wrote:
Hi ALL,
I have a sql query like below :
SELECT A,B,C,(SELECT D,E,F,G FROM ABC,DEF,... WHERE ABC.ID=DEF.ID......) NAME FROM TEST1,TEST2 WHERE .......
So my subquery is throwing an error as too many values so how can i get column values in my subquery through single select statement.Select Sub-Query can Select only 1 Column and 1 Record at a Time.
If you wish to cater it, you will have to write the same select statement for all the columns individually. viz. D, E, F, G shall amount to 4 individual selects in your main select statement. -
Queerish behavior of 'IN' sub query
Hii all,
I've encountered strange behavior of in clause.Here I'm providing a test case.
SQL> Create table r_dummy_1
2 as select object_name from user_objects
3 where rownum <= 10
4 ;
Table created.
SQL> select object_name from r_dummy_1;
OBJECT_NAME
CP_ST_CRITERIA
CP_ST_EXPENSE_CODE
CP_ST_FACILITIES
CP_ST_FACILITY_INFO
CP_ST_FAC_RESULT
CP_ST_INSTR_RESULT
CP_ST_RATINGS
CP_ST_RESULT_SET_DATE
CP_ST_RLTNSP_NAME
CP_ST_SIC_CODES
10 rows selected.I don't have a column named 'NAME' in my table 'R_DUMMY_1'.
But....
select *
from user_source
where name in(
select name
from r_dummy_1
)This query runs fine !!!!!!!!!!!!!!!
And when I individually run the inner query
SQL> select name
2 from r_dummy_1;
select name
ERROR at line 1:
ORA-00904: "NAME": invalid identifierIs this is a bug??Hi,
RGH wrote:
I thought the inner query executes first(Unlike correlated sub queries) and the outer picks the name matching in the result set.The optimizer decides whether the sub-query gets executed first or not.
Consider this query:
SELECT *
FROM scott.emp
WHERE 1 = 2
AND deptno IN (SELECT ...)
;The sub-query may not be run at all, because the optimizer "short-circuits". That is, it sees that the WHERE clause will resolve to FALSE regardless of what the sub-query produces, so it won't waste time running the sub-query.
Most of the time, assuming the sub-query is run, the results are as if the siub-query gets executed first, but in a correlated sub-query, at least part of the parent query has to be done earlier, because the values referenced in the sub-query have to be fetched before the sub-query can complete.
So in your query
select *
from user_source
where name in(
select name
from r_dummy_1
)The main query has to decide whether or not to include a row (let's say it's a row where name='PROC_X') from user_source, so it runs the sub-query. The sub-query runs, and produces a result set that includes the name from the main query, 'PROC_X'; control returns to the main query, and it compares the name to the results of the sub-query. The net effect is the same as:
select *
from user_source
where name in(name)For all I know, the optimizer may realize this, and re-write the query to actually perform the second version, never touchiung r_dummy_1 at all.
Maybe you are looking for
-
Excise invoice ..?
Hi all Can anybody explain me , what are all the transaction codes are there in SAP for below activity. 1.were to get the list of captured vendor excise invoice list. 2.How to find whether posting done for the same list or not ..? Thanks sap-mm
-
I have upgraded my iPhone 4s to iOS7. Now my podcasts I have added to ITunes and synced with iPhone do not open. When I touch the screen like in the old iOS6, nothing happens. What is the solution?
-
Has anybody had a problem with greyed out wifi on i phone 4 gs
Has anybody had a problem with greyed out wifi setting on iphone 4gs
-
[Emergency] SBS a320 speker system working full volume
Hi. I have Creative SBS a320 speaker system. Tomorrow we have a party. That speaker system will work on full volume about 2-3 hours. Can that make a problem with my speaker system? Note: My english is bad sorry.
-
I have just upgraded to itunes 7 and downloaded 2 games namely Tetris and Pacman. Both originally appeared in the left hand bar underneath music, movies etc but they have now disappeared. Tried downloading pacman again - once it downloaded it disappe