Sorry for Title, don't know how to explain.
Ok so I want to see if any protocol (PTC_ID) is linked to an Audit (AUD_ID), in the picture you can see there is 3 tables and each one has a value of the other.
I though of using inner join all 3 tables with the ON , ON ADA_PTCID = PTC_ID etc. and if a audit is linked with a PTC then display year?
Select AUD_YEAR
From AUD_Table at
Inner Join ADA_TABLE ad
ON at.AUD_ID = ad.ADA_AUD_ID
Inner Join PTC_TABLE pt
ON pt.PTC_ID=ad.ADA_PTCID
try
select
ptc.ptc_name,
aud.aud_year
from
ptc_table ptc
inner join
ada_table ada
on
ada.ada_ptcid=ptc.ptc_id
inner join
aud_table aud
on
aud.aud_id=ada.ada_aud_id
Something like this?
select
aud.year,ptc.name
from ada
inner join aud on ada.aud_id = aud.aud_id
inner join ptc on ada.ptc_id = ptc.ptc_id
Related
I have the following DB structure:
And right now I can't make up a query to get
a creator data, admin data and tech data from item_contacts...
What kind of JOIN I need to use and how?
I think you want 3 joins on item_contacts - one for each column whose data you want to recover:
select
i.*,
cc.data as creator_data,
ca.data as admin_data,
ct.data as tech_data
from items i
inner join item_contacts cc on cc.contact_id = i.creator_id
inner join item_contacts ca on ca.contact_id = i.admin_id
inner join item_contacts ct on ct.contact_id = i.tech_id
how to create view of this query anyone help me please, i want create view of this but its show me error
Msg 4506, Level 16, State 1, Procedure ordersview, Line 3 Column names
in each view or function must be unique. Column name 'ID' in view or
function 'ordersview' is specified more than once.
CREATE VIEW ordersview
AS
Select * from UserClaimData cd
Inner join UserClaimDeductions ud on
ud.CLAIMID = cd.ID
Inner join UserClaimApproval ua on
ua.CLAIMID = cd.ID
inner join ClaimDataBreakdown cb on
cb.CLAIMID = cd.ID
inner join AppExpenseTypes ae on
ae.ID = cb.EXPENSETYPE
inner join AppNOWTypes an on
ae.ID = an.EXPENSETYPEID
inner join AppAreas aa on
aa.ID = cb.AREAID
inner join AppZones az on
cb.ZONEID = az.ID
inner join AppRegions ar on
ar.ID = cb.REGIONID
The answer to the question you've asked is to specifically reference elements from each table; for example:
CREATE VIEW ordersview
AS
Select cd.ID AS ID1, ua.ID as ID2, etc... from UserClaimData cd
Inner join UserClaimDeductions ud on
ud.CLAIMID = cd.ID
Inner join UserClaimApproval ua on
ua.CLAIMID = cd.ID
inner join ClaimDataBreakdown cb on
cb.CLAIMID = cd.ID
inner join AppExpenseTypes ae on
ae.ID = cb.EXPENSETYPE
inner join AppNOWTypes an on
ae.ID = an.EXPENSETYPEID
inner join AppAreas aa on
aa.ID = cb.AREAID
inner join AppZones az on
cb.ZONEID = az.ID
inner join AppRegions ar on
ar.ID = cb.REGIONID
I would, however, suggest that you don't put such a complex join inside a view. Consider the columns you want, and perhaps think about a stored procedure, or a table value function.
What part of the error message do you not understand?
You have select *, which brings together all columns from all tables. Just based on the join conditions, it is clear that most tables have an ID column, so there are multiple columns called ID. CLAIMID also seems quite popular.
In general, using select * is discouraged. However, it should not be used for views. A view should state the columns that it contains:
select cd.Id, . . .
Your view has more than one column with the same name, and this is causing the error.
AppRegions has a column called ID
AppAreas has a column called ID
UserClaimData has a column called ID
Change the Select * to specify the columns of the tables you want to have on the View table, and put a particularry name, or just don't put them.
You need to change the name of some columns in the view. Use AppAreas.ID as aaID
A view is like a virtual table, So you can't have the same name for 2(or more) columns.
So to avoid this, in your select Query instead of * provide the column names, and if there are 2 columns with the same name and you need them both in the view, give them different alias names.
Suppose you have ColumnA in TableA and TableB and you want them both in the view. Create view like this
CREATE VIEW vm_Sample
SELECT
A.COLUMNA COLUMNA_1,
B.COLUMNA COLUMNA_2
FROM TABLEA A INNER JOIN TABLE B
ON A.ID = B.ID
I have three tables in sql-server like table A table B table C.
How can I join 3 tables as expressed in the image below?
More information needed to give you a correct piece of code, but from the image you need LEFT JOINs.
(ID's have been presumed)
SELECT *
FROM Customers c
LEFT JOIN Items i ON c.iid = i.id
LEFT JOIN Sales s ON c.sid = s.id
It's never too late :)
Most probably you'll need this:
select ...
from customers
full outer join (items inner join sales on (xxx)) on (xxx)
I have a query (ContactFormTypesRequired) that returns ContactID and FormTypeID utilizing related tables that are not shown below. This is a list of FormTypes that each Contact should have related to it as a Form.
I need a query that returns Contacts that do not have one or more related forms of the FormTypes specified in the above query.
I've tried a left outer join from Form to ContactsFormTypesRequired on FormTypeID, but the results don't take into account FormTypes that each specific Contact should have.
Please let me know if you have any questions.
Thank you in advance for any suggestions.
I am writing the query this way. This first starts with your query to get needed forms as a CTE, then cross joins them to Contacts to get every needed combination, before left joining to the actual forms.
with NeededForms (<yourqueryhere>)
select distinct c.*
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
I'm doing it this way, so you can answer the query of what forms are missing with a very similar query:
with NeededForms (<yourqueryhere>)
select c.*, nf.FormTypeId
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
Try this simple query using NOT IN Cluase:
SELECT * FROM Contact
WHERE ContactID IN
(SELECT ContactID FROM ContactForm
INNER JOIN FORM ON ContactForm.FormID=Form.FormID
WHERE FormTypeID=#FormTypeID)
I created ContactFormTypeExist:
SELECT DISTINCT Contact.ContactID, Form.FormTypeID
FROM Contact
INNER JOIN ContactForm
ON Contact.ContactID = ContactForm.ContactID
INNER JOIN Form
ON ContactForm.FormID = Form.FormID
Then joined ContactFormTypesRequired described in the question above to ContactFormTypeExist with outer join to give me the ConactIDs that are missing related FormTypeIDs:
SELECT ContactFormTypesRequired.ConactID
FROM ContactFormTypeExist
RIGHT JOIN ContactFormTypesRequired
ON (ContactFormTypeExist.FormTypeID = ContactFormTypesRequired.FormTypeID)
AND (ContactFormTypeExist.ConactID = ContactFormTypesRequired.ConactID)
WHERE (((ContactFormTypeExist.ConactID) Is Null));
This returns all the ContactID for Contacts missing FormTypes required by their ContactType.
Hello in my system i have a search page for a student that the admin will be able to view
the students history i have a problem with showing the last name of his/her adviser which is lname_A. This is the code i currently us so far everything is ok except i cant manage to get the lname_a.
$qry_display = "SELECT
a.student_id, a.section_id, a.level, a.photo, a.address, a.father_occupation, a.father_phone, a.father_company, a.mother_occupation, a.mother_phone, a.mother_company,a.gpa,
b.fname, b.sex, b.lname, b.mname, b.birth_date, b.birth_place, b.address, b.father, b.father_degree, b.mother, b.mother_degree,
c.section_name, d.adviser_id , d.lname_a
FROM tbl_er AS a
LEFT OUTER JOIN tbl_enroll AS b ON a.student_id = b.student_id
LEFT OUTER JOIN tbl_section AS c ON a.section_id = c.section_id
LEFT OUTER JOIN tbl_adviser AS d ON a.section_id = d.adviser_id
WHERE a.student_id=".$id." AND a.level='Grade 2'";
Would gladly appreciate any help.
Are you sure you are joining both tables or correct columns?
a.section_id = d.adviser_id
If every student has adviser then you should use inner join rather than left outer join.
When you use left outer join there is chance lname_a is empty when the student doesn't has adviser.