Using oracle SQL developer,
I have two views that have different columns. How can I join column 1 from view A into view B? I want to join AR_INVOICE_INQ_V.CLI_NAME CLI_NAME from view B into view A.
View A
CREATE OR REPLACE FORCE VIEW "KPRD"."AC_INPUT_AUDIT" ("CLIENT", "SEQUENCE_CODE", "EMPLOYEE", "ORIGINAL_DATE", "HOURS_SPENT", "LONG_DESCRIPTION", "TIME_AT_ACTUAL", "MANAGER")
AS
SELECT CLIENT,
SEQUENCE_CODE,
EMPLOYEE,
ORIGINAL_DATE,
HOURS_SPENT,
LONG_DESCRIPTION,
TIME_AT_ACTUAL,
MANAGER
FROM TIME_DETAILS
WHERE MANAGER BETWEEN 'AAGG' AND 'AZJE'
AND ORIGINAL_DATE > '28/SEP/2012'
AND TIME_AT_ACTUAL ! = 0;
View B
CREATE OR REPLACE FORCE VIEW "KPRD"."AC_AR_INV_BAL" ("CLIENT", "CLI_NAME", "PARTNER", "PAR_NAME", "MANAGER", "REFERENCE", "STATUS_CODE", "INVOICE_DATE", "TIME_BILLED", "GROUP_CLIENT_CODE", "GRPCLI_NAME", "INVOICE_BALANCE")
AS
SELECT AR_INVOICE_INQ_V.CLIENT CLIENT,
AR_INVOICE_INQ_V.CLI_NAME CLI_NAME,
AR_INVOICE_INQ_V.PARTNER PARTNER,
AR_INVOICE_INQ_V.PAR_NAME PAR_NAME,
AR_INVOICE_INQ_V.MANAGER MANAGER,
AR_INVOICE_INQ_V.REFERENCE REFERENCE,
AR_INVOICE_INQ_V.STATUS_CODE STATUS_CODE,
AR_INVOICE_INQ_V.INVOICE_DATE INVOICE_DATE,
AR_INVOICE_INQ_V.TIME_BILLED TIME_BILLED,
AR_INVOICE_INQ_V.GROUP_CLIENT_CODE GROUP_CLIENT_CODE,
AR_INVOICE_INQ_V.GRPCLI_NAME GRPCLI_NAME,
AR_INVOICE_INQ_V.INVOICE_BALANCE
FROM AR_INVOICE_INQ_V
WHERE MANAGER BETWEEN 'AAGG' AND 'AZGG';
The syntax for joining views is the same as for tables. I'm not entirely sure what you're trying to achieve, but I think you want all the columns from view A and one additional column from view B. Assuming CLIENT has a common value on both the syntax would be something like:
SELECT AIA.CLIENT,
AIA.SEQUENCE_CODE,
AIA.EMPLOYEE,
AIA.ORIGINAL_DATE,
AIA.HOURS_SPENT,
AIA.LONG_DESCRIPTION,
AIA.TIME_AT_ACTUAL,
AIA.MANAGER,
AAIB.CLI_NAME
FROM AC_INPUT_AUDIT AIA
JOIN AC_AR_INV_BAL AAIB ON AAIB.CLIENT = AIA.CLIENT;
This seems to assume there will be a one-to-one relationship between the records in both views, which seems unlikely, particularly since the MANAGER filters are different. So you might get a different set of results back to what you expect, possibly with duplicates and/or missing records. An outer join might address that but might also make it worse, so you'll need to be clearer how the views are related and what you're trying to achieve.
If you have access to the underlying tables, you might find it more performant to join those, but you'd probably need to test it both ways.
In order to join column 1 from view A into view B, both tables or views have to have the column 1. If you want to join AR_INVOICE_INQ_V.CLI_NAME CLI_NAME from view B into view A, View A has to have the column CLI_NAME. Otherwise you cannot do a join based on CLI_NAME column.
Related
I need to brind a lot of columns from several tables using LEFT JOIN. My starting point is the orders table and I bring the vendor name from the "Address_table". Then I add another table with order details and then the shipping information of each order detail.
My problem is that I need to bring a different record from "Address_table" to refer onether id's detailed in shipment table as of "origin_id" and "destination_id".
In other words, "address_id", "origin_id" and "destination_id" are all records from "Address_table". I brought the first one related to the vendor, how can I retrieve the other two?
Example
Thanks in advance
Your question is not exactly clear in terms of the tables and their relationships. It is, however, clear what the problem is. You need to join against the same table twice using different columns.
In order to do that you need to use table aliases. For example, you can do:
select *
from shipment s
left join address_table a on a.address_id = s.origin_id
left join address_table b on b.address_id = s.destination_id
In this example the table address_table is joined twice against the table shipment; the first time we use a as an alias, the second time b. This way you can differentiate how to pick the right columns and make the joins work as you need them to.
So, this is probably gonna sound like a weird or dumb question.
I have this application that I'm writing, which consists of four tables. Two are 'connected' to one main table, and one last one is connected to one of those two tables. I've included a database diagram to give you a better idea of what I mean.
Now, my goal is to have 'Bedrijfsnaam' from the 'Bedrijven' table into the 'Samenwerkingen' table. Problem is: I can't add more than two foreign keys, so I was assuming that I would have to create a FK in'Contactpersonen' table and pick it from the 'Bedrijven' table. It would basically mean I'd have a JOIN in 'Contactpersonen' table to my 'Bedrijven' table. And then the 'Samenwerkingen' table has a JOIN to the 'Contactpersonen' table and accesses the column from 'Bedrijven'.
Does that make any sense? Hope it does, because I could really use some help making this possible. xD
Since Samenwerkingen has a foreign key to Contactpersonen which is itself related to Bedrijven, you don't need an additional constraint: your data integrity is guaranteed and you can join the two tables.
Your query should look like :
select s.*, b.* from Samenwerkingen s
inner join Contactpersonen c on s.Contactpersonen = c.Contactpersonen
inner join Bedrijven b on c.Bedrijfnaam = b.Bedrijfnaam
I currently have a schema set up in the following manner:
The table tblCategoryRiskArea is set up as an intermediate table for the many-to-many relationship that can exist between Categories and RiskAreas.
Within the tblBase table, I would like to make it so that the RiskArea choices are dependant upon the Category choice. MS Access allows you to set a Lookup for a field in a table based upon a Row Source SQL statement. I am having trouble figuring out the correct SQL statement to define the Row Source for RiskArea dependant upon Category. This:
SELECT tblRiskAreas.RiskAreaID, tblRiskAreas.RiskArea
FROM tblRiskAreas INNER JOIN
((tblCategories INNER JOIN tblBase
ON tblCategories.CategoryId = tblBase.Category)
INNER JOIN tblCategoryRiskArea
ON tblCategories.CategoryId = tblCategoryRiskArea.Category)
ON (tblRiskAreas.RiskAreaID = tblCategoryRiskArea.RiskArea)
AND (tblRiskAreas.RiskAreaID = tblBase.RiskArea)
WHERE (((tblCategoryRiskArea.Category)=[tblBase]![Category]))
ORDER BY tblRiskAreas.RiskAreaID;
is the best I've come up with so far, using MS Access' Query Builder, so all of the Inner Joins have been created just by my having defined the relationships between the tables and dragging them into the Query Builder. This query returns nothing, however.
I suspect that it may have something to do with the circular nature of the relationships I set up?
Thank you.
Edited: tblRiskArea contains 4 RiskAreas, as follows:
Environmental
Health
Safety
Security
Each Category can fall into one or two of these RiskAreas, so the tblCategoryRiskArea creates the relationship bewtween them.
First remove Category and RiskArea from tblBase and replace them for CategoriRiskAreaID
You will show in your form 2 combos. First combo Catagory data source:
Select CategoryId,Category from tblCategories
Second combo Risk Areas data source:
Select a.CategoryRiskId, b.RiskArea
from tblCategoryRiskArea a
inner join tblRiskArea b
where a.RiskAreaId=b.RiskAreaId
AND a.category = #ComboBoxCategorySelectedItem
Now you have the value to insert in tblBase, ComboBoxSelectedItem is tblCategoryRiskArea.CategoryRiskId
I was going through union and union all logic and trying examples. What has puzzled me is why is it necessary to have same number of columns in both the tables to perform a union or union all operation?
Forgive me if my question's silly, but i couldn't get the exact answer anywhere. And conceptually thinking, as long as one common column is present, merging two tables should be easy right?(like a join operation). But this is not the case, and I want to know why?
JOIN operations do NOT require the same number of columns be selected in both tables. UNION operations are different that joins. Think of it as two separate lists of data that can be "pasted" together in one big block. You can't have columns that don't match.
Another way to look at it is a JOIN does this:
TableA.Col1, TableA.Col2 .... TableB.Col1, TableB.Col2
A UNION does this:
TableA.Col1, TableA.Col2
TableB.Col1, TableB.Col2
JOINS add columns to rows, UNIONS adds more rows to existing rows. That's why they must "match".
Join Operation combines columns from two tables.
Where as Union and Union all combines two results sets, so In order to combine two results you need to have same number of columns with compatible data types.
In real world e.g. In order to play a game of cricket you need 11 players in both team, 7 in one team and 11 in opposite team not allowed.
Lets say you have EMPTable with columns and values as below:
id, name, address, salary, DOB
1, 'SAM', '2 Merck Ln', 100000, '08/18/1980'
IF you want to UNION with only the column name (lets say value is 'TOBY'), it means you have to default other values to NULL (a smart software or db can implicitly do it for you), which in essence translates to below (to prevent the integrity of a relational table) ->
1, 'SAM', '2 Merck Ln', 100000, '08/18/1980'
UNION
NULL,'TOBY', NULL, NULL, NULL
A "union" by definition is a merger of (different) values of THE same class or type.
You need to research the difference between UNION and JOIN.
Basically, you use UNION when you want to get records from one source (table, view, group of tables, etc) and combine those results with records from another source. They have to have the same columns to get a common set of results.
You use JOIN when you want to join records from one source with another source. There are several types of JOINs (INNER, LEFT, RIGHT, etc) depending on your needs. When using JOINs, you can specify whichever columns you'd like.
Good luck.
join are basically used when we want to get data from two or more tables, and for this there is no need to fetch same number of columns, consider a situation where by using normalization concepts we have divided the table into two parts,
emp [eid, ename,edob,esal]
emp_info [eid,emob_no]
here i we want to know name and mobile no. of all employees then we will use the concept of join, because here information needed by us can't be provided by same table.
so we will use..
SELECT E.ENAME,EI.EMOB_NO FROM EMP E, EMP_INFO EI WHERE E.EID = EI.EID AND LOWER(E.ENAME)='john' ;
now consider about situation where we want to find employees those are having saving account and loan account in a bank.. here we want to find common tuples from two tables results. for this we will use set operations. [ intersection ]
for set operations 2 conditions MUST BE satisfied.
same no. of attributes must be fetches from each table.
domain of each attribute must be same of compatible to the higher one...
I have a view which was working fine when I was joining my main table:
LEFT OUTER JOIN OFFICE ON CLIENT.CASE_OFFICE = OFFICE.TABLE_CODE.
However I needed to add the following join:
LEFT OUTER JOIN OFFICE_MIS ON CLIENT.REFERRAL_OFFICE = OFFICE_MIS.TABLE_CODE
Although I added DISTINCT, I still get a "duplicate" row. I say "duplicate" because the second row has a different value.
However, if I change the LEFT OUTER to an INNER JOIN, I lose all the rows for the clients who have these "duplicate" rows.
What am I doing wrong? How can I remove these "duplicate" rows from my view?
Note:
This question is not applicable in this instance:
How can I remove duplicate rows?
DISTINCT won't help you if the rows have any columns that are different. Obviously, one of the tables you are joining to has multiple rows for a single row in another table. To get one row back, you have to eliminate the other multiple rows in the table you are joining to.
The easiest way to do this is to enhance your where clause or JOIN restriction to only join to the single record you would like. Usually this requires determining a rule which will always select the 'correct' entry from the other table.
Let us assume you have a simple problem such as this:
Person: Jane
Pets: Cat, Dog
If you create a simple join here, you would receive two records for Jane:
Jane|Cat
Jane|Dog
This is completely correct if the point of your view is to list all of the combinations of people and pets. However, if your view was instead supposed to list people with pets, or list people and display one of their pets, you hit the problem you have now. For this, you need a rule.
SELECT Person.Name, Pets.Name
FROM Person
LEFT JOIN Pets pets1 ON pets1.PersonID = Person.ID
WHERE 0 = (SELECT COUNT(pets2.ID)
FROM Pets pets2
WHERE pets2.PersonID = pets1.PersonID
AND pets2.ID < pets1.ID);
What this does is apply a rule to restrict the Pets record in the join to to the Pet with the lowest ID (first in the Pets table). The WHERE clause essentially says "where there are no pets belonging to the same person with a lower ID value).
This would yield a one record result:
Jane|Cat
The rule you'll need to apply to your view will depend on the data in the columns you have, and which of the 'multiple' records should be displayed in the column. However, that will wind up hiding some data, which may not be what you want. For example, the above rule hides the fact that Jane has a Dog. It makes it appear as if Jane only has a Cat, when this is not correct.
You may need to rethink the contents of your view, and what you are trying to accomplish with your view, if you are starting to filter out valid data.
So you added a left outer join that is matching two rows? OFFICE_MIS.TABLE_CODE is not unique in that table I presume? you need to restrict that join to only grab one row. It depends on which row you are looking for, but you can do something like this...
LEFT OUTER JOIN OFFICE_MIS ON
OFFICE_MIS.ID = /* whatever the primary key is? */
(select top 1 om2.ID
from OFFICE_MIS om2
where CLIENT.REFERRAL_OFFICE = om2.TABLE_CODE
order by om2.ID /* change the order to fit your needs */)
If the secondd row has one different value than it is not really duplicate and should be included.
Instead of using DISTINCT, you could use a GROUP BY.
Group by all the fields that you want to be returned as unique values.
Use MIN/MAX/AVG or any other function to give you one result for fields that could return multiple values.
Example:
SELECT Office.Field1, Client.Field1, MIN(Office.Field1), MIN(Client.Field2)
FROM YourQuery
GROUP BY Office.Field1, Client.Field1
You could try using Distinct Top 1 but as Hunter pointed out, if there is if even one column is different then it should either be included or if you don't care about or need the column you should probably remove it. Any other suggestions would probably require more specific info.
EDIT: When using Distinct Top 1 you need to have an appropriate group by statement. You would really be using the Top 1 part. The Distinct is in there because if there is a tie for Top 1 you'll get an error without having some way to avoid a tie. The two most common ways I've seen are adding Distinct to Top 1 or you could add a column to the query that is unique so that sql would have a way to choose which record to pick in what would otherwise be a tie.