Join subquery with a count function - sql

I have four tables:
Room (holds room details)
property( holds property details)
property interest (linking table matching customers with properties
interested in)
buying_potential_customer (customer details).
I currently have this query which tells me how many rooms in each property has.
SELECT
property.property_id,
property.property_address_first_line,
COUNT(room.property_id) AS number_of_rooms
FROM room
INNER JOIN property
ON room.property_id = property.property_id
GROUP BY property.property_id,
property.property_address_first_line
ORDER BY property.property_id;
The property_interest and buying_potential interest need to be joined to this also.
This is the property interest table
This is the buying potential customer table
In the end I need, the data from property interest which matches property with buyer but with the buyer's first and last name attached, additionally counting the number of rooms in the property which is the query above. It seems I would need a subquery but am not sure how to complete this, and help would be appreciated
Sorry I am quite new to SQL.

If I understand your aim :)
You must write a query as main table your middle table property_interest, so you can join it with others parent tables, as follow:
SELECT bpc.buying_customer_first_name || bpc.buying_customer_surname,
(SELECT COUNT(*)
FROM room r
JOIN property p
ON r.property_id = p.property_id
WHERE p.property_id = pi.property_id)
FROM property_interest pi
JOIN buying_potential_custoter bpc
ON pi.buying_customer_id = bpc.buying_customer_id

Related

SQL INNER JOIN without linked column

I have an UltraGrid displaying customer information in it. The way the database is set up, there are 2 tables. Customers and Customer_Addresses. I need to be able to display all of the columns from Customers as well as Town and Region from Customer_Addresses, but I'm under the impression that I'd need Town and Region columns in the Customer table to be able to do this? I've never used an INNER JOIN before so I'm not sure if this is true or not, so can anybody give me pointers on how to do this, or if I need the matching columns or not?
Does it even require an INNER JOIN, or is there an alternative way to do this?
Below are the design views of both of the tables - Is it possible to display Add4 and Add5 from Customer_Addresses with all of Customers?
As long as you have another key column you can use to link the tables (ex. ID_Column), it is better that you use LEFT JOIN.
Example:
SELECT c.col1, ... , c.colN, a.town, a.region FROM Customers c
LEFT JOIN Customer_Addresses a ON a.ID_Column = c.ID_Column
In order to clarify how JOIN types work, look at this picture:
In our case, using a LEFT JOIN will take all information from the Customers table, along with any found matching (on ID) information from Customer_Addresses table.
First of all you need some column in common in two tables, all what you have to do is:
CREATE TABLE all_things
AS
SELECT * (or columns that you want to have in the new table)
FROM Costumers AS a1
INNER JOIN Customer_Addresses AS a2 ON a1.column_in_common = a2.column_in_common
The point is what kind of join do you want.
If you can continue the process without having information in table Costumers or in table Customer_Addresses maybe you need OUTER JOIN or other kind of JOIN.

List manipulating data from two tables content

I have these two tables
I would like to know how to do to list the product table: ProdID, Quantity, Name, Price
and table productUser: userId, State
The problem is that I need to also list all the information of product table and adding the UserId field with the same value and the state looks for a default value would be false ..
It is possible? could also, not to add the userId, State and drive it from my application code for assigning values​​. thanks
UPDATE:
If I understand your question correctly, you want to list specific fields from both tables but only when the child records match your criteria. If so, the query below would allow you to specify the userID and State.
SELECT p.prodId
,p.Quantity
,p.Name
,p.Price
,pu.userId
,pu.State
FROM Product p
INNER JOIN ProductUser pu
ON p.prodId = pu.prodId
WHERE pu.userID = #userID
AND pu.State = 0
--AND pu.State = #State
If my understanding is not correct, please post some sample table data and indicate which results you want returned.
Update to your update: I've defaulted State to zero in the query above. Followup question: do you want columns from both tables or are you trying to do an existence check against ProductUser to return just columns from Product?

SQL query multiple tables in database

I need to query 3 tables from a database.
The table Client includes client id no. and client name.
The table Property includes property id no. and property name amongst other things.
The table clientsInterestedInProperties includes client id no. property id no. and date of visit to property.
I want to list the name of clients who have an interest in a specific property (with the name, not id. no) and the date they visited the property.
For example say the property is called Barker Hall, who is interested in it and when did they visit?
Can anyone help?
First, use main filtering condition in your from table. So you must start with
select ... from Property ... ,
Then connect visitations to the property. It will generate different row for each visit, with
LEFT JOIN to get visitation to the property
Then we need to get readably client names, not IDs, so we do
LEFT JOIN to get client names
And some additional filtering/conditions can be added with
WHERE clauses ( or INNER JOIN ) , like visiting period.
Example SQL for your case ( i don't know your table/column names )
select PropertyName, ClientsInProp_VisitDate, ClientName
from Property
left join ClientsInProp on ClientsInProp_Propertyid = Property_PropertyID
left join Client on Client_Clientid = ClientsInProp_ClientId
where Proprty_Name = 'House 1' and ClientsInProp_VisitDate > '01.10.2013'
Cheers !
What you want is an IN clause:
SELECT * FROM Client
WHERE ClientId IN
(SELECT ClientID
FROM clientsInterestedInProperties cip
INNER JOIN Property p
ON cip.PropertyID = p.PropertyID
WHERE p.ProperyName = #propertyName)

Multiple table join query, with count in Oracle SQL

I'm trying to count the number of inspections of each property in YR_Inspection table by counting the number of times the property number occurs in the table, i then need to display this next to the town the city the property is located in and the branch the property belongs to all in the same query. Here is the link to my ERD to try and give this question some context,
http://www.gliffy.com/pubdoc/4239520/L.png
this is the code so far, it works so far but as soon as i add YR_Branch.CITY i get,
ORA-00979: not a GROUP BY expression
SELECT YR_Property.PROPERTYNUM, COUNT(YR_Inspection.PROPERTYNUM) AS Number_of_inspections
FROM YR_Property
INNER JOIN YR_Inspection
ON YR_Property.PROPERTYNUM = YR_Inspection.PROPERTYNUM
JOIN YR_Branch
ON YR_Property.BRANCHNUM = YR_Branch.BRANCHNUM
GROUP BY YR_Property.PROPERTYNUM
To add the branch number and the city for the branch, add max values for each to the query - like so:
SELECT YR_Property.PROPERTYNUM,
COUNT(YR_Inspection.PROPERTYNUM) AS Number_of_inspections,
MAX(YR_Branch.BRANCHNUM) AS Branch_Number,
MAX(YR_Branch.CITY) AS Branch_City
FROM YR_Property
INNER JOIN YR_Inspection
ON YR_Property.PROPERTYNUM = YR_Inspection.PROPERTYNUM
JOIN YR_Branch
ON YR_Property.BRANCHNUM = YR_Branch.BRANCHNUM
GROUP BY YR_Property.PROPERTYNUM

How can I compare two tables and delete on matching fields (not matching records)

Scenario: A sampling survey needs to be performed on membership of 20,000 individuals. Survey sample size is 3500 of the total 20000 members. All membership individuals are in table tblMember. Same survey was performed the previous year and members whom were surveyed are in tblSurvey08. Membership data can change over the year (e.g. new email address, etc.) but the MemberID data stays the same.
How do I remove the MemberID/records contained tblSurvey08 from tblMember to create a new table of potential members to be surveyed (lets call it tblPotentialSurvey09). Again the record for a individual member may not match from the different tables but the MemberID field will remain constant.
I am fairly new at this stuff but I seem to be having a problem Googling a solution - I could use the EXCEPT function but the records for the individuals members are not necessarily the same from one table to next - just the MemberID may be the same.
Thanks
SELECT
* (replace with column list)
FROM
member m
LEFT JOIN
tblSurvey08 s08
ON m.member_id = s08.member_id
WHERE
s08.member_id IS NULL
will give you only members not in the 08 survey. This join is more efficient than a NOT IN construct.
A new table is not such a great idea, since you are duplicating data. A view with the above query would be a better choice.
I apologize in advance if I didn't understand your question but I think this is what you're asking for. You can use the insert into statement.
insert into tblPotentialSurvey09
select your_criteria from tblMember where tblMember.MemberId not in (
select MemberId from tblSurvey08
)
First of all, I wouldn't create a new table just for selecting potential members. Instead, I would create a new true/false (1/0) field telling if they are eligible.
However, if you'd still want to copy data to the new table, here's how you can do it:
INSERT INTO tblSurvey00 (MemberID)
SELECT MemberID
FROM tblMember m
WHERE NOT EXISTS (SELECT 1 FROM tblSurvey09 s WHERE s.MemberID = m.MemberID)
If you just want to create a new field as I suggested, a similar query would do the job.
An outer join should do:
select m_09.MemberID
from tblMembers m_09 left outer join
tblSurvey08 m_08 on m_09.MemberID = m_08.MemberID
where
m_08.MemberID is null