Sub Query Help
First, I'm new to oracle so please bear with me....
I have the current query which works great:
SELECT
CASE WHEN (RX.STORENBR = '101114') THEN 'CC-FTL'
WHEN (RX.STORENBR = '103073') THEN 'CC-MIA'
WHEN (RX.STORENBR = '102520') THEN 'CC-WPB'
ELSE '0'
END AS PHARMACY_NAME,
CASE WHEN (RX.STORENBR = '101114') THEN '1598762015'
WHEN (RX.STORENBR = '103073') THEN '1063423606'
WHEN (RX.STORENBR = '102520') THEN '1770580292'
ELSE '0'
END AS PHARMACY_NPI,
CASE WHEN (RX.STORENBR = '101114') THEN '1079638'
WHEN (RX.STORENBR = '103073') THEN '1019303'
WHEN (RX.STORENBR = '102520') THEN '1099286'
ELSE '0'
END AS PHARMACY_NCPDP,
CASE WHEN (RX.STORENBR = '101114') THEN 'BN9568861'
WHEN (RX.STORENBR = '103073') THEN 'BN9754450'
WHEN (RX.STORENBR = '102520') THEN 'BA8399962'
ELSE '0'
END AS PHARMACY_DEA,
PATIENT.PATSTOREID AS PT_STORE,
PATIENT.PATKEY AS PT_KEY,
(PATIENT.PATSTOREID || LPAD(PATIENT.PATKEY, 6, 0)) AS PTID,
DOCTOR.NAMELAST AS PRESCRIBER_LAST,
DOCTOR.NAMEFIRST AS PRESCRIBER_FIRST,
DOCTOR.ADRSCITY AS PRESCRIBER_CITY,
DOCTOR.ADRSSTATE AS PRESCRIBER_STATE,
DOCTOR.ADRSZIP AS PRESCRIBER_ZIP,
DOCTOR.STATENUMBER,
DOCTOR.DEAALPHAALPHA || DOCTOR.DEAALPHANBR || DOCTOR.DEAALPHASFX AS PRESCRIBER_DEA,
CASE WHEN (DOCTOR.PLANCODE01 = 'NPI') THEN DOCTOR.PLANID01
WHEN (DOCTOR.PLANCODE02 = 'NPI') THEN DOCTOR.PLANID02
WHEN (DOCTOR.PLANCODE03 = 'NPI') THEN DOCTOR.PLANID03
ELSE '0'
END AS NPI,
DOCTOR.SPECIALTY,
DRUG.NAME, DRUG.DRUGNDCNBR,
PLANS.PLANID,
(NVL(RX.CASHPAID, 0)/100) AS COPAY,
to_char(to_date(RX.FILLDATE, 'yyyyddd'), 'MM/DD/YYYY') AS DISPENSED_DATE,
RX.DISPENSEDQTY01, RX.DAYSUPP,
FROM RX
LEFT OUTER JOIN PLANS ON (RX.PLANSTOREID01=PLANS.PLANCODESTOREID AND RX.PLANKEY01=PLANS.PLANCODEKEY)
LEFT OUTER JOIN DRUG ON (RX.DISPENSEDDRUGSTOREID=DRUG.DRUGNBRSTOREID AND RX.DISPENSEDDRUGKEY=DRUG.DRUGNBRKEY)
LEFT OUTER JOIN DOCTOR ON (RX.DOCNBRSTOREID=DOCTOR.DOCSTOREID AND RX.DOCNBRKEY = DOCTOR.DOCKEY)
LEFT OUTER JOIN PATIENT ON (RX.PATNBRSTOREID=PATIENT.PATSTOREID AND RX.PATNBRKEY=PATIENT.PATKEY)
WHERE RX.FILLDATE >= 2007274
AND RX.FILLDATE <= 2007304
AND DRUG.DRUGNDCNBR = 0004038039
AND NVL(RX.RXSTATUS, 0) <> 16
AND NVL(RX.RXSTATUS, 0) <> 17
AND NVL(RX.RXSTATUS, 0) <> 18
AND NVL(RX.RXSTATUS, 0) <> 20
AND NVL(RX.RXSTATUS, 0) <> 24
AND NVL(RX.RXSTATUS, 0) <> 32
AND NVL(RX.RXSTATUS, 0) <> 33
AND NVL(RX.RXSTATUS, 0) <> 48
However, I also want to add a subquery to return the LEAST(RX.FILLDATE) on each Patient.Patkey. I belive the subquery should be something like this:
SELECT * FROM (
SELECT RX.FILLDATE AS FIRST_DATE
FROM RX
LEFT OUTER JOIN DRUG ON (RX.DISPENSEDDRUGSTOREID=DRUG.DRUGNBRSTOREID AND RX.DISPENSEDDRUGKEY=DRUG.DRUGNBRKEY)
WHERE RX.PATNBRSTOREID=PT_STORE
AND RX.PATNBRKEY=PT_KEY
AND DRUG.DRUGNDCNBR = 0004038039
ORDER BY LEAST(RX.FILLDATE)
WHERE ROWNUM = 1
How do I format the subquery so oracle performs the lookup on each record returned in the main query, based on the main query's "WHERE" Clause?
Don't know if your version supports subquery factoring this way the whole thing is much more evident
with
the_query as
< your query here >
the_subquery as
< your subquery here >
select the_query.*,the_subquery.*
from the_query
left outer join the_subquery on (< join conditions here >)or (in old notation)
select q.*,s.*
from (< your query here >) q,
(< your subquery here >) s,
where < join conditions here > Regards
Etbin
Similar Messages
-
Hi,
1. In the query below, How to display FirstActNum to the User( which is inner select decode statement ) ?
SELECT A.One, A.Two, A.Three, A.Four from
(Select FL.first_act_num as One,FL.move_num as Two, SUM (FL.payment) as Three,SUM(FL.next_amount) as Four from FirstLib FL
where FL.act_num IN (
Select
DECODE(l.first_act_num,'0000000',l.act_num,l.first_act_num) as FirstActNum
from FirstLib L, ManageAccount A
WHERE
L.act_id=A.act_id
AND A.id='123456789')
group by FL.primary_acct_num,FL.loan_num) A,OtherTable B
where A.One = B.first_act_num AND A.Two = B.move_num;
2. Is there any other better way to write these kind of queries?
ThanksSundar,
No, its different.. I am just comparing with that field.
The data looks like..
ACT_NUM FIRST_ACT_NUM
11111 11111
22222 11111
33333 11111
In some cases FIRST_ACT_NUM is not populated (i.e it will have 0000000), in this case I am using ACT_NUM
ACT_NUM FIRST_ACT_NUMM
55555 0000000
87878 0000000
90909 0000000 -
This is my table
Doctor Appointment Date Rating
111 23/12/2009 G1
111 23/12/2009 G2
111 23/12/2009 B
111 23/12/2009 D
111 23/12/2008 G1
111 23/12/2008 B
111 23/12/2007 G2
111 23/12/2007 B
111 23/12/2006 D
For Doctor 111, on a given appointment date he has multiple ratings.
I have to fetch the rows (with doctor id, appointment date)
I) which does not have G1 rating but has G2 rating for each appointment date
And vice versa ( does not have G2 rating but has G1 rating for each appointment date)
And
II) if the particular appointment date has both G1 and G2 rating then I can ignore that appointment date – i.e do not fetch any rows if that appointment date has both G1 and G2 ratings.
And
III) if the appointment date has no G1 or G2 rating, then display the appointment date and the doctor.
So, my result set should have the output from all the three conditions for each doctor.
I am not sure how it works. I am trying using correlated subqueries any suggestions???ME_XE?with data as
2 (
3 select 111 as doc_id, to_date('23/12/2009', 'dd/mm/yyyy') as app_date, 'G1' as app_type from dual union all
4 select 111 as doc_id, to_date('23/12/2009', 'dd/mm/yyyy') as app_date, 'G2' as app_type from dual union all
5 select 111 as doc_id, to_date('23/12/2009', 'dd/mm/yyyy') as app_date, 'B' as app_type from dual union all
6 select 111 as doc_id, to_date('23/12/2009', 'dd/mm/yyyy') as app_date, 'D' as app_type from dual union all
7 select 111 as doc_id, to_date('23/12/2008', 'dd/mm/yyyy') as app_date, 'G1' as app_type from dual union all
8 select 111 as doc_id, to_date('23/12/2008', 'dd/mm/yyyy') as app_date, 'B' as app_type from dual union all
9 select 111 as doc_id, to_date('23/12/2007', 'dd/mm/yyyy') as app_date, 'G2' as app_type from dual union all
10 select 111 as doc_id, to_date('23/12/2007', 'dd/mm/yyyy') as app_date, 'B' as app_type from dual union all
11 select 111 as doc_id, to_date('23/12/2006', 'dd/mm/yyyy') as app_date, 'D' as app_type from dual
12 )
13 select
14 doc_id,
15 app_date,
16 app_type
17 from
18 (
19 select
20 sum( case when app_type = 'G1' then 1 else 0 end ) over (partition by doc_id, app_date) as g1,
21 sum( case when app_type = 'G2' then 1 else 0 end ) over (partition by doc_id, app_date) as g2,
22 doc_id,
23 app_date,
24 app_type
25 from data
26 )
27 where g1 = 0 and g2 = 0
28 or
29 (
30 g1 > 0 and g2 = 0
31 )
32 or
33 (
34 g2 > 0 and g1 = 0
35 );
DOC_ID APP_DATE APP_TY
111 23-DEC-2006 12 00:00 D
111 23-DEC-2007 12 00:00 B
111 23-DEC-2007 12 00:00 G2
111 23-DEC-2008 12 00:00 G1
111 23-DEC-2008 12 00:00 B
5 rows selected.
Elapsed: 00:00:00.59
ME_XE?
ME_XE?select * from v$version;
BANNER
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
5 rows selected.
Elapsed: 00:00:00.64Is one method using analytics (which allow you to perform operations over a 'window' of data, which seems to be what you need to do). -
How to rewrite this query without sub query please help me
Hello All Good Evening,
Could you please help me with this query, how can i write this query without sub query, or how can write this query another ways
please help me
select planno, status1, count(*) Counts from
select a.ValetNO PlanNo ,
case
when JoinCode in ('00', '01', '02') then 'Actcess'
when JoinCode in ('20', '21', '22', '23','38', '39') then
'Secured' else 'Other' end Status1 ---, COUNT (*)
from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
--group by a.ValetNO
a group by planno, status1
order by 2
Thank you in Advance
MilanWhats your objective here? Sorry, am not able to understand the reason for this change.
Try the below:(Not tested)
;With cte
As
select a.ValetNO PlanNo ,
case
when JoinCode in ('00', '01', '02') then 'Actcess'
when JoinCode in ('20', '21', '22', '23','38', '39') then
'Secured' else 'Other' end Status1 ---, COUNT (*)
from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
select planno, status1, count(*) Counts from cte
a group by planno, status1
order by 2
Even below:
select a.ValetNO PlanNo ,
case
when JoinCode in ('00', '01', '02') then 'Actcess'
when JoinCode in ('20', '21', '22', '23','38', '39') then
'Secured' else 'Other' end Status1 , COUNT (1)
from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
Group by a.ValetNO ,
case
when JoinCode in ('00', '01', '02') then 'Actcess'
when JoinCode in ('20', '21', '22', '23','38', '39') then
'Secured' else 'Other' end -
Dear Sir/Madam
I'm a student who is interested in Oracle Database and
I have some problems with JOIN and Sub-query.
I hope so many of you could help me.
if i use JOIN without sub-query, may it be faster or not?
SELECT field1, field2 FROM tableA INNER JOIN tableB
if i use JOIN with sub-query, may it be faster or not?
SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
Thanks in advance!Hi,
fac30d8e-74d3-42aa-b643-e30a3780e00f wrote:
Dear Sir/Madam
I'm a student who is interested in Oracle Database and
I have some problems with JOIN and Sub-query.
I hope so many of you could help me.
if i use JOIN without sub-query, may it be faster or not?
SELECT field1, field2 FROM tableA INNER JOIN tableB
if i use JOIN with sub-query, may it be faster or not?
SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
Thanks in advance!
As the others have said, the execution plan will give you a better idea about which is faster.
If you're trying to see how using (or not using) a sub-query affects performance, make the rest of the queries as similar as possible. For example, include field3 in both queries, or ignore field3 in both queries.
In this particular case, I guess the optimizer would do the same thing either way, but that's just a guess. I can't see your execution plans.
In general, simpler code is faster, and better in other ways, too. In this case
tableB
is simpler than
(SELECT field1, field2 FROM tableB)
Why do you want a sub-query in this example? -
Can someone please help me with a sub query question?
I need to list a 3rd party contract and then a list of students who belong to that contract.
On the students segment of the listing I need to show students with student id, name, total credit hours,
total amount spent on tuition, total amount spent on books and total amount spent on misc.
This code has a query for the contract information and I get that just fine.
Then it has a query that gives me total credit hours and that works fine
then it has a query that gives me total amount spent on tuition and that works fine
but
when I add the next query to get total amount spent on books I get only the information for the contract, I don't get student stuff anymore.
I would really appreciate any guidance that you could give.
Thanks in advance, Bob Hohulski
DECLARE
l_conn utl_TCP.connection;
v_filehandle utl_file.file_type;
v_output varchar2(1000);
v_contract_id varchar2(9);
v_contract_addr1 varchar2(30);
v_contract_addr2 varchar2(30);
v_contract_city varchar2(20);
v_contract_stat varchar2(03);
v_contract_zip varchar2(10);
v_contract_name varchar2(60);
v_student_id varchar2(09);
v_student_first_name varchar2(15);
v_student_mid_name varchar2(15);
v_student_last_name varchar2(60);
v_last_out varchar2(20);
v_student_detail_code varchar2(04);
v_student_amount number(12,2);
v_student_ref_number varchar2(09);
v_credit_hrs number(7,2);
v_tuition_amount number(12,2);
v_books_amount number(12,2);
v_misc_amount number(12,2);
v_total_for_student number(12,2);
v_current_student varchar2(09);
v_sftregs_pidm varchar2(09);
v_tbraccd_pidm varchar2(09);
CURSOR c_sel_contract IS
SELECT DISTINCT spriden_id, spriden_last_name,
spraddr_street_line1, spraddr_street_line2,
spraddr_city, spraddr_stat_code, spraddr_zip
FROM spriden, spraddr
-- WHERE spriden_id = '&Enter_Id'
where spriden_id = 'T10474666'
AND spriden_pidm = spraddr_pidm
AND SPRIDEN_CHANGE_IND IS NULL;
CURSOR c_sel_student IS
SELECT DISTINCT spriden_id, spriden_first_name, spriden_mi, spriden_last_name,
sftregs_credit_hr,
tbraccd_amount,
sftregs_pidm,
tbraccd_pidm
FROM spriden, tbraccd, tbbcstu, sftregs
WHERE 559220 = tbbcstu_contract_pidm
AND spriden_pidm = tbraccd_pidm
AND spriden_pidm = tbbcstu_stu_pidm
AND spriden_pidm = sftregs_pidm
AND (sftregs_pidm, sftregs_credit_hr) IN
(SELECT DISTINCT sftregs_pidm, SUM(sftregs_credit_hr)
FROM sftregs, tbbcstu, spriden
WHERE sftregs_term_code = '201010'
AND sftregs_pidm = tbbcstu_stu_pidm
AND sftregs_pidm = spriden_pidm
GROUP BY sftregs_pidm)
AND (tbraccd_pidm, tbraccd_amount) IN
(SELECT DISTINCT tbraccd_pidm, SUM(tbraccd_amount)
-- tuition
FROM tbraccd, tbbcstu, spriden
WHERE tbraccd_term_code = '201010'
AND (tbraccd_detail_code = 'TU01' OR
tbraccd_detail_code = 'TU02' OR
tbraccd_detail_code = 'TU03' OR
tbraccd_detail_code = 'TU04')
AND tbraccd_pidm = tbbcstu_stu_pidm
AND tbraccd_pidm = spriden_pidm
GROUP BY tbraccd_pidm)
--this code works up to this point
--when I add the next query I get nothing
AND (tbraccd_pidm, tbraccd_amount) IN
(SELECT tbraccd_pidm, SUM(tbraccd_amount)
books
FROM tbraccd, tbbcstu
WHERE tbraccd_term_code = '201010'
AND (tbraccd_detail_code = 'BKSU' OR
tbraccd_detail_code = 'BKCH')
AND tbraccd_pidm = tbbcstu_stu_pidm
GROUP BY tbraccd_pidm, tbraccd_amount)
--AND (tbraccd_pidm, tbraccd_amount) IN
-- (SELECT tbraccd_pidm, SUM(tbraccd_amount)
-- misc
-- FROM tbraccd, tbbcstu
-- WHERE tbraccd_term_code = '201010'
-- AND tbraccd_pidm = tbbcstu_stu_pidm
-- AND (tbraccd_detail_code = 'AUNA' OR
-- tbraccd_detail_code = 'OTPB')
-- GROUP BY tbraccd_pidm, tbraccd_amount)
--ORDER BY tbraccd_pidm, spriden_first_name, spriden_mi, spriden_last_name,
-- tbbcstu_sponsor_ref_number, sftregs_credit_hr;
ORDER BY tbraccd_pidm;
BEGIN
v_filehandle := utl_file.fopen(location => 'UTLFILE_MISAP9',
filename => 'ban_matrix.dat',
open_mode => 'w',
max_linesize => 32767);
OPEN c_sel_contract;
LOOP
DBMS_OUTPUT.PUT_LINE('looping');
FETCH c_sel_contract INTO v_contract_id, v_contract_name,
v_contract_addr1, v_contract_addr2,
v_contract_city, v_contract_stat,
v_contract_zip;
EXIT WHEN c_sel_contract%NOTFOUND;
v_output :=
nvl(rpad(v_contract_id,9),rpad(' ',9)) ||
' ' ||
nvl(rpad(v_contract_name,60),rpad(' ',60));
utl_file.put_line(v_filehandle,v_output);
v_output :=
nvl(rpad(v_contract_addr1, 30),rpad(' ',30)) ||
' ' ||
nvl(rpad(v_contract_addr2, 30),rpad(' ',30));
utl_file.put_line(v_filehandle,v_output);
v_output :=
nvl(rpad(v_contract_city, 20), rpad(' ',20)) ||
' ' ||
nvl(rpad(v_contract_stat, 3), rpad(' ',3)) ||
' ' ||
nvl(rpad(v_contract_zip, 10), rpad(' ',10));
utl_file.put_line(v_filehandle,v_output);
utl_file.new_line(v_filehandle);
OPEN c_sel_student;
LOOP
FETCH c_sel_student into v_student_id, v_student_first_name, v_student_mid_name,
v_student_last_name,
v_credit_hrs,
v_tuition_amount,
v_sftregs_pidm,
v_tbraccd_pidm;
-- v_books_amount, v_misc_amount;
EXIT WHEN c_sel_student%NOTFOUND;
v_last_out := substr(v_student_last_name, 1, 20);
v_output :=
nvl(rpad(v_student_id, 09),rpad(' ',09)) ||
' ' ||
nvl(rpad(v_student_first_name, 15),rpad(' ',15)) ||
nvl(rpad(v_student_mid_name, 15),rpad(' ',15)) ||
nvl(rpad(v_last_out, 20),rpad(' ',20)) ||
' ' ||
nvl(rpad(v_student_ref_number, 09),rpad(' ',09)) ||
' ' ||
v_credit_hrs ||
' ' ||
v_tuition_amount ||
-- v_books_amount ||
-- ' ' ||
-- v_misc_amount;
utl_file.put_line(v_filehandle,v_output);
END LOOP;
END LOOP;
--EXCEPTION
--WHEN OTHERS THEN
-- DECLARE
-- err_msg VARCHAR2(100);
-- BEGIN
-- err_msg := 'ERR- '||SUBSTR(SQLERRM, 1,100);
-- - utl_file.put_line(v_filehandle,err_msg);
--END;
utl_file.fclose(v_filehandle);
CLOSE c_sel_contract;
CLOSE c_sel_student;
--END AR_MATRIX_PROC;
END;run this original query
SELECT DISTINCT spriden_id,
spriden_first_name,
spriden_mi,
spriden_last_name,
sftregs_credit_hr,
tbraccd_amount,
sftregs_pidm,
tbraccd_pidm
FROM spriden, tbraccd, tbbcstu, sftregs
WHERE 559220 = tbbcstu_contract_pidm
AND spriden_pidm = tbraccd_pidm
AND spriden_pidm = tbbcstu_stu_pidm
AND spriden_pidm = sftregs_pidm
AND (sftregs_pidm, sftregs_credit_hr) IN (SELECT DISTINCT sftregs_pidm, SUM(sftregs_credit_hr)
FROM sftregs, tbbcstu, spriden
WHERE sftregs_term_code = '201010'
AND sftregs_pidm = tbbcstu_stu_pidm
AND sftregs_pidm = spriden_pidm
GROUP BY sftregs_pidm)
AND (tbraccd_pidm, tbraccd_amount) IN (SELECT DISTINCT tbraccd_pidm, SUM(tbraccd_amount)
-- tuition
FROM tbraccd, tbbcstu, spriden
WHERE tbraccd_term_code = '201010'
AND (tbraccd_detail_code = 'TU01' OR
tbraccd_detail_code = 'TU02' OR
tbraccd_detail_code = 'TU03' OR
tbraccd_detail_code = 'TU04')
AND tbraccd_pidm = tbbcstu_stu_pidm
AND tbraccd_pidm = spriden_pidm
GROUP BY tbraccd_pidm)
AND (tbraccd_pidm, tbraccd_amount) IN (SELECT tbraccd_pidm, SUM(tbraccd_amount) books
FROM tbraccd, tbbcstu
WHERE tbraccd_term_code = '201010'
AND (tbraccd_detail_code = 'BKSU' OR
tbraccd_detail_code = 'BKCH')
AND tbraccd_pidm = tbbcstu_stu_pidm
GROUP BY tbraccd_pidm, tbraccd_amount)
ORDER BY tbraccd_pidm;then run this sub-query:
SELECT tbraccd_pidm, SUM(tbraccd_amount) books
FROM tbraccd, tbbcstu
WHERE tbraccd_term_code = '201010'
AND (tbraccd_detail_code = 'BKSU' OR
tbraccd_detail_code = 'BKCH')
AND tbraccd_pidm = tbbcstu_stu_pidm
GROUP BY tbraccd_pidm, tbraccd_amount)see if you have a matching tbraccd_pidm and tbraccd_amount between the two results. -
Help on Converting inerr query to sub query
Hi Guys,
This query is written in inner join, can any one try to write using sub query.
SELECT B.CNO
FROM CUSTEN A
INNER JOIN ORDS B
ON A.CNO = B.CNO
AND A.PRNO = B.PRNO
INNER JOIN ORDI C
ON B.ORDNO = C.ORDNO
INNER JOIN PRD D
ON C.PRNO = D.PRNO
WHERE B.STS = 'COM'
AND B.LMD >= A.LMD
GROUP BY B.CSTNO, C.PRNO
HAVING COUNT(B.ORDNO) > 1
Thanks in advance!
Regards,
LKRao
Edited by: kolipaka on Sep 18, 2012 6:37 PM
Edited by: kolipaka on Sep 18, 2012 6:38 PMHi,
kolipaka wrote:
Hi Guys,
This query is written in inner join, can any one try to write using sub query. Why?
We don't know your requirements, so we can't be sure if any solution we might think of meets your requirements.
... GROUP BY B.CSTNO, C.PRNOIf you really need to GROUP BY columns from different tables, then you almost certainly want to join those tables. You might be able to get the results you want with a scalar sub-query instead of a join, but that's needlessly complicated.
You said that what you posted is just a sub-query of some other query. Perhaps the solution involves making changes in that query, too. Post the complete query, CREATE TABLE and INSERT statments for a little sample data, the results you want from that data, and an explanation of how you get those results from that data. Say which columns are unique (expecially the columns used in the join conditions); that could be important. -
How to use sub query in CAML in splist
hi,
want to know whether any sub query functionality can be achieved in splist/sp doc lib using CAML designer 2013/ query builder. also inner joins need to be implemented as pe rmy requirement.
if caml designer is not supported, whats the allternative way of doing this?
can LINQ to SP be used? if yes, can anyone pls roivde how to implement the same
help is appreciatedYou can use the let keyword to scope a variable containing the result of your subquery.
However, it depends on the LINQ provider as to whether or not it can understand the command.
For example, you can create the sample like this
var result = (from p in productInQuery
join o in orderInfoQuery on p.refNo equals o.refNo
join t in productOutQuery on p.no equals t.productInNo into productIn
from t in productIn.DefaultIfEmpty()
let dateOut = (from m in orderInfoQuery where m.refNo == t.refNo select m.processData).FirstOrDefault()
orderby o.processDate descending
select new
modelNo = x.modelNo,
qty = p.qty,
dateIn = o.processDate,
dateOut = dateOut
https://www.youtube.com/watch?v=N3K0h6GDwW4
For more info, check this
http://msdn.microsoft.com/en-us/library/ee539975.aspx -
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 -
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 "=" -
Joining with sub query not working
Hi
I am new with these complex queries. I am trying to join a sub query to a query as below;
SELECT Events1.InvoiceBatch AS BatchNo, Events1.InvoiceBatchDate AS BatchDate, tblClients.Company, tblClients.ID AS ClientID, COUNT(Events1.ID) AS Invoices,
COUNT(*) - COUNT(Events1.InvoicePrintDate) AS E, Events1.InvoiceBatchFromDate AS BatchFrom, Events1.InvoiceBatchToDate AS BatchTo, (SELECT EventID, SUM(Total) FROM tblStaffBookings AS StaffBookings WHERE StaffBookings.EventID = Events1.ID GROUP BY EventID) AS Total
FROM tblEvents AS Events1 LEFT OUTER JOIN
tblClients ON Events1.ClientID = tblClients.ID
WHERE (Events1.FactoringExportDate IS NULL) OR (Events1.AccountsExportDate IS NULL) OR (Events1.InvoiceSentDate IS NULL)
GROUP BY Events1.InvoiceBatch, Events1.InvoiceBatchDate, tblClients.Company, tblClients.ID, Events1.InvoiceBatchFromDate, Events1.InvoiceBatchToDate
HAVING (Events1.InvoiceBatch = 5212)
ORDER BY tblClients.Company
I am getting these two errors;
Msg 8120, Level 16, State 1, Line 2
Column 'tblEvents.ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 116, Level 16, State 1, Line 2
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
What am I missing?
Thanks
RegardsSELECT Events1.InvoiceBatch AS BatchNo, Events1.InvoiceBatchDate AS BatchDate, tblClients.Company, tblClients.ID AS ClientID, COUNT(Events1.ID) AS Invoices,
COUNT(*) - COUNT(Events1.InvoicePrintDate) AS E, Events1.InvoiceBatchFromDate AS BatchFrom, Events1.InvoiceBatchToDate AS BatchTo,Total
FROM tblEvents AS Events1 LEFT OUTER JOIN
tblClients ON Events1.ClientID = tblClients.ID
LEFT OUTER JOIN (SELECT EventID, SUM(Total) AS Total FROM tblStaffBookings AS StaffBookings WHERE StaffBookings.EventID = Events1.ID GROUP BY EventID) sb
ON sb.EventID = Events1.ID
WHERE (Events1.FactoringExportDate IS NULL) OR (Events1.AccountsExportDate IS NULL) OR (Events1.InvoiceSentDate IS NULL)
GROUP BY Events1.InvoiceBatch, Events1.InvoiceBatchDate, tblClients.Company, tblClients.ID, Events1.InvoiceBatchFromDate, Events1.InvoiceBatchToDate,Total
HAVING (Events1.InvoiceBatch = 5212)
ORDER BY tblClients.Company
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page
Says;
Msg 4104, Level 16, State 1, Line 5
The multi-part identifier "Events1.ID" could not be bound.
on
LEFT OUTER JOIN (SELECT EventID, SUM(Total) AS Total FROM tblStaffBookings AS StaffBookings WHERE StaffBookings.EventID = Events1.ID GROUP BY EventID) sb
Regards -
Use of Sub Query in a filter is throwing error
Hi
I am using a sub query in a filter which throws me the following error in owb 9i.
PLS 00405 : Subquery cannot be used in this context.
But the use of subquery works fine in a joiner operator.
Use of Sub Query in a filter works fine in owb 2i.
Could any one please help me.
Thanks in Advance
Nanda KishoreNanda,
Please wrap the source table in a view containing the subquery and then use the view as a source rather than use the subquery in the filter.
Regards:
Igor -
How do you get a Sub Query in a Mapping
Hi, I am wanting to know how or what the work around is for getting a Sub query in a join operator?
Its a really common place thing to do, I need to add an where exists or not exists type clause to one of my mapping joins.
Can someone please advise how this can be done using OWB.
Thanks,
Richard.I'm interested too.
Please help, I had own method to management iCal's event – new line, of 10.7 worked great – now is a little terrible for me :/ -
The issue with using the multiple columns sub-query in WHERE clause
Hi All,
my database version is 10.2.
the problem i am trying to deal with is that when I use multiple column sub-query in the WHERE clause of the SELECT statement, the actual row number returned from the sub-query is different from the whole statement.
And what I found is that, whenever there is NULL in any of those columns returned from the SUB-QUERY, the outer query will just return NULL for that whole row.
Here is an example:
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t;
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT NULL 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
select * from EMP where (EMPNO, ENAME, job, MGR, HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 If I specifically deal with the NULL situations for the columns which might return NULL, I can get the result right.
select * from EMP where (EMPNO, ENAME, job, NVL(MGR,-1), HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from EMP
intersect
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT null 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 the problem is that, I feel this is a very lame way of handling it.
So, I wonder or am asking if there is any better or standard way to do it?
any help would be highly appreciated.
ThanksHi,
As you discovered, INTERSECT treats NULL as a value, but IN does not.
What you did with NVL is one way to handle the situation. If there was a chance that any of the columns could be NULL, then you might prefer something like this:
select *
from EMP
where ( EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
) in (
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from emp
intersect
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from t
);This assumes that you can identify some string (I used '~') that never occurs in the strings in these tables.
This is implicitly converting the NUMBERs. That's usually not a good thing to do. but explicitly converting them would make this even more tedious.
You should explicitly convert any DATEs to strings, however. Depending on your default format, and your data, you might get away with implicit conversions even for DATEs, but don't bet on it.
If you had to do this often, you might consider writing a user-defined function:
delimited_string (empno, ename, job, mgr, hiredate, sal, deptno) would return a string like
'7839~KING~PRESIDENT~~17-NOV-1981~5000~10'
This will make the coding easier, but beware: it will make the execution slower. -
SELECT records larger than date specified in sub query
Dear All
Thank you for your attention.
I would like to select records larger than date specified in sub query
query should be something like the following
SELECT my_order_number, my_date, my_task
FROM MYTB
WHERE my_order_number IN order_no AND my_date > date (SELECT order_no, date FROM MySubQueryResult)
(it is incorrect)
Sub query result:
order_no | date
A1 | 2014-12-21 09:06:00
A2 | 2014-12-20 09:07:00
A3 | 2014-12-20 08:53:00
A4 | 2014-12-20 08:57:00
MYTB:
my_order_number | my_task | my_date
A1 | T1 | 2014-12-21 09:06:00
A1 | T2 | 2014-12-22 10:01:00
A2 | T1 | 2014-12-20 09:07:00
A3 | T2 | 2014-12-20 08:53:00
A3 | T4 | 2014-12-21 09:30:00
A3 | T8 | 2014-12-23 20:32:00
A4 | T6 | 2014-12-20 08:57:00
expected result:
my_order_number | my_task | my_date
A1 | T2 | 2014-12-22 10:01:00
A3 | T4 | 2014-12-21 09:30:00
A3 | T8 | 2014-12-23 20:32:00
Any ideas? Thanks.
swivanHi,
try this
SELECT my_order_number, my_date, my_task
FROM MYTB
WHERE my_order_number IN (SELECT order_no FROM MySubQueryResult)
AND my_date > (SELECT date FROM MySubQueryResult)
Alternatively, you can also make use of joins to achieve the same.
Please mark solved if I've answered your question, vote for it as helpful to help other users find a solution quicker
Praveen Dsa | MCITP - Database Administrator 2008 |
My Blog | My Page
Dear Praveen Dsa
Thanks for your reply, but order_no and date are paired and related, cannot separate.
each order have its own date, so it is not working
Best Regards
swivan
Maybe you are looking for
-
Little background: Went to open Itunes, error message told me I was missing files and I was unable to open. I deleted Itunes and re-downloaded and received same message. I then "repaired" Itunes from my "unistall/change" program menu. The error messa
-
How to compile the BEA's petstore in JBuilder 7?
Hi: I want to unpack the BEA's petstore in JBuilder 7 and then deploy it to weblogic 7. What steps do I need to do? Have any advise? Thanks! John Lee
-
Found this trying to manage the storage on the ipad. Just wondering if the file size listed on the App Store is a zipped file. If so, isn't that misleading?
-
Adding subaccounts to sbcblobal email
Can't add subaccount to sbcglobal. Main account ok Takes all info for subaccount but goes into a loop when i try to add outgoing server info. Yes I've deleted everything, Manually added all info with the help of the folks at att. Still won't get past
-
Edit of multiple clips duration
Hi, I have hunderds of video clips (not stills/photos) in my iMovie and need to make them of the same length, of course shorter than their sources. Can someone help me with this in the easiest and fastest way? Please describe it, if possible. Duratio