Select and inner join with condition - abap

I would like to get customer address (street) from one table (kna1) when customer partner role is "WE" in other table (vbpa).
I've done it like this however it returns completely different address from wrong customer. What am I doing wrong?
SELECT SINGLE stras
FROM kna1
INNER JOIN vbpa ON (vbpa-kunnr)
WHERE parvw EQ 'WE'
INTO #zadrwe.
Thank you.

Your inner Join is not correct.You can use below code to see correct value.
DATA zadrwe TYPE stras_gp.
SELECT SINGLE stras
FROM kna1
INNER JOIN vbpa ON kna1~kunnr = vbpa~kunnr
WHERE parvw EQ 'WE'
INTO #zadrwe.

Related

Adding a condition to an inner join query in Oracle

I have this inner join query:
select *
from ioa_invoice_line
INNER JOIN ioa_invoice
ON ioa_invoice_line.invo_id = ioa_invoice.id ;
Now, I want to add this condition also in the above inner join that is
where ioa_invoice_line.invo_id =234
Please advise how to add this condition in above query.
As Felix says in his comment you can add it without problems:
select *
from ioa_invoice_line
INNER JOIN ioa_invoice
ON ioa_invoice_line.invo_id = ioa_invoice.id
AND ioa_invoice_line.invo_id = 234
As this is criteria on the first table, you would usually simply add this WHERE clause at the end of your query (before the semicolon of course).
However, you are dealing with an invoice table and its detail table here and the criteria is on the key linking the tables. So for readability, I would swap the tables and name the parent table first and join the child table. That feels more natural:
select *
from ioa_invoice i
join ioa_invoice_line il on il.invo_id = i.id
where i.id = 234;
select * from
ioa_invoice_line il
INNER JOIN ioa_invoice i
ON il.invo_id = i.id
where il.invo_id = 234
This format use as a professional practice

The opposite of INNER JOIN

I have query with Inner Join.
Query 1:
select *
from vehicle_models vmodel
Inner join ogpo_voilure_model md on md.Name = vmodel.VEHICLE_MODEL
Now, i Need data that not exists in these id. With another word - opposite Inner JOIN.
I tried to make query that I need, but not successfull.
Query 2:
Select top 500 *
from ogpo_voilure_model md
Where md.id not in
(
select md.id
from Novelty.dbo.vehicle_models vmodel
Inner join [ogpo_voilure_model] md on md.Name = vmodel.VEHICLE_MODEL
)
I find in StakOverflow answer like this(sixth example). But my fields are not NULL.
How I can achieve it?
The inner join means you want everything is in set table vehicle_models but also got one or more correlated row in ogpo_voilure_model
If you define the oposite to it as everything is in vehicle_models but don't got a correlated in ogpo_voilure_model
All you needs is:
select *
from [Novelty].[dbo].[vehicle_models] vmodel
Where vmodel.VEHICLE_MODEL not in
(
select md.Name
from [Novelty].[dbo].[ogpo_voilure_model] md
)
And following that definition it's right even if results returns zero rows.
If it's not the right answer you must first define what the opposite of inner join means to you. For example you maybe want to swap the tables.
What do you mean by "my fields are not NULL"?
The link that you gave in the question has an answer: FULL JOIN.
SELECT *
FROM
vehicle_models vmodel
FULL JOIN ogpo_voilure_model md on md.Name = vmodel.VEHICLE_MODEL
WHERE
md.Name IS NULL
OR vmodel.VEHICLE_MODEL IS NULL
;
This will return rows from vehicle_models and ogpo_voilure_model that don't have common Vehicle Model Name.
It would help if you could add few sample rows to the question and your expected result.

SQL Query for Inner Join

There Is three table Master, Regular and Customer.
I'm saving ControlId in customer master for both Master and Regular. I want to get Profile from Master from the Customer Record.
By using below Query. I'm able to get MasterID from regular But I want Profile.
Query
select * from customer where refId='R000003'
(select ControlId from regular where LicenseId='R000003')
Result
Master Table
Regular Table
My Query Is..
SELECT Customer.CustomerId, Regular.LicenseId, Regular.ControlId,
Master.FullName, Master.profile
FROM Customer INNER JOIN
Regular ON Customer.RefId = Regular.LicenseId INNER JOIN
Master ON Regular.ControlId = Master.MasterId
WHERE (Customer.RefId = 'R000003')
But Its showing Regural's only I want Masters record also...
Is this what you mean? I'm not sure..
select regular.ControlId, master.profile
from regular r inner join master m ON (r.controlId = m.masterId)
where regular.LicenseId='R000003'
Posting an image of your data is not helpful. No one is going to type this in.
Paste the sample.
I am going to guess that RefId and LicenseId refer to each other. I think this is the query you want:
select c.*, m.profile
from customer c join
regular r
on c.refId = r.LicenseId join
master m
on r.controlId = m.MasterId;
I would advise you to fix your tables. Join keys in different tables should have similar names, so you know they line up. In fact, I almost always name my join keys as "Id", so this query would look more like like:
select c.*, m.profile
from customer c join
regular r
on c.CustomerId = r.CustomerId join
master m
on r.MasterId = m.MasterId;

What are some good examples where SQL's OUTER JOIN is used?

I often get asked the questions in an interview that "what is an outer join in SQL"?
While it can be answered, I wonder what might be some classic and good real life examples where a (LEFT) OUTER JOIN is used?
In the Northwind database on the Customers and Orders table.
Doing an inner join will only give you customers that have placed orders.
Doing an outer join will get all customers and orders for customers that have placed orders.
To add to Robin Day's answer, you can also use a Left Outer Join to grab only customers who have NOT placed orders by checking for NULL.
SELECT *
FROM Customer
LEFT OUTER JOIN Order
ON Customer.CustomerId = Order.CustomerId
WHERE Order.CustomerId IS NULL
Following is the visual represntation of the left outer join
SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
read more about joins in the below article
http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx ( one of the best article must read )
A LEFT OUTER JOIN can be used when you want all records from one table, as well as records from another table if any.
E.g., given table User and Address, where Address has a FK to User and there could be 0 or more addresses per user:
select *
from User u
left outer join Address a on u.UserID = a.UserID
This will ensure you get all User records, regardless of whether there was a corresponding Address record or not.
If you want to show all Users that do not have addresses, you can do this:
select *
from User u
left outer join Address a on u.UserID = a.UserID
where a.UserID is null
Classic example is cutomers and orders. Some customers have orders and others do not. You want to show a list of customers with total sales. So you do a left outer join from the customer to the order and get:
Customer A: $100;
Customer B: $0;
Customer C: $500
instead of:
Customer A: $100;
Customer C: $500
Here is an example:
I need a list of all customers, with their vouchers, I also need the customers that never used vouchers.
SELECT *
FROM Customer
LEFT OUTER JOIN Voucher
ON Customer.CustomerId = Voucher.CustomerId
Get a list of all customers including any details of orders they have made. Some customers may not have made orders and therefore an INNER JOIN would exclude them from this list.
SELECT
*
FROM
Customer
LEFT OUTER JOIN
Order
ON
Customer.CustomerId = Order.CustomerId

query for inner join of table 4

I'm facing the problem of inner join of table 4
following is query given plz see & give me solution
select INSURED.FNAME + ' ' + INSURED.LNAME AS MNAME
,INSURED.MEMBCODE as MEMBERCODE
,INSURED.POLICYNO AS POLICYNO
,INSURED.POLICYFRMDATE AS POLICYFROMDATE
,INSURED.POLICYTODATE AS POLICYTODATE
, MEMBERSHIP.MRKEXTNAME AS MARKETINGEXECUTIVE
,MEMBERSHIP.EMPLOYEECOUNT AS EMPLOYEECOUNT
,INSURED.CLAIMID AS CLAIMID
,POLICY.POLICYTYPE
,POLICY.COVAMTHOSPITAL as SUMINSURED
,ORGANIZATION.ORGANIZATIONNAME
from ((INSURED
inner join MEMBERSHIP on MEMBERSHIP.MEMBERSHIPID=INSURED.MEMBERSHIPID)
inner join POLICY on MEMBERSHIP.POLICYNAME=POLICY.POLICYNAME)
inner join ORGANIZATION on ORGANIZATION.ORGANIZATIONID=MEMBERSHIP.ORGANIZATIONID
WHERE INSUREDID=427
After making it a bit more readable there doesn't seem to be much wrong. I removed the brackets as they aren't needed. I would also put the INNER JOINS the other way round as its normal to put them in this order.
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
So it should look something like
select INSURED.FNAME + ' ' + INSURED.LNAME AS MNAME,
INSURED.MEMBCODE as MEMBERCODE,
INSURED.POLICYNO AS POLICYNO,
INSURED.POLICYFRMDATE AS POLICYFROMDATE,
INSURED.POLICYTODATE AS POLICYTODATE,
MEMBERSHIP.MRKEXTNAME AS MARKETINGEXECUTIVE,
MEMBERSHIP.EMPLOYEECOUNT AS EMPLOYEECOUNT,
INSURED.CLAIMID AS CLAIMID,
POLICY.POLICYTYPE,
POLICY.COVAMTHOSPITAL as SUMINSURED,
ORGANIZATION.ORGANIZATIONNAME
FROM INSURED
INNER JOIN MEMBERSHIP on INSURED.MEMBERSHIPID=MEMBERSHIP.MEMBERSHIPID
INNER JOIN POLICY on POLICY.POLICYNAME=MEMBERSHIP.POLICYNAME
INNER JOIN ORGANIZATION on MEMBERSHIP.ORGANIZATIONID=ORGANIZATION.ORGANIZATIONID
WHERE INSUREDID=427
You need to tell us what the problem is. Does it return results different to what you are expecting or does it fail with an error.
You should also tell us which database you are using.
Also there appears to be something odd about your table structure. You would not normally store PolicyName in your Membership Table rather you should be linking with ID Fields of some kind rather than PolicyName Strings...Something Like
INNER JOIN POLICY on POLICY.POLICYID=MEMBERSHIP.POLICYID
You got it right for with OrganizationID and MEMBERSHIPID
In short you need to tell us more about it.
Replace INNER JOIN with LEFT OUTER JOIN.
Get rid of the brackets around your inner join statements.