Inner join on two tables, with clause - sql

with one as
(Select sno = ROW_NUMBER()OVER (order by complaint_id), Complaint_Id, Complaint.ComplaintType_id, Complaint.complaintProfileId, Complaint.Description,
Complaint.Email, Complaint.PriorityLevel_id, Complaint.Date_Complained, Complaint.Status, Complaint.AdminComments, Complaint.Phone, Complaint.Evidence
from Complaints Complaint )
The result of this query is (not the entire result)
sno complaintProfileId
1 14
2 15
3 15
4 14
5 14
6 13
The second subquery:
two as
(SELECT Complaint.complaintProfileId,
CASE
WHEN MMB_Name IS NOT NULL THEN MMB_Name
WHEN UPPMembership.profile_id IS NOT NULL THEN 'UPP'
ELSE 'Not found'
END as Name
FROM Complaints Complaint
LEFT JOIN MMBMembership
ON MMBMembership.profile_id = Complaint.complaintProfileId
left JOIN MMB_BusinessProfiles mmbProfiles
ON mmbProfiles.MMB_id = MMBMembership.MMB_id
LEFT JOIN UPPMembership
ON UPPMembership.profile_id = Complaint.complaintProfileId)
complaintProfileId Name
14 UPP
15 Marlon
15 Marlon
14 UPP
14 UPP
13 Rodolfo
So this is where I am having trouble with
select one.*, two.Name
from one join two
on one.complaintProfileId = two.complaintProfileId
This query returns 36 records. id 14 is being returned 9times and 15-6times and so on..
I am doing a inner join but still not sure
Thanks
Sun

You need to join on a unique key. Each '14' on the left side is being joined to each of the three '14's on the right side. (3x3=9)

Related

Values from a table that are not in another one

I have two tables, A and B.
A
ID age
1 24
2 25
45 22
B
ID school
34 school1
1 school2
I want to select IDs that are in B but not in A.
I wrote
Select distinct bb.school
From B as bb
Left outer join A as aa
On bb.ID=aa.ID
inner join C as cc
On bb.school=cc.school
This code returns exactly the same number of rows that I would have with an inner join instead of left outer join.
Am I doing something wrong?
Try using not in;
Select * From A Where ID Not In ( Select ID From B )

full-join issue in SQL server

I have two tables total_sales_store :
store. total_sales
23 198750953.849999
29 77141561.3099999
9 77789512.9899997
15 89133935.9200002
3 57586980.0699998
32 166819624.16
26 143416610.79
12 144287538.149999
35 131520910.08
6 223756634.64
43 90565869.4100002
21 108118179.92
27 253856294.88
38 55159990.42
7 81598450.1399996
and storesdata:
store. type
1 A
2 A
3 B
4 A
5 B
6 A
7 B
8 A
9 B
10 B
11 A
I want to full join the two table so that I have this desired table:
store. Type. totalsales
1 A 123124
2 B 141221
3 C 134141
4 A. 234234
5 B 2323
6 C 123214
...
So I used the following query:
SELECT A.Store,a.total_sales
FROM totalsales_store A
FULL JOIN
(
SELECT [Type], Store
FROM storesdata
) B
ON A.Store = B.Store
ORDER BY total_sales DESC
But what I got is this:
[enter image description here][3]
which neglects the Type column. What can I do to make this work? Thank you!
With a FULL JOIN you will get all results from both tables, even when they don't match each other, so you should account for the NULL store values across tables. Otherwise simply include the Type column in the select.
I didn't see a need for a sub-query.
SELECT
ISNULL(A.Store, B.Store) Store,
B.Type,
a.total_sales
FROM
totalsales_store A
FULL JOIN storesdata B ON
A.Store = B.Store
ORDER BY
a.total_sales DESC
I suspect you can get away with
SELECT
A.Store,
B.Type,
a.total_sales
FROM
totalsales_store A
JOIN storesdata B ON
A.Store = B.Store
ORDER BY
a.total_sales DESC

combine two group by queries to one to get count of both table's record in sql server

I have two queries to get Tutor and Parent Counts by Locations, are below:
Select a.LocationId, COUNT(*) from TutorAddress ta
inner join Address a on a.Id=ta.AddressId group by a.LocationId
Out Put is:
LocationId Count
1 4
14 13
second query is:
Select a.LocationId, COUNT(*) from ParentAddress pa
inner join Address a on a.Id=pa.AddressId group by a.LocationId
Out Put is:
LocationId Count
1 6
14 3
12 8
I want to merge these two queries and get the result single output as below:
LocationId tutorCount ParentCount
1 4 6
14 13 3
12 0 8
is it possible, i m not able to get how i can achieve this. please help.
You can combine both query using a left join
select a.locationid,
count(pa.AddressId) as parentcount, COUNT(ta.AddressId) tutorcount
from Address a
left join ParentAddress pa on a.Id=pa.AddressId
left join TutorAddress ta on a.Id=ta.AddressId
group by a.locationid

cross joining two tables

I have a table that looks like this, lets call this table B.
id boardid schoolid subject cnt1 cnt2 cnt3 ....
=================================================================
1 20 21 f
2 20 21 r
3 20 21 w
4 20 21 m
5 20 30 r
6 20 30 w
7 20 30 m
Suppose the counts are just integers. Notice that there is no subject = f for schoolid = 30. Similarly, for most schools, some subject dosnt exist. You might have a schoolid that has just r, w or some that are just r, m, f..
So what I want to do is have 4 consistent rows for each school, and the row that dosnt exist I want dummy values. I thought about creating a secondary table
drop table #A
Select * into #A FROM
(
select [subject_s] = 'r', orderNo = 1
union all
select [subject_s] = 'w', orderNo = 2
union all
select [subject_s] = 'm', orderNo = 3
union all
select [subject_s] = 'f', orderNo = 4
) z
and doing some joins on them, but I've gotten NO where. I've tried inner join, left outer, cross join, everything. I've even tried to make cartesian product. I think my cartesian product messes up because I have orderno in there so it makes 16 rows per row in the main table. Actually typing this out, I realize if I remove the orderno, apply the cartesian product and then add orderno in later, it might work but I am interested to see what you guys can come up with. I am stumped.
End result
id boardid schoolid subject cnt1 cnt2 cnt3 ....
=================================================================
1 20 21 r
2 20 21 w
3 20 21 m
4 20 21 f
5 20 30 r
6 20 30 w
7 20 30 m
7 20 30 f
Try the following:
SELECT S.boardid, S.schoolid, A.[subject], B.cnt1, B.cnt2, B.cnt3
FROM (SELECT DISTINCT boardid, schoolid FROM YourTable) S
CROSS JOIN #A A
LEFT JOIN YourTable B
ON B.boardid = S.boardid AND B.schoolid = S.schoolid
AND A.[subject] = B.[subject]
Since I do not know which RDBMS you are using I tried the following with sqlite and a simpler table:
sqlite> create table schools (name varchar, subject varchar, teacher varchar);
sqlite> select * from schools;
School1|Maths|Mr Smith
School2|English|Jack
School3|English|Jimmy
School3|Maths|Jane
School4|Computer Science|Bob
sqlite> select
schoolnames.name,
subjects.subject,
ifnull(teachers.teacher, "Unknown")
from (select distinct name from schools) schoolnames
join (select distinct subject from schools) subjects
left join schools teachers
on schoolnames.name = teachers.name
and subjects.subject = teachers.subject;
School1|Maths|Mr Smith
School1|English|Unknown
School1|Computer Science|Unknown
School2|Maths|Unknown
School2|English|Jack
School2|Computer Science|Unknown
School3|Maths|Jane
School3|English|Jimmy
School3|Computer Science|Unknown
School4|Maths|Unknown
School4|English|Unknown
School4|Computer Science|Bob
I'd use:
SELECT
boardid, schoolid, dist_subject, id, cnt1, ...
FROM
(SELECT
boardid, schoolid, dist_subject
FROM
(SELECT
DISTINCT subject AS dist_subject
FROM b ) s full outer join
(SELECT
boardid, schoolid
FROM b
GROUP BY
boardid, schoolid ) g ) sg LEFT OUTER JOIN
b ON
sg.boardID = b.boardID AND
sg.schoolid = b.schoolID
sg.dist_subject = b.subject

Transposing rows to columns using self join

I have a table named category with values as below,
CategoryId | Value | Flag
1 25 a
2 26 a
3 27 a
1 28 m2 23 m
1 36 p2 33 p
Now I want to transpose the rows present in this table to columns based on the flag, something like
CategoryId | aValue | mValue | PValue
1 25 28 36
2 26 23 33
3 27 null null
I am trying to join based on the category id but I am just getting the matched records (inner join) in my resultset even if I use left outer join in my query.
My query:
SELECT
A.CategoryId,
A.Value AS actual,
B.Value AS projected,
C.Value AS Manual
FROM ((a AS A left JOIN b AS B ON A.categoryid=B.categoryid)
left JOIN c AS C ON A.categoryid=C.categoryid)
WHERE (((A.flag)="a") and ((B.flag)="p") and ((C.flag) ="m"))
I am getting the proper results if I have the data in 3 different tables.
I just want to check what would be the best way to transpose a rows to column when using self join...
Thanks,
Barani
Try this:
SELECT CategoryId,
MIN(SWITCH(YourTable.Flag = 'a',Value)) AS aValue,
MIN(SWITCH(YourTable.Flag = 'm',Value)) AS mValue,
MIN(SWITCH(YourTable.Flag = 'p',Value)) AS pValue
FROM YourTable
GROUP BY CategoryId