Join on 3 tables not working properly - sql

I'm trying to get members according to business and subscriber id. But this is not working properly. Please help me to solve this.
SELECT distinct m.Fname,m.Created,m.[User_Name],b.Business_Name
FROM dbo.Members m,dbo.Business b,dbo.Assign_Business a
WHERE m.Subcriber_id=a.Subscriber_id
AND a.Business_id=b.id
AND b.Subcriber_id=a.Subscriber_id
AND a.Business_id='6' AND a.Subscriber_id='1'

Try This..
select distinct m.Fname,m.Created,m.[User_Name],b.Business_Name
from dbo.Members m
left join dbo.Assign_Business a on m.Subcriber_id=a.Subscriber_id
left join dbo.Business b on a.Business_id=b.id
where a.Business_id='6' and a.Subscriber_id='1' and b.Subcriber_id=a.Subscriber_id

First step, format your code cleanly. This will help greatly with debugging.
SELECT
distinct m.Fname,
m.Created,
m.[User_Name],
b.Business_Name
FROM
dbo.Members as M
inner join dbo.Assign_Business as A on M.Subscriber_Id = A.Subscriber_id
inner join dbo.Business as B on B.id = A.Business_id and B.Subscriber_id = A.Subscriber_id
WHERE
A.Business_ID = '6'
and A.Subscriber_id = '1'

It's hard to tell without seeing your tables' DDL but you can try
SELECT DISTINCT
m.Fname,m.Created,m.[User_Name],b.Business_Name
FROM dbo.Business b INNER JOIN
dbo.Assign_Business a ON a.Business_id=b.id AND
a.Subscriber_id=b.Subcriber_id INNER JOIN
dbo.Members m ON m.Subcriber_id=a.Subscriber_id
WHERE a.Business_id=6 AND
a.Subscriber_id=1

Try the following:
SELECT DISTINCT
m.Fname,
m.Created,
m.[User_Name],
b.Business_Name
FROM dbo.Members m
INNER JOIN dbo.Assign_Business a ON m.Subcriber_id=a.Subscriber_id
INNER JOIN dbo.Business b ON a.Business_id=b.id AND b.Subcriber_id=a.Subscriber_id
WHERE
a.Business_id='6' AND a.Subscriber_id='1'

Related

SQL many to many select people with multiple vacancies

I am working with sql server through SSMS right now. How can i choose all people with multiple(>2)vacancies?
I am trying something like that, but i dont understand how to make part with "more than 2 vacancies"?
SELECT dbo.applicants.FirstName, dbo.vacancy.Name
FROM dbo.applicants INNER JOIN
dbo.VacancyApplicant ON dbo.applicants.id = dbo.VacancyApplicant.ApplicantId INNER JOIN
dbo.vacancy ON dbo.VacancyApplicant.VacancyId = dbo.vacancy.id WHERE dbo.vacancy.Name='third vacancy'
SELECT dbo.applicants.FirstName, dbo.vacancy.Name
FROM dbo.applicants A INNER JOIN
dbo.VacancyApplicant V ON A.id = V.ApplicantId
WHERE EXIST(
SELECT 1
FROM dbo.applicants INNER JOIN
dbo.VacancyApplicant ON dbo.applicants.id =
dbo.VacancyApplicant.ApplicantId INNER JOIN
dbo.vacancy ON dbo.VacancyApplicant.VacancyId = dbo.vacancy.id
WHERE A.id=dbo.applicants.id
GROUP BY dbo.applicants.id,dbo.vacancy.id
HAVING COUNT(1)>2
)
Group By and Having are you basic answer. Below is a simple solution, might not be ideal, but can give you the idea.
I am finding target "applicants" ids in subquery, that uses GROUP BY and HAVING then outer query joins to that to output FirstName and LastName of applicant
SELECT dbo.applicants.FirstName, dbo.applicants.LastName FROM
dbo.applicants a INNER JOIN
(
SELECT dbo.applicants.id
FROM dbo.applicants INNER JOIN
dbo.VacancyApplicant ON dbo.applicants.id = dbo.VacancyApplicant.ApplicantId INNER JOIN
dbo.vacancy ON dbo.VacancyApplicant.VacancyId = dbo.vacancy.id AND dbo.vacancy.Name='third vacancy'
GROUP BY dbo.applications.id
HAVING COUNT(dbo.vacancy.id) > 2
) targetIds ON a.id = targetIds.id
"more than 2 vacancies"?
Your question only mentions vacancies but your query is filtering for a particular name. I assume you really want more than two of that name.
If I understand correctly, you want aggregation:
SELECT a.FirstName, a.Name
FROM dbo.applicants a INNER JOIN
dbo.VacancyApplicant va
ON a.id = va.ApplicantId INNER JOIN
dbo.vacancy v
ON va.VacancyId = v.id
WHERE v.Name = 'third vacancy'
GROUP BY a.FirstName, v.Name
HAVING COUNT(*) > 2;
Note the use of table aliases. They make the query easier to write and to read.
WITH TempCTE AS (
SELECT DISTINCT ap.FirstName
,vc.Name
,COUNT (va.VacancyId) OVER (PARTITION BY ap.id) AS NoOfVacancies
FROM dbo.applicants ap
JOIN dbo.VacancyApplicant va
ON ap.id = va.ApplicantId
JOIN dbo.vacancy vc
ON va.VacancyId = vc.id
)
SELECT FirstName,[Name], NoOfVacancies FROM TempCTE
WHERE NoOfVacancies > 2

How to create distinct count from queries with several tables

I am trying to create one single query that will give me a distinct count for both the ActivityID and the CommentID. My query in MS Access looks like this:
SELECT
tbl_Category.Category, Count(tbl_Activity.ActivityID) AS CountOfActivityID,
Count(tbl_Comments.CommentID) AS CountOfCommentID
FROM tbl_Category LEFT JOIN
(tbl_Activity LEFT JOIN tbl_Comments ON
tbl_Activity.ActivityID = tbl_Comments.ActivityID) ON
tbl_Category.CategoryID = tbl_Activity.CategoryID
WHERE
(((tbl_Activity.UnitID)=5) AND ((tbl_Comments.PeriodID)=1))
GROUP BY
tbl_Category.Category;
I know the answer must somehow include SELECT DISTINCT but am not able to get it to work. Do I need to create multiple subqueries?
This is really painful in MS Access. I think the following does what you want to do:
SELECT ac.Category, ac.num_activities, aco.num_comments
FROM (SELECT ca.category, COUNT(*) as num_activities
FROM (SELECT DISTINCT c.Category, a.ActivityID
FROM (tbl_Category as c INNER JOIN
tbl_Activity as a
ON c.CategoryID = a.CategoryID
) INNER JOIN
tbl_Comments as co
ON a.ActivityID = co.ActivityID
WHERE a.UnitID = 5 AND co.PeriodID = 1
) as caa
GROUP BY ca.category
) as ca LEFT JOIN
(SELECT c.Category, COUNT(*) as num_comments
FROM (SELECT DISTINCT c.Category, co.CommentId
FROM (tbl_Category as c INNER JOIN
tbl_Activity as a
ON c.CategoryID = a.CategoryID
) INNER JOIN
tbl_Comments as co
ON a.ActivityID = co.ActivityID
WHERE a.UnitID = 5 AND co.PeriodID = 1
) as aco
GROUP BY c.Category
) as aco
ON aco.CommentId = ac.CommentId
Note that your LEFT JOINs are superfluous because the WHERE clause turns them into INNER JOINs. This adjusts the logic for that purpose. The filtering is also very tricky, because it uses both tables, requiring that both subqueries have both JOINs.
You can use DISTINCT:
SELECT
tbl_Category.Category, Count(DISTINCT tbl_Activity.ActivityID) AS CountOfActivityID,
Count(DISTINCT tbl_Comments.CommentID) AS CountOfCommentID
FROM tbl_Category LEFT JOIN
(tbl_Activity LEFT JOIN tbl_Comments ON
tbl_Activity.ActivityID = tbl_Comments.ActivityID) ON
tbl_Category.CategoryID = tbl_Activity.CategoryID
WHERE
(((tbl_Activity.UnitID)=5) AND ((tbl_Comments.PeriodID)=1))
GROUP BY
tbl_Category.Category;

How to Fix Right Outer Join

I am Trying to make Left Outer Join Use bellow code;
The Result Using (LEFT OUTER , RIGHT OUTER, FULL OUTER And INNER JOIN) is The same Result.
I am Try to return All Publisher Name Include Those who don't connected with any Books yet!
SELECT P.PublisherName,
COUNT(B.BookID) AS BookPublished
FROM LR_Publisher AS P LEFT OUTER JOIN LR_Book As B
ON P.PublisherID = B.PublisherID
WHERE (P.PublisherID = #pPublisherID OR #pPublisherID IS NULL)
GROUP BY PublisherName
Thanks In Advance
Publishers that have no books published are filtered out by GROUP BY.
It can be fixed by using GROUP BY ALL:
SELECT P.PublisherName,
COUNT(B.BookID) AS BookPublished
FROM LR_Publisher AS P
LEFT OUTER JOIN LR_Book As B ON P.PublisherID = B.PublisherID
-- WHERE (P.PublisherID = #pPublisherID OR #pPublisherID IS NULL)
GROUP BY PublisherName
you could write it this way
select P.PublisherName,
isnull(bi.BookPublished, 0) as BookPublished
from LR_Publisher as P
left join (
select B.PublisherID, Count(B.BookID) BookPublished
from LR_Book as B
where (#pPublisherID is null or B.PublisherID = #pPublisherID)
group by B.PublisherID
) bi on P.PublisherID = Bi.PublisherID
where (#pPublisherID is null or P.PublisherID = #pPublisherID)
I also was not aware of the filtering done by group by. Thank you for the question.
simple left join will help you want all publisher name including who does not published any book yet
SELECT P.PublisherName
FROM LR_Publisher AS P LEFT OUTER JOIN LR_Book As B
ON P.PublisherID = B.PublisherID

How to select multiple many to many in relation with a single table

I'm currently working with database, but I've got stuck with a select query.
However, I'm not database expert.
The query should return the data from a table that has two relationships of many to many.
This is my tables Diagram that would shows the concept of my question
The Select Query should View three columns, which are VidTbl.Name, ActorTbl.Name and SubTitelTbl.name.
So, I've read and search in the Internet and I've given tries
First try
SELECT
VidTbl.NAME AS Video_Titel_Name,
ActorTbl.NAME AS Actor_Name
FROM ActorInVid
INNER JOIN VidTbl
ON VidTbl.Id = ActorInVid.FKVidId
INNER JOIN ActorTbl
ON ActorTbl.Id = ActorInVid.FKActorId
UNION all
SELECT
VidTbl.NAME AS Video_Titel_Name,
SubTitelTbl.NAME AS SubTitel_Langu
FROM SubTitelInVid
INNER JOIN VidTbl
ON VidTbl.Id = SubTitelInVid.FKVidId
INNER JOIN SubTitelTbl
ON SubTitelTbl.Id = SubTitelInVid.FKSTId
The Result I've got, it was wrong
Then I tried another way to solve this problem, but again I've got another error
second try
SELECT Temp1.*
From (SELECT VidTbl.Id AS Video_Id,
VidTbl.NAME AS Video_Titel_Name,
ActorTbl.NAME AS Actor_Name
FROM ActorInVid
INNER JOIN VidTbl
ON VidTbl.Id = ActorInVid.FKVidId
INNER JOIN ActorTbl
ON ActorTbl.Id = ActorInVid.FKActorId) AS Temp1
SELECT Temp2.*
FROM (SELECT VidTbl.Id AS Video_Id,
SubTitelTbl.NAME AS SubTitel_Langu
FROM SubTitelInVid
INNER JOIN VidTbl
ON VidTbl.Id = SubTitelInVid.FKVidId
INNER JOIN SubTitelTbl
ON SubTitelTbl.Id = SubTitelInVid.FKSTId) AS Temp2
SELECT *
FROM VidTbl
INNER JOIN Temp1
on Temp1.Video_Id = VidTbl.Id
INNER JOIN Temp2
on Temp2.Video_Id = VidTbl.Id
The error, I've got in the last select that was wrong
Thanks a lot for your help any ways
I wish that my question is clear and useful
Thanks again.
You are close. This should work...
SELECT
VidTbl.Name,
ActorTbl.Name,
SubTitelTbl.name
FROM VidTbl
INNER JOIN ActorInVid ON VidTbl.Id = ActorInVid.FKVidId
INNER JOIN ActorTbl ON ActorTbl.Id = ActorInVid.FKActorId
INNER JOIN SubTitelInVid ON VidTbl.Id = SubTitelInVid.FKVidId
INNER JOIN SubTitelTbl ON SubTitelTbl.Id = SubTitelInVid.FKSTId
SELECT DISTINCT vt.Name, at.Name, st.Name
FROM VidTbl vt
JOIN ActionInVid aiv ON aiv.VidId = vt.Id
JOIN SubtitleInVid siv ON siv.VidId = vt.Id
JOIN ActorTbl at ON at.Id = aiv.ActorId
JOIN SubTitleTbl st ON st.Id = siv.STId

INNER JOIN SQL QUERY

the next sql statement work fine. it shows both patient_id and serv_name, but i try to show patient_name instead of patient_id
SELECT C1.patient_id, S.serv_name
FROM
Checkup_Details C
INNER JOIN Services S ON (C.serv_id=S.serv_id),
Checkup C1
WHERE
C1.today = DATE() AND C1.check_id=C.check_id
ORDER BY C.check_id
so how am i suppose to do that by adding this sql statement
INNER JOIN Patient P ON (C1.patient_id=P.patient_id)
but i don't know how exactely.
Assuming the field patient_name is in Checkup_Details, you have to put
SELECT C1.patient_name ...
instead from
SELECT C1.patient_id ...
Simply add the INNER JOIN clause to get the columns of the table Patient and change C1.patient_id by P.patient_name.
SELECT P.patient_name, S.serv_name
FROM
Checkup_Details C
INNER JOIN Services S ON (C.serv_id=S.serv_id),
INNER JOIN Patient P ON (C1.patient_id=P.patient_id)
Checkup C1
WHERE
C1.today = DATE() AND C1.check_id=C.check_id
ORDER BY C.check_id
You should not mix implicit and explicit join syntax. A simple rule: never use commas in the from clause:
SELECT C1.patient_id, S.serv_name
FROM Checkup_Details C INNER JOIN
Services S
ON C.serv_id = S.serv_id INNER JOIN
Checkup C1
ON C1.check_id = C.check_id INNER JOIN
Patient P
ON C1.patient_id = P.patient_id
WHERE C1.today = DATE()
ORDER BY C.check_id;