sql max of count 2 tables - sql

i've been searching for literally hours (1pm-11pm) for a solution to this SQL query I have to write. Basically, I have 2 tables and I have to select the ID from one table which has the maximum results in another. The second issue is there are 2 IDs. I can't quite explain what I mean because I'm that unsure but I can post my instructions and a link to the tables.
Any help will be greatly appreciated. I've also looked at a million other posts on SO and other places but even if it seems remotely relevant, I've no idea what changes to make to suit my needs.
i.e
SQL SELECT MAX COUNT
So my task is as follows:-
Display the name and the telephone number of private owners which have more properties than anybody else.
The top table in the image shows the "properties for rent" table and the lower shows the "private owners".
In reference to the question, I need to use the primary key of the private owners table to count the number of properties that each private owner has available to rent and then display the details of the private owner(s) who has the most properties available - which by studying the data, is 2 private owners (CO87 and CO93).
Again, I'd appreciate any help at all with this, I've been pulling my hair out for the best part of 12 hours :/
Thanks in advance guys,
Tim.
P.s - Just for the curious, this is one of an insane amount of SQL tasks for a university assignment =)
Edit:- The owner IDs are strings, not integers.

Your process should be:
Get a count of properties for each owner
Find the owner IDs with the max # of properties
Find the owners with those owner ID.
Seems like this should work:
SELECT * FROM Owners
WHERE OwnerID IN
(
SELECT OwnerID
FROM Properties
GROUP BY OwnerID
HAVING COUNT(*) =
(SELECT COUNT(*)
FROM Properties
GROUP BY OwnerID
ORDER BY COUNT(*) DESC
LIMIT 1)
)

Related

Looking for identical sequences over all users

I want to get the identical pathes (with counts) of all users
Hey everybody,
Want to keep the question short and hopefully it‘s clear what I want.
I have a table in BigQuery. There I have the following columns
- UserID
- Timestamp
- Domain
- some other columns (but I guess they are unimportant)
I have totally no idea how to fix this!
So I want to look for the same paths over all users and count how many users have the same sequence of domains.
Problem: We are talking about 129 000 users and around 5TB of data. I guess I have to limit the amount of path length or something else.
I‘m familiar with SQL but I need some help/input to keep the costs low. Every query costs money and my thought was to ask the community before I spend thousands of Dollars.
Thanks for any input!
EDIT:
I tried the following to rank the visits of domains:
SELECT
guid,
domain AS channel,
timestamp,
RANK() OVER (PARTITION BY guid ORDER BY timestamp ASC ) AS rank
FROM
data.all
My problem is now: How can I match identical pathes afters merge each "step" in this customer journey?
This may help or at least help you get started:
select domains, count(*)
from (select userid, string_agg(domain order by timestamp, ',') as domains
from t
group by userid
) u
group by domains;
I would prefer to use arrays to store the path itself, but BigQuery does not (yet) support arrays as GROUP BY keys.

Looking to display records from Table A that have more than one relationship to Table B

This sounds like a very simple query but I have never needed this calculation before. I'm using SQL Management Studio and SQL Server 2008.
I have a table ct_workers which contains individual employees and a second table cs_facilities which shows the sites that they work at.
The table ct_workers has a field person which is the primary ID for each employee and has a field facility which links the employees to cs_facilities via a field guid
I'm looking to display all workers that have 2 or more facilities.
I've though about using Excel or rownumber but surely that must be a simple efficient way of doing this?
Can anyone assist please?
Thanks,
You can use a GROUP BY with HAVING
SELECT cw.person
FROM ct_workers cw
GROUP BY cw.person
HAVING COUNT(DISTINCT cw.facility) >= 2
Your question suggests that you can use aggregation:
select w.person
from ct_workers w
group by w.person
having min(w.facility) <> max(w.facility); -- at least 2 values
However, if the person is the unique key in ct_workers, then a person can only be in one facility. So, your question would not make sense. You should actually have a junction table with one row per person and per facility.

MS Access SQL - Count for each record in a recordset

Good day all, hope you can help!
I have been toiling with this Query for a while now, and I am sure it will be relatively simple to fix!
I use the query to return all of the values from one table, along with a count of votes from one table, and a count of comments from another.
I have got it to work for loading an individual record
DCount('[query_id]','[comments]','[query_id]=" & Target & "')
However if I add something similar to the query that returns every query_id, the count shows the same for them all.
Is there a different function I can use than DCount to achieve this?
My previous issue was using count, and as the query had non unique data it was counting all votes from a person (i.e if I had made 6 votes, the count would show as 6 for any record my user id was attached to)
Happy to provide any further detail regarding the query.
You may be after something like:
DCount("*","[comments]","[person_id] = " & [Target] & "")
where [Target] is the field holding the PersonId of the other table.
I perhaps didn't ask the question in the best possible way, but I have managed to get the results I expected by using sub select statements in the SELECT clause
Just in case someone else doesn't know how to word or ask a question, Andre posted a very helpful comment to my original question above, advising the following link.
How to ask a good SQL question
SELECT issues.query_id, issues.query_raised_by, issues.query_raised_date, issues.query_summary, issues.query_status, issues.query_status_date, issues.query_detail, issues.query_response
(SELECT COUNT(*) FROM vote WHERE query_id = issues.query_id) AS voteCount,
(SELECT COUNT(*) FROM comments WHERE query_id = issues.query_id) AS commentCount

Count the amount of unique IP adresses per category using SQL

I'm teaching myself SQL and Access 2016 and now I am trying to do the following:
I have two tables, Table1 contains a list of URI's and categories and Table2 contain a list of URI's and IP's. Now I want to find out how many different IP's there are for each category in the list. Table 1's URI column is a subset of table 2's URI column.
Table 1:
URI | Categories
Table 2:
URI | IP
So obviously I should do an inner join on both tables with the URI which gets me:
URI | Categories | IP
Now I need to find out per category how many different IP's there are. So for example if the category is "Boats" I want to know how many different IP's looked for that category. I believe this can be done with COUNT, but that only works for 1 single category right? I want to figure out the amount of different IP's each category has within a huge dataset (millions of rows), so not just for 1 category.
So basically what I'm looking to get is an output like this:
Category | # of unique IP's
___________________________
Boats | 5
Insects | 22
This question might be a bit simple and honestly I've been thinking about it / googling it for a full day now because the question seemed too simple to ask here, but I'm just stuck now.
Thanks in advance for the help!
P.S. If you need any more examples or explanation on what I'm trying to do just tell me and I'll get it! :)
EDIT:
I forgot to use DISTINCT.. So I just added that but now I get a syntax error in Access 2016? Here is my SQL query:
SELECT Count(DISTINCT Final_parsed_userlogs_access.Field1) AS CountOfField1, Final_category_list_all_uri_access_edited.Field2
FROM Final_category_list_all_uri_access_edited INNER JOIN Final_parsed_userlogs_access ON Final_category_list_all_uri_access_edited.Field1 = Final_parsed_userlogs_access.Field4
GROUP BY Final_category_list_all_uri_access_edited.Field2;
Small final edit:
Apparently Access 2016 doesn't really work with Count distinct (Select Distinct in Access? Syntax error), but I don't understand how I should edit my own query to work with this fix?
Group it based on category, that should help!
Access-Engine does not support
SELECT count(DISTINCT....) FROM ...
You have to do it like this:
SELECT count(*)
FROM
(SELECT DISTINCT Name FROM table1)
Edit your query to:
SELECT Count(T.Field1) AS CountOfField1, T.Field2
FROM
(SELECT Final_parsed_userlogs_access.Field1, Final_category_list_all_uri_access_edited.Field2
FROM Final_category_list_all_uri_access_edited INNER JOIN Final_parsed_userlogs_access ON Final_category_list_all_uri_access_edited.Field1 = Final_parsed_userlogs_access.Field4
GROUP BY Final_category_list_all_uri_access_edited.Field2, Final_parsed_userlogs_access.Field1) as T
GROUP BY T.Field2;
Hope it helps.

Oracle SQL, not exist and count

I am currently stuck with my assignment. I am not trying to be lazy but I can't seems to find a solution and my teacher guided me by saying to use not exist or count.
I need to find patients name who have been treated by all the doctors. Currently I am just using plain intersect SQL command for each doctor name (In the question, there are only 3 doctors) but of course its not realistic when there are 100's of doctors name.
The scheme is as below.
Patient (PatientID, FamilyName, GivenName)
Account (ProviderNo, PatientID)
Doctor (ProviderNo, Name)
Any help would be greatly appreciated.
I won't provide you the exact Query.
But here is the psuedo code:
group PatientId in Account where its distinct ProviderNo count should be
equal to no. of Doctors