I have to display all the customers who have been referred by a referrer with the same last name as the customer.
You can use a self-join as
select c1.customer#, c1.lastname, c1.city, c1.zip, c1.referred
from customers c1
join customers c2
on c1.customer# = c2.referred
and c1.lastname = c2.lastname;
customer# lastname city zip referred
--------- -------- ----------- ------ ---------
1003 SMITH TALLAHASSEE 32306 NULL
Rextester Demo
You can try this query
select cust.*, cust_ref.*
from customers cust,
referred cust_ref
where cust_ref.lastname = cust.lastname
Note : You can select field according your requirement.
I hope it will used.
Related
I need a sql command that do the following things:
For exemple,
PhonesTBL HistoryTBL
--------- --------
PhoneID UserID
PhoneType PhoneID
PhonesTBL HistoryTBL
--------- ----------
3,Samsung 100,3
4,Samsung 100,4
5,Apple 100,5
I need a sql command that gives me the most common PhoneType by any PhoneID in my Tables.
The query, on my example will reurn Samsung for userID 100.
becuase samsung is the most common Type.
Im using access and c# but i need the sql query to work.
Looking For Help, Ohad.
SELECT MAX(sum)
FROM (SELECT COUNT(PhoneType) as sum
FROM PhonesTBL
GROUP BY Quantity)
AS result;
Please try above query. Hope it will solve your problem.
Please try this query.
select distinct(h.userid), p.PhoneType
from PhonesTBL p
join HistoryTBL h on h.PhoneID = p.PhoneID
where p.phonetype in (select phonetype
from PhonesTBL
group by phonetype
having count(phonetype) = (SELECT MAX(c) FROM
(SELECT COUNT(phonetype) AS c
FROM PhonesTBL
GROUP BY phonetype) AS TBL1))
"select PhonesTBL.PhoneType
from PhonesTBL
Inner join HistoryTBL on PhonesTBL.PhoneID=HistoryTBL.PhoneID
where UserID={0}
group by PhoneType
order by count(*) desc
"
Did This, Worked for me.
Consider these 2 tables:
CustAddress
Name Address
---- -------
John 116 States Ave
Mike 404 16th Street
CustPhone
Name Phone
---- -------
John 342-345-456
Smith 678-435-567
How do I combine these into a single result like this in Oracle:
Customer info with Address and Phone:
Name Phone Address
---- ------- -------
John 342-345-456 116 States Ave
Smith 678-435-567
Mike 404 16th Street
Full Outer Join doesn't help as I end up losing some data.
Here is a SQL Fiddle to play with:
http://sqlfiddle.com/#!9/ebb9b/1
Using a full join.
select coalesce(ca.name,cp.name) as name, cp.phone, ca.address
from custaddress ca
full join custphone cp on ca.name=cp.name
Or using union all assuming there is at most one record per name in either of the tables.
select name,max(phone) as phone,max(address) as address
from (select name,phone,null as address from custphone
union all
select name,null,address from custaddress
) x
group by name
Kinda ugly but
select Names.Name, CustAddress.Address, CustPhone.Phone from
(select Name from CustAddress union select Name from CustPhone) as Names
left join CustAddress on Names.Name = CustAddress.Name
left join CustPhone on Names.Name = CustPhone.Name;
I am wondering how to use 2 instances of the same table in the following example , I know how to do it but I just cant make it work for my task.
I have the following tables :
Agency(id_agency,name)
Space(id_space,address)
Offer(id_agency,id_space)
Task: Find out the address,agency name 1 ,agency name 2 for spaces offered by two different agencies(the combination of 2 agencies is unique).
What I tried:
1)
SELECT ADDRESS,A.NAME,B.NAME
FROM AGENCY A
INNER JOIN OFFER O ON A.ID_AGENCY=O.ID_AGENCY
INNER JOIN SPACE S ON O.ID_SPACE=S.ID_SPACE
WHERE S.ID_SPACE=ANY(SELECT S.ID_SPACE FROM AGENCY B
INNER JOIN OFFER O ON B.ID_AGENCY=O.ID_AGENCY
INNER JOIN SPACE S ON O.ID_SPACE=S.ID_SPACE
);
2)
SELECT ADDRESS
FROM AGENCY A
INNER JOIN OFFER O ON A.ID_AGENCY=O.ID_AGENCY
INNER JOIN SPACE S ON O.ID_SPACE=S.ID_SPACE
INTERSECT
SELECT ADDRESS
FROM AGENCY B
INNER JOIN OFFER O ON B.ID_AGENCY=O.ID_AGENCY
INNER JOIN SPACE S ON O.ID_SPACE=S.ID_SPACE
WHERE A.ID_AGENCY<>B.ID_AGENCY;
In the second example I have no idea how to make it show A.name and b.name, since intersect won't work if I try to add them...
I tried to do this for the last 3 hours , sadly, I guess I can`t do it with the skills I have so far. :(
Thanks in advance
Edit 1: I hope you understand, thats how it should look.
Agency
id_agency name
---------- -------
1 Agency1
2 Agency2
3 Agency3
Space
id_space address
--------- --------
1 address1
2 address2
3 address3
Offer
id_agency id_space
----------- --------
1 1
2 1
3 2
Expected output:
Address Name1 Name2
----------- -------- -------
address1 Agency1 Agency2
To have the results in 1 row, each pair of agencies per row, as you asked for:
select S.address as address, A1.name as agency_1, A2.name as agency_2
from offer O1
join offer O2
on O2.id_space = O1.id_space
and O2.id_agency != O1.id_agency
join space S
on S.id_space = O1.id_space
join agency A1
on A1.id_agency = O1.id_agency
join agency A2
on A2.id_agency = O2.id_agency
;
The "core functionality" here is the join of offer no.1 (O1 alias) to offer no.2 (O2 alias) on equality of id_space but difference of id_agency.
An interesting exercise: To have the results in multiple rows, one agency per row:
select S.address, A.name as agency, X.number_of_agencies_per_space
from (
select id_space, id_agency, count(1) over (partition by id_space) as number_of_agencies_per_space
from offer
) X
join space S
on S.id_space = X.id_space
join agency A
on A.id_agency = X.id_agency
where X.number_of_agencies_per_space > 1
;
I am using Oracle SQL (TeraTerm),
and
I am trying to join specific information from two tables CONSULTANT, and PROJECT_CONSULTANT, and I need to retrieve only the employees who worked over 40 hours. Here are the tables
Project Consultant
PROJECT_ID CONSULTANT_ID NUMBER_HOURS
--------------- --------------- ------------
94738949 49620928 6
45699847 34879223 57
45699847 95928792 44
45699847 04875034 59
19870398 49620928 32
30495394 95928792 57
30495394 07811473 50
62388923 07811473 82
and Consultant
CONSULTA NAME ZIP START_DT
-------- -------------------------------- ----- ---------
CON_TITLE
-------------------------
49620928 Tom Jones 39875 01-SEP-98
Junior Consultant
04875034 Jack Johnson 29087 05-OCT-93
Manager
34879223 Lanny Harris 03944 30-APR-04
Principal
CONSULTA NAME ZIP START_DT
-------- -------------------------------- ----- ---------
CON_TITLE
-------------------------
95928792 Michael Johnson 02953 22-JUN-02
Senior Manager
07811473 Wendy Adams 29087 05-JUL-05
Senior Consultant
The code I came up with is
select Consultant_ID, Name, Zip, and Number_Hours
from Consultant
Inner Join project_consultant
ON Consultant.Consultant_ID=project_consultant.Consultant_ID
WHERE project_consultant.number_Hours>40;
I am getting an error
ERROR at line 1:
ORA-00936: missing expression
I just wanna know how to write the join statement correctly any help would be awesome, because I am having trouble knowing how to fix this join statement
You don't use and in the select clause:
select c.Consultant_ID, c.Name, c.Zip, pc.Number_Hours
from Consultant c Inner Join
project_consultant pc
on c.Consultant_ID = pc.Consultant_ID
where pc.number_Hours > 40;
You also need a table alias in the select clause to be clear what table Consultant_Id refers to.
EDIT:
You might actually want to sum the hours for employees. If so, you need an aggregation:
select c.Consultant_ID, c.Name, c.Zip, sum(pc.Number_Hours)
from Consultant c Inner Join
project_consultant pc
on c.Consultant_ID = pc.Consultant_ID
group by c.Consultant_ID, c.Name, c.Zip
having sum(pc.number_Hours) > 40;
You can't use and in Select Clause
Try this
SELECT C.Consultant_ID, C.Name, C.ip, PC.Number_Hours
FROM Consultant C
INNER Join project_consultant PC
ON C.Consultant_ID=PC.Consultant_ID
WHERE PC.number_Hours > 40;
select c.Consultant_ID, c.Name, c.Zip, p.Number_Hours
from Consultant c
Inner Join project_consultant p
ON c.Consultant_ID=p.Consultant_ID
WHERE p.number_Hours>40;
i have 2 tables, article and caretaker with following columns and structure
SQL> desc caretaker;
Name Null? Type
----------------------------------------- -------- ----------------------------
CID NOT NULL NUMBER(5)
CNAME VARCHAR2(15)
ADDRESS VARCHAR2(20)
SALARY NUMBER(10,2)
SQL> desc article;
Name Null? Type
----------------------------------------- -------- ----------------------------
ART_NO NOT NULL NUMBER(5)
ART_TITLE VARCHAR2(15)
TYPE VARCHAR2(15)
A_DATE DATE
CID NUMBER(5)
MUSEUM_ID NUMBER(5)
and i need to execute 2 queries,
1) find the details of the articles cared by person whose salary is more than 20000 and who takes care of atleast 2 articles
2)display the details of the caretaker taking care of maximum articles.
for the 1st query i have made it do far
select a.art_no,a.art_title,a.type,a.a_date from article a,caretaker c
where a.cid = c.cid and c.salary > 20000;
now i am confused about on how to extract the articles which are cared by person who takes care of atleast 2 articles?!!
2)for the second query,
select c.cid,c.cname,c.address,c.salary from caretaker c,article a
where c.cid=a.cid
and count( select a.cid from article a group by a.cid ) = MAX(a.cid)?????
am confused,please correct me,thank you
(I'm not supposed to JOIN commands)
For the first query:
select a.art_no,a.art_title,a.type,a.a_date
from article a,caretaker c
where a.cid = c.cid and c.salary > 20000
and c.cid in (select cid from article group by cid having count(cid) > 1)
SQL Fiddle: http://sqlfiddle.com/#!4/bef24/16
For the second query:
select cname, a.art_no,a.art_title,a.type,a.a_date
from article a,caretaker c
where a.cid = c.cid and c.cid = (select cid
from(select cid, count(cid)
from article
group by cid
having count(cid) = (select max(count(cid))
from article group by cid)));
SQL Fiddle: http://sqlfiddle.com/#!4/bef24/18