This question already exists:
Use left join and inner join with more than 2 tables
Closed 4 months ago.
Here you can see I have three tables
Student table
Stud_id Name Br_id Email City_id
1001 Ankit 101 ankit#bmail.com 1
1002 Pranav 105 pranav#bmail.com 2
1003 Raj 102 raj#bmail.com 2
1004 Shyam 112 shyam#bmail.com 4
1005 Duke 112 duke#bmail.com 2
1006 Jhon 102 jhon#bmail.com 3
1007 Aman 101 aman#bmail.com 4
1008 Pavan 111 pavan#bmail.com 13
1009 Virat 112 virat#bmail.com 12
Branch Table
Br_id Br_name HOD Contact
101 CSE SH Rao 22345
102 MECH AP Sharma 28210
103 EXTC VK Reddy 34152
104 CHEM SK Mehta 45612
105 IT VL Shelke 22521
106 AI KH Verma 12332
107 PROD PG Kakde 90876
Address Table
City_id City Pincode
1 Mumbai 400121
2 Pune 450011
3 Lucknow 553001
4 Delhi 443221
5 Kolkata 213445
6 Chennai 345432
7 Nagpur 323451
8 Sri Nagar 321321
I am using here three tables first one is a student table and the second one is a Branch table and the third one is an Address table
So I am writing a query like this here you can see below
select [Name], Br_name, City
from student
inner join Branch on student.Br_id = Branch.Br_id
left join [Address] on student.City_id = [Address].City_id
I have three tables I want to show here the student's name and branch name city name, but I want to show the student who has their branch only. I also want to show the student who has their city as well as show a student who does not have any city.
I wrote the query above but here I am not getting the result. Here student's name who does not have any city what's wrong here in my above join SQL query?
Here as you can see I am getting this result which I do not want to show
Name Br_name City
Ankit CSE Mumbai
Pranav IT Pune
Raj MECH Pune
Jhon MECH Lucknow
Aman CSE Delhi
Why can I not get the students who do not have a city and who have a city? What's wrong here in my join query what I am missing here?
Please let me know what's wrong in my above join query what should I do to get proper result? I have given three tables above
You can see that and why I am not able to get the result what I want to show what is the wrong in the above my join query please let me know
I hope someone will help me. Thank you so much.
I have 2 ds as below
ds1:
CustId Name Street1 City
=================================
1 Ron 1 Mn strt Hyd
2 Ashok westend av Delhi
3 Rajesh 5th Cross Mumbai
4 Venki 2nd Main NY
ds2:
Id CustName CustAddr1 City
=========================================
11 Ron 1 Mn Street Hyd
12 Ron eastend avn Patna
13 Rajesh 2nd Main Mumbai
14 Girish 100ft rd BLR
15 Dinesh 60ft Mum
16 Rajesh 1st Cross Mumbai
I am trying to find an exact match like ds1.Name --> ds2.CustName, ds1.city --> ds2.city
Output:
GrpID Rec_Id Count ds1.cond Rec_Id Count ds2.cond
======================================================================
1 1 1 Ron + Hyd 1001 1 Ron + Hyd
2 2 1 Rajesh + Mumbai 1002 2 Rajesh + Mumbai
How to write (SPARK) SQL query for it?
I tried
final Dataset<Row> rslt = spark.sql("select * from ds1 JOIN ds2 ON ds1.Name==ds2.CustName");
(using only name)
but it gives output of mXn for m matching rows in ds1 with n matching rows in ds2.
My first work on this. Any suggestion?
Basically i'm trying to take an address field that is one column and separate it into three columns, the first having the address number, the second having the street name, and the third having if its an apt # or lot #
Address
-------------------
990 A street Apt 1B
127 B street Lot 3
So far i have this which works for the number and the street.
Select LEFT(MP.[ADDRESS],PATINDEX('%[0-9][^0-9]%', MP.[ADDRESS] )) AS Number,
LTRIM(RIGHT(MP.[ADDRESS], LEN(MP.[ADDRESS]) - PATINDEX('%[0-9][^0-9]%', MP.[ADDRESS] ))) As Street
From MPFILE mp
and it ends up like this:
Number | Street
----------------------------
990 | A street Apt 1B
127 | B Street Lot 3
Trying to get this though:
Number | Street | Apt/Lot
-----------------------------------
990 | A street | Apt 1B
127 | B street | Lot 3
Something like this will work with your example data given, but as Gordon says Addresses are hard to parse and probably should not be parsed in SQL.
WITH locations AS
(
SELECT ADDRESS,
LEN(ADDRESS) AS Len,
PATINDEX('%[0-9][^0-9]%', ADDRESS) AS NumLoc,
PATINDEX('%APT%', ADDRESS) AS APTLoc,
PATINDEX('%Lot%', ADDRESS) AS LotLoc
FROM MPFILE
), numbers AS
(
ADDRESS,
NumLoc,
Len - NumLoc AS StartAddr,
CASE WHEN APTLoc > 0 THEN AptLoc
WHEN LotLoc > 0 THEN LotLoc
ELSE Len AS EndAddr,
CASE WHEN APTLoc > 0 THEN AptLoc
WHEN LotLoc > 0 THEN LotLoc
ELSE Len AS StartApt,
Len AS EndApt
)
Select LEFT(NumLoc, MP.ADDRESS )) AS Number,
SUBSTRING(MP.ADDRESS, StartAddr, EndAddr),
SUBSTRING(MP.ADDRESS, StartApt, EndApt)
From numbers
Declare #t table ( Address varchar(200));
Insert Into #t (Address) values
('990 A street Apt 1B'),
('127 B street Lot 3'),
('127 B street Lot 345'),
('4444 Rice Street Suite 105'),
('715 South King Street #200'),
('101 Aupuni Street Room 342')
Select Left(Address,CHARINDEX(' ',Address)) as Number,
SUBSTRING(Address,CHARINDEX(' ',Address)+1, CHARINDEX('street',SUBSTRING(Address,CHARINDEX(' ',Address)+1,LEN(Address)))+LEN('street')) as Street
,Substring(Address,CHARINDEX('street',Address)+LEN('street'),LEN(Address)) as [Apt/Lot]
From #t
** Result**
Number |Street |Apt/Lot
----------------------------|-------
990 |A street |Apt 1B
127 |B street |Lot 3
127 |B street |Lot 345
4444 |Rice Street |Suite 105
715 |South King Street |#200
101 |Aupuni Street |Room 342
Let's say there is a table of medical records. Each visit has a unique ID but is made up of several rows corresponding to various codes/services rendered for the visit.
For example, there could be 3 rows with claimID "John" for each unique procedure code "123", "456", and "789"; 15 rows for "Jane" with codes; 6 rows for "David"...
ID Code
John 123
John 456
John 789
Jane 123
Jane 456
Jane 789
Jane 321
Jane 654
David 123
David 456
David 789
David 987
I have a list of 50 unique procedure codes and want to return the entire set of claim lines (i.e. all rows of "John") where any combination of these 50 codes have been billed with another, but not with themselves ("123" with "321", but not "123" with "123"). If "123" is in my list of 50 but "456" and "789" are not, it should not return the set of "John" claims since only one code of my 50 are present. I hope this makes sense.
Positive Result Codes
123
321
987
The query should return all 5 Jane rows (123 and 321) and all 4 David rows (123 & 987).
ID Code
Jane 123
Jane 456
Jane 789
Jane 321
Jane 654
David 123
David 456
David 789
David 987
Try this code:
;WITH Visits as (
SELECT claimID,COUNT(DISTINCT Code) as CNT FROM tbl_Visits
WHERE Code in (123,123,321,987)
GROUP by claimID
HAVING COUNT(DISTINCT Code) > 1
)
SELECT * FROM tbl_Visits
WHERE claimID in (SELECT claimID FROM Visits);
I have a query that returns a list of customers and their addresses.
ID FName LName Address1 City Postcode
--------------------------------------------------------
1 James Smith 1 Bank Street London W1C 1AA
2 Sarah Jones 45 Moor Ave London SW1 1YH
3 Mary Smith 1 Bank Street London W1C 1AA
4 Sean Baker 17 White Blvd London SE3 7TH
5 Bob Patel 58B Canal St London NW2 2TT
6 Seeta Patel 58B Canal St London NW2 2TT
7 David Hound 4 Main St London E11 8AB
I'm trying to produce another query from this data that selects a list of customers who are related/living together.The criteria for this would be the same Address 1 and Postcode fields.
My question is how I can produce a query that only selects records that have at least 1 other record with matching [Address1] and [Postcode]? ie; in the above example return only records 1, 3, 5 and 6.
Select * From
Customers c JOIN
(SELECT Address1, PostCode FROM Customer GROUP BY Address1, PostCode HAVING Count(1) > 1) c2
ON c.Address1 = c2.Address1 AND c.PostCode = c2.PostCode