Query Using Joins.Please Help
Hi,
A Query please
I have a Customer table and an Order Table
The customer can place multiple orders
Create Table Customers
(Cust_id number(2),
cust_name varchar2(15),
constraint pk_custid PRIMARY KEY(Cust_id)
Create Table Orders
(Order_no number(2),
Cust_id number(2),
order_status varchar2(1),
constraint pk_orderno PRIMARY KEY(order_no),
constraint fk_custid FOREIGN KEY(cust_id)
REFERENCES Customers(cust_id)
)Now,the Customer table has a single record
whereas the Order Table has multiple records
for the same customer.
SQL> Select * from Orders
ORDER_NO CUST_ID ORDER_STATUS
1 1 P
2 1 PI'd like to view the different orders for that same customer
Using a simple Join DOES NOT give the right output:
SQL> Select C.cust_id,C.cust_name,
2 O.order_no,O.order_status
3 From Customers C,Orders O
4 where C.cust_id = O.order_no;
CUST_ID CUST_NAME ORDER_NO O
1 ABCD 1 PThere are 2 records in the Orders Table?
How to view both the records from Orders Table?
Do I need to use the EXISTS clause?
Can someone please help?
Well, I would have thought that you would join C.cust_id with the O.cust_id (and not O.order_no)???
SQL> Select C.cust_id,C.cust_name,
2 O.order_no,O.order_status
3 From Customers C,Orders O
4 where C.cust_id = O.order_no;
Similar Messages
-
Need SQL query using View - Please help
Hi,
I have similar requirement like below.
I have two tables DEPT and EMP and some departments may not have employees. I have created below view, which displays all DEPT records, even though there are no emplyees.
CREATE OR REPLACE VIEW dept_emp_vw AS
SELECT deptno, empid, 0 AS selected
FROM dept d, emp e
WHERE d.deptno = e.deptnno (+);
Ex.
DEPTNO EMPID SELECTED
10 101 0
10 102 0
20 103 0
30 103 0
40 104 0
50 <null> 0
Application will pass "empid" to the view (for ex. empid = 103) and I want result like below.
Ex.
DEPTNO EMPID SELECTED
10 101 0
10 102 0
20 103 1
30 103 1
40 104 0
50 <null> 0
Can you please let me know the query using "dept_emp_vw" view. We have Oracle 11g Release 2.
Thanks a lot for the help.Not possible using normal SQL - as SQL is not a procedure language and does not support variable declaration and use (e.g. passing empid as a variable and using it both as a predicate and as a condition in the SQL projection).
That said - SQL can be "+parameterised+". An approach that is ugly and contrary to the basic design and use of SQL. But it can support the (very weird) view definition of yours.
E.g.
SQL> create or replace procedure SetVariable( name varchar2, value varchar2 ) is
2 begin
3 DBMS_SESSION.set_context( 'MyVariables', name, value );
4 end;
5 /
Procedure created.
SQL>
SQL>
SQL> create or replace context MyVariables using SetVariable;
Context created.
SQL>
SQL> create or replace view my_funky_weird_view as
2 select
3 e.empno,
4 e.ename,
5 e.job,
6 case e.empno
7 when to_number(sys_context( 'MyVariables', 'empid' )) then
8 0
9 else
10 1
11 end as "SELECTED"
12 from emp e
13 /
View created.
SQL>
SQL> exec SetVariable( 'empid', 7499 )
PL/SQL procedure successfully completed.
SQL>
SQL> select * from session_context where namespace = 'MYVARIABLES';
NAMESPACE ATTRIBUTE VALUE
MYVARIABLES EMPID 7499
SQL>
SQL> select * from my_funky_weird_view order by selected;
EMPNO ENAME JOB SELECTED
7499 ALLEN SALESMAN 0
7521 WARD SALESMAN 1
7566 JONES MANAGER 1
7654 MARTIN SALESMAN 1
7698 BLAKE MANAGER 1
7934 MILLER CLERK 1
7788 SCOTT ANALYST 1
7839 KING PRESIDENT 1
7844 TURNER SALESMAN 1
7876 ADAMS CLERK 1
7900 JAMES CLERK 1
7902 FORD ANALYST 1
7369 SMITH CLERK 1
7782 CLARK MANAGER 1
14 rows selected.
SQL>But I will N\OT recommend doing it this way. It is not natural SQL as PL/SQL is needed to "+inject+" name-value pairs into the context for the SQL view to use. It is ugly. It is not standard. It cannot scale. It is complex to use. Etc.
Yes, there are instances when this approach is exactly what one needs - when for example dealing with a trusted context and using the contents for implementing a security layer. But in the above case - I would rather want to see the actual business requirement first, as I think you're barking up the wrong tree with the view solution you have imagined. -
Urgent query on joins --Please Help!!
Hi Experts,
We have two table T1 and T2 with A three columns in T1 as A(uniquie constraint and not null )and in table T2 as X.
We need to find rows in T1 for which no value of A exists in X of T2.Please provide your suggestion on the query we can frame for this requirement.
Thanks and Regards
Debashis
Edited by: user13267825 on Aug 27, 2010 3:08 AMMaybe something like this then:
SELECT a FROM t1 WHERE b = :something
MINUS
SELECT x FROM t2
/ This is a case where it is always helpful to provide the following:
1. Oracle version (SELECT * FROM V$VERSION)
2. Sample data in the form of CREATE / INSERT statements.
3. Expected output
4. Explanation of expected output (A.K.A. "business logic")
5. Use \ tags for #2 and #3. See FAQ (Link on top right side) for details. -
Hi,
Please give me the Select query
I have to fetch EBELN,BUKRS,LIFNR,BSART,EKORG,EKGRP,WAERS,WKURS from EKKO and
EBELP,LGORT,MATKL ,WERKS,BEDNR,IDNLF,BPRME,NETPR from EKPO and
MENGE, SAKTO,AUFNR,ANLN1,GSBER,PS_PSP_PNR from EKKN and
LPEIN,EINDT,SLFDT, from EKET.
I have to fetch all the data into an single internal table(Mandatory) by a select query using Joins.
EKKO is having EBELN as primary key
EKPO is having EBELN,EBELP as primary key
EKKN is having EBELN,EBELP as primary key
EKET is having EBELN,EBELP as primary key
Can anybody Please give me the Select query.
With Regards
AjayHi ,
I am sending this sample coed for the Inner joins .Hope this will help you:
Inner joins using 3 tables
Try this :-
SELECT stpostlnr stpoidnrk mastmatnr maramtart stpo~menge
INTO CORRESPONDING FIELDS OF TABLE zmat1 FROM mast
JOIN stpo ON stpostlnr = maststlnr
JOIN mara ON maramatnr = mastmatnr
WHERE stpostlty = 'M' "AND stpoidnrk IN s_matnr
AND mast~werks = 1000.
Here s_matnr is a select-options on the selection-screen.
Or this.
Code:
Select single VbrkBukrs VbrkKunrg Vbrk~Vbeln
VbrkFkdat VbrkBstnk_Vf Vbrk~Zterm
Tvzbt~Vtext
VbakVbeln VbakBstdk
LikpVbeln Likplfdat Likp~Lfuhr
into w_vbrk
from vbrk
inner join Tvzbt on TvzbtZterm = VbrkZterm and
Tvzbt~Spras = sy-langu
Inner join Vbfa as SalesLnk
on SalesLnk~vbeln = pu_vbeln and
SalesLnk~vbtyp_v = c_order
inner join Vbak on VbakVbeln = SalesLnkVbelv
Inner join Vbfa as DeliveryLnk
on DeliveryLnk~vbeln = pu_vbeln and
DeliveryLnk~vbtyp_v = c_Delivery
inner join Likp on LikpVbeln = DeliveryLnkVbelv
where vbrk~vbeln = pu_Vbeln.
This code locates sales, delivery and payment terms info from a billing document number.
or
Here, this one also works fine :
select zfpcdcadivi zfpcdproforma zfpcdfactura zfpcdaniofactura
zfpcdmontousd zfpcdmontoap zfpcdebeln zfpcdinco1
zfpcdlifnr lfa1name1 zcdvsstatus zfpcdconint
into it_lista
from zfpcd inner join zcdvs
on zfpcdebeln = zcdvsebeln
and zfpcdproforma = zcdvsproforma
and zfpcdlifnr = zcdvslifnr
inner join lfa1
on zfpcdlifnr = lfa1lifnr
where zcdvs~status = '04'.
Also Here is another solution that just uses inner joins:
SELECT vbakvbeln vbfaerdat INTO (itab-vbeln, itab-wadat)
FROM ( vbak INNER JOIN vbap
ON vbapvbeln = vbakvbeln )
INNER JOIN vbfa ON vbakvbeln = vbfavbelv
WHERE vbak~kunnr = m_wm AND
vbak~vbtyp = 'C' AND
vbfa~erdat IN s_date AND
vbap~matnr = 'MZ1807F' AND
vbfaposnv = vbapposnr AND
vbfa~vbtyp_n = 'J'.
<REMOVED BY MODERATOR>
Cheers,
Chandra Sekhar.
Edited by: Alvaro Tejada Galindo on Apr 10, 2008 5:20 PM -
I bought used iPhone and on this iPhone I'm not able to make new account on i Cloud'. How can I use this iCloud's on used iPhone Please help me
When I logging there it told me that maximum no of iCloud's Account already activated on this iPhone.
Please help me how can I open new iCloud's account on this iPhone
Regard
Faiz khanSet up the account using a desktop or laptop, then sign
into that account on your iPhone.
I am not aware of any method around the restriction once
the maximum has been created on any individual iPhne. -
HEY, i have some issue i am using macbook air i5...the problem is that without doing anything 1.5gb of ram out of 2gb always in use ..please help me how to make it normal....
and my battery back up is max 3 to 3.5 hours in normal use with web ....is this normal or need some betterment!!!!ADEEL UR REHMAN wrote:
...the problem is that without doing anything 1.5gb of ram out of 2gb always in use
No computer can do a thing without using memory. 1.5 GB is very little. It's normal.
3 or so hours from a full charge may also be normal, for what you are doing. As batteries age, they don't last as long on a full charge as they did when they were new. All batteries eventually wear out, and are easily replaceable. -
In Siri, I can call by my voice, but why I can not use Siri voice call in my country (Laos), just can call only us phone number; my country we use like 3 number for option call, 8 numbers for call friend but Siri cannot use this please help us, thanks
And please help me can type Laos font in it like andrio phone.Hi Cozumel,
Thanks for posting. I'm sorry you're having problems with your bills. I can take a look at this for you. Drop me an email with your account details and a link to this thread for reference. You'll find the address in my profile.
Cheers
David
BTCare Community Mod
If we have asked you to email us with your details, please make sure you are logged in to the forum, otherwise you will not be able to see our ‘Contact Us’ link within our profiles.
We are sorry but we are unable to deal with service/account queries via the private message(PM) function so please don't PM your account info, we need to deal with this via our email account :-) -
Write the following query using JOIN keyword ?
Qest:
Write the following query using JOIN keyword ?
select ID_category from category_master X
where Exist(select 1 from sub_category Y where
X.ID_category=Y.ID_category)Edited by: 799660 on Oct 3, 2010 6:05 AMselect X.ID_category
from category_master X join (select distinct ID_category from sub_category) Y
on (X.ID_category=Y.ID_category)
/SY. -
how can i access to iCloud to my iphone because it appear to the screen of my iPhone when i try to access my iCloud that the maximum number of free account have been use already please help me
Welcome to the Apple community.
Once three iCloud accounts have been created on any iOS device, it can you no longer be used to create any more accounts, regardless of what you do to it. You will need to use a computer or another iOS device to create your account and then use the details to log into your account on your phone. -
An app counting minutes and data use. Please help!!!
An app counting minutes and data use. Please help!!!
Here's one, there are others in the App store:
http://itunes.apple.com/us/app/data-usage/id386950560?mt=8
However, since your carrier is the one that bills you, the only stats that matter are your carrier's. Some carrier's offer apps that provide this info, see if yours does. -
Issues with query using joins in 3 tables
I am trying to fetch data from 3 tables (Project,RIsk and Issues) using join. There are Risks associated with some projects and Issues associated with some projects.
ProjectID is primary key in Project table.
RiskID is primary key in risk table. ProjectID is foreign key.
IssueID is primary key in Issue table.ProjectID is foreign Key.
I need the projectname, count of risks for projects, count of issues for projects. I am using joins in all the 3 tables. Issue here is, its giving me double of count of risks and issues for each project.
Please advise how can I get the correct number. I have used the below query,
select p.projectname,count(r.riskid),count(i.issueid) from project as p
left outer join risk as r on p.projectid=r.projecctid
left outer join issue as i on p.projectid=i.projectid
group by
p.projectname
thanksHi All,
I got a new requirement to count, the number of high priority risks as well as high priority issues along with the other details. I modified the below table to include the changes, but I am not getting the desired result. Could you please help?
Original query:
select p.projectname,count(distinct r.riskid), count(distinct i.issueid) from project as p
left outer join risk as r on p.projectid=r.projecctid
left outer join issue as i on p.projectid=i.projectid
group by p.projectname
Modified query:
select p.projectname,count(distinct r.riskid),sum(case when r.riskpriority='high' then 1 else 0 end), sum(case when i.issuepriority='high' then 1 else 0 end),count(distinct i.issueid) from project as p
left outer join risk as r on p.projectid=r.projecctid
left outer join issue as i on p.projectid=i.projectid
group by p.projectnameI should get the desired result as:XYZ,8,1,4,4But I am getting:XYZ,8,4,4,32thanks for the reply. -
How to know the most queried item? (please help me)
I'm doing a library's bookkeeping.
In a report I wanna know what's the book more queried or the most looked. But I dont know how to put the query in SQL.
can you please tell me?
the columns in the table are:
books varchar2(30)
author varchar2 (30)
(THERE's NOT any Book_id) or something.
please help me.That's not a Forms question. Please direct your question to the appropriate forum.
However, using those columns, there is no way to tell the book most queried, unless you store that information yourself, or use some sort of auditing in the database.
Regards,
Robin Zimmermann
Forms Product Management -
Error in select query using join
hi all
please help.
The follwing code givs this error....
select skb1-bukrs ska1-ktoks skb1-saknr skb1-waers skb1-xsalh skb1-xopvw skb1-xkres
into corresponding fields of table it_skab1
from ( skb1
OUTER JOIN ska1 on ska1-saknr=skb1-saknr ) .
*Error while executing....*
Program ZABHI_FIREPORT
"(" has no closing ")".
Please guide me for the correct syntax.
Thank u.hi,
use this code n check
tables: ska1,skb1.
data: begin of it_skab1 occurs 0,
bukrs like skb1-bukrs,
ktoks like ska1-ktoks,
saknr like skb1-saknr,
waers like skb1-waers,
xsalh like skb1-xsalh,
xopvw like skb1-xopvw,
xkres like skb1-xkres,
end of it_skab1.
select skb1~bukrs
ska1~ktoks
skb1~saknr
skb1~waers
skb1~xsalh
skb1~xopvw
skb1~xkres
from skb1 left outer join ska1 on skb1saknr = ska1saknr
into corresponding fields of table it_skab1 up to 10 rows.
loop at it_skab1.
write: / it_skab1-bukrs,it_skab1-ktoks,it_skab1-saknr,it_skab1-waers,it_skab1-xsalh,
it_skab1-xopvw,it_skab1-xkres.
endloop.
n let me knw is ur doubt cleared
rgds
shivraj
Edited by: ShivrajSinha on May 27, 2009 8:27 AM -
Hi,
I am trying to join two tables using joining.
SQL> select * from test10;
ID
1
2
SQL> select * from test11;
ID
1
3
4
My expected output is
ID
1
2
3
4
I am able to do this using "Union"
SQL> select * from test10
2 union
3 select * from test11;
ID
1
2
3
4
But i want to achieve this using join. Please help..
Thanks & Regards,
ArijitHi, Arijit,
The way to do that with a join is FULL OUTER JOIN, like this:
SELECT NVL (t10.id, t11.id) AS id
FROM test10 t10
FULL OUTER JOIN test11 t11 ON t10.id = t11.id
ORDER BY 1
;If that's exactly what you're trying to do, you should stick with the UNION.
FULL OUTER JOIN is good when there are other columns involved. -
Hi,
I want to write a SQL statement using Joins. The base table "A" has about 100 fields and the join table "B" a few. I would like to fetch all fields from table "A" and a few from table "B".
Is there a way to write a query wherein I can use "Select * " for table "A" rather than writing all 100 field name in the query? Please keep in mind that I want to use a Join.
Thanks.Hi Thomas,
but if there are some fields which are in both tables, SELECT * will be not sufficient. E.g. fields ERNAM, ERDAT, AENAM, AEDAT are such fields which can occur in more tables. In case that a target area will contain these fields, by * with addition INTO CORRESPONDING FIELDS OF, you are not able to specify from which table these values should be taken. In such case, it can be solved by specifying field list dynamically. If it is not that case SELECT * is sufficient. You should check intersection of common fields for tables you are joining. Thereafter you should decide if you can use SELECT *.
Maybe for better understanding, what exactly I am talking about, look at this example:
TYPES: BEGIN OF ts_data,
ernam TYPE mara-ernam,
END OF ts_data.
DATA: ls_data TYPE ts_data,
ls_mara TYPE mara,
ls_mchb TYPE mchb.
START-OF-SELECTION.
ls_mara-matnr = '1'.
ls_mchb-matnr = ls_mara-matnr.
ls_mara-ernam = 'A'.
ls_mchb-ernam = 'B'.
INSERT mara FROM ls_mara.
INSERT mchb FROM ls_mchb.
SELECT SINGLE * FROM mara INNER JOIN mchb ON mara~matnr = mchb~matnr INTO CORRESPONDING FIELDS OF ls_data.
ROLLBACK WORK.
You as a developer are not able to tell from which table common fields should be selected. But maybe this is really too specific case...
Regards,
Adrian
Maybe you are looking for
-
Help needed in absence valuation
Hi all, I have a specific requirement from my client. My client wants to a pay premium to some management( salaried employees based up their Personnel area location. In our company all Management employees belong to one personnel sub area. So I have
-
Help needed to create a CSS 'style-switcher' button.
Hi, I need some help please in creating alternate style sheets for each page on a site I'm creating to enable users to change text size (i.e. small, normal and large options - sometimes, symbolized as a letter 'A' in varying sizes). The site has been
-
Safari 5.1.4 crashes upon loading pages
After much time getting my MBP 5,1 10.6.8 running right another bug has cropped up, Safari crashes under these circumstances: Only when loading pages. If Safari is only opened it's fine. In normal user mode. In another administrator ac
-
How to put Azure blob Storage service off-line
I am doing a migration and like to make sure Azure Storage service is off-line to make sure no content is pointing to this account before deleting exiting Storage account. Please help.
-
A transaction code ZME22n has been created in the R/3 system as a variant to ME22N. Now, when the transaction is being run, it calls for ME22N internally and is thus causing authorization issues to the users since this transaction has not been grante