SQL count involving multiple tables - sql

I have two tables.University and UniversityType.Structure looks like this.
For University
universityId universityName universityTypeId universityTypeName
1 xyz 1 UGC
2 abc 1 UGC
3 lmn 2 NCT
4 pqr 3 AICT
For UniversityType
universityTypeId universityTypeName
1 UGC
2 NCT
3 AICT
The UniversityType table is fixed.Now I want to get the count of universityType that is UGC=2, AICT=1, NCT=1.Now I am writing something like this
SELECT universityId,universityTypeId,universityName FROM
University where universityTypeId=:universityTypeId";
But I am not getting correct result.Can anyone tell.I am new to SQL.

SELECT universityTypeId, COUNT(1)
FROM University where universityTypeId=:universityTypeId
OR
SELECT universityTypeId, COUNT(1)
FROM University
GROUP BY universityTypeId
OR
SELECT universityTypeId, (SELECT COUNT(1)
FROM University where universityTypeId = UniversityType.universityTypeId)
FROM UniversityType

Like this
SELECT universityId,universityName,count(universityTypeId) as total FROM
University group by universityId,universityName
You must add all columns list in the group by clause unless it used in the aggregate function

select universityTypeName, COUNT(University.universityId) FROM UniversityType JOIN University ON UniversityType.universityTypeId = University.universityTypeId GROUP BY UniversityType.universityTypeName
Try this query

You need to join the tables by UniversityType.universityTypeId and count the Universitys. So, try this:
SELECT t.universityTypeName, count(u.universityId) as total
FROM University u, UniversityType t
WHERE u.universityTypeId = t.universityTypeId
GROUP BY u.universityTypeName;
The output it gives is:
UGC|2
NCT|1
AICT|1

Related

Postgres, groupBy and count for table and relations at the same time

I have a table called 'users' that has the following structure:
id (PK)
campaign_id
createdAt
1
123
2022-07-14T10:30:01.967Z
2
1234
2022-07-14T10:30:01.967Z
3
123
2022-07-14T10:30:01.967Z
4
123
2022-07-14T10:30:01.967Z
At the same time I have a table that tracks clicks per user:
id (PK)
user_id(FK)
createdAt
1
1
2022-07-14T10:30:01.967Z
2
2
2022-07-14T10:30:01.967Z
3
2
2022-07-14T10:30:01.967Z
4
2
2022-07-14T10:30:01.967Z
Both of these table are up to millions of records... I need the most efficient query to group the data per campaign_id.
The result I am looking for would look like this:
campaign_id
total_users
total_clicks
123
3
1
1234
1
3
I unfortunately have no idea how to achieve this while minding performance and most important of it all I need to use WHERE or HAVING to limit the query in a certain time range by createdAt
Note, PostgreSQL is not my forte, nor is SQL. But, I'm learning spending some time on your question. Have a go with INNER JOIN after two seperate SELECT() statements:
SELECT * FROM
(
SELECT campaign_id, COUNT (t1."id(PK)") total_users FROM t1 GROUP BY campaign_id
) tbl1
INNER JOIN
(
SELECT campaign_id, COUNT (t2."user_id(FK)") total_clicks FROM t2 INNER JOIN t1 ON t1."id(PK)" = t2."user_id(FK)" GROUP BY campaign_id
) tbl2
USING(campaign_id)
See an online fiddle. I believe this is now also ready for a WHERE clause in both SELECT statements to filter by "createdAt". I'm pretty sure someone else will come up with something better.
Good luck.
Hope this will help you.
select u.campaign_id,
count(distinct u.id) users_count,
count(c.user_id) clicks_count
from
users u left join clicks c on u.id=c.user_id
group by 1;
See here query output

BigQuery - count the count of a column

Newbie on SQL and BigQuery in general. How to count the count of a column in BigQuery? As you can see from the code sample, the query returns the count of appName as WhitelistNames, but I would like to get a count of WhitelistNames.
SELECT
COUNT(appName) AS WhitelistNames,
bridgeToken
FROM (
SELECT
bridgeToken,
appName
FROM
[DB]
GROUP BY
bridgeToken,
appName )
GROUP BY
bridgeToken
ORDER BY
WhitelistNames DESC
Current query return is:
Row UniquebridgeToken WhitelistEntries
1 11111 5
2 22222 13
3 33333 3
4 44444 3
5 55555 3
But I would like to count the occurrence of UniquebridgeToken like below. Thanks in advance.:
Row WhitelistEntries BridgeCount
1 13 1
2 5 1
3 3 3
Below is for BigQuery Standard SQL and based on how I interpreted your question - which is:
for each bridgeToken how many unique appName's and how many total entries (rows) for that bridge
#standardSQL
SELECT
COUNT(DISTINCT appName) AS WhitelistNames,
COUNT(bridgeToken) AS BridgeCount
FROM `project.dataset.your_table`
GROUP BY bridgeToken
I understand that you want is to count how many UniquebridgeToken have the same number of WhitelistEntries. I think what you are looking for is that:
WITH nestedQuery AS (SELECT
appName,
COUNT(appName) as WhitelistEntries
FROM `project_name.dataset_name.table_name`
GROUP BY
price)
SELECT n.WhitelistEntries, COUNT(n.WhitelistEntries) as BridgeCount
FROM nestedQuery as n
GROUP BY n.WhitelistEntries
You can read about WITH clause here: https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#with_clause

How to get a correlated subquery as column

I dont know how I can do this sql query, probably its simple but I don't know how i can do it.
I have 2 tables:
Table_Articles:
COD NAME
1 Bottle
2 Car
3 Phone
Table_Articles_Registered
COD_ARTICLE DATE
1 05/11/2014
1 06/11/2014
1 07/11/2014
2 08/11/2014
2 09/11/2014
3 05/11/2014
I want take in the table Table_Articles_Registered the row with the MAX date , finally I want get this result:
COD NAME DATE
1 Bottle 07/11/2014
2 Car 09/11/2014
3 Phone 05/11/2014
I need use the sencente like this. The problem its in the subquery. Later I use other inner join in the sentence, this is only a fragment.
select
_Article.Code,
_Article.Description ,
from Tbl_Articles as _Article left join
(
select top 1 *
from ArticlesRegisterds where DATE_REGISTERED <= '18/11/2014'
order by DATE_REGISTERED
)
as regAux
on regAux.CODE_ARTICLE= _Article.CODE
I dont know how can I connect the field CODE_ARTICLE in the table ArticlesRegisterds with the first query.
I think this is a basic aggregation query with a join:
select a.cod, a.name, max(ar.date) as date
from Artiles a join
ArticlesRegisterds ar
on ar.cod_article = a.cod
group by a.cod, a.name
Try this:-
SELECT TAR.COD_ARTICLE, TA.NAME, MAX(TAR.DATE)
FROM Table_Articles_Registered TAR JOIN
Table_Articles.TA ON TAR.COD_ARTICLE = TA.COD
GROUP BY TAR.COD_ARTICLE, TA.NAME;
Can't you just do this?:
SELECT
Table_Articles.COD,
Table_Articles.NAME,
(
SELECT MAX(Table_Articles_Registered.DATE)
FROM Table_Articles_Registered
WHERE Table_Articles.COD_ARTICLE=Table_Articles.COD
) AS DATE
FROM
Table_Articles

write a query to identify discrepancy

I have a table with Student ID's and Student Names. There has been issues with assigning unique Student Id's to students and Hence I want to find the duplicates
Here is the sample Table:
Student ID Student Name
1 Jack
1 John
1 Bill
2 Amanda
2 Molly
3 Ron
4 Matt
5 James
6 Kathy
6 Will
Here I want a third column "Duplicate_Count" to display count of duplicate records.
For e.g. "Duplicate_Count" would display "3" for Student ID = 1 and so on. How can I do this?
Thanks in advance
Select StudentId, Count(*) DupCount
From Table
Group By StudentId
Having Count(*) > 1
Order By Count(*) desc,
Select
aa.StudentId, aa.StudentName, bb.DupCount
from
Table as aa
join
(
Select StudentId, Count(*) as DupCount from Table group by StudentId
) as bb
on aa.StudentId = bb.StudentId
The virtual table gives the count for each StudentId, this is joined back to the original table to add the count to each student record.
If you want to add a column to the table to hold dupcount, this query can be used in an update statement to update that column in the table
This should work:
update mytable
set duplicate_count = (select count(*) from mytable t where t.id = mytable.id)
UPDATE:
As mentioned by #HansUp, adding a new column with the duplicate count probably doesn't make sense, but that really depends on what the OP originally thought of using it for. I'm leaving the answer in case it is of help for someone else.

SQL in MS-Access: Using COUNT, JOIN and returning 0s

Apologies for posting this but although there are a few examples on the site, I just can't get mine to work.
So I have two tables as follows:
A Telephony table
ID | Name | GradeID
1 Richard 1
2 Allan 1
3 Peter
I also have a Grade table:
ID | Name
1 1
2 2
3 3
4 4
5 5
Anyway I'm trying to use COUNT() and LEFT JOIN to find out the number of times each grade is found in the Telephony table, including returning any which are 0, by using the following query:
SELECT telephony.GradeID, COUNT(*) AS Total
FROM telephony LEFT JOIN grade
ON telephony.GradeID = grade.ID
GROUP BY telephony.GradeID
ORDER BY 1;
This query returns all found but will not return all grades with 0 entries:
Grade | Total
1 2
Please help. I'm using Microsoft Access 2003.
Thanks for all your help. That's working great.
However, when I try to incorporate a DATE BETWEEN it returns only the grades found again.
Any ideas?
Thanks
I think this is what you are looking for:
SELECT grade.ID, Count(telephony.ID) AS CountOfID
FROM grade LEFT JOIN telephony ON grade.ID= telephony.GradeID
GROUP BY grade.ID
ORDER BY 1;
BTW: "Name" is a very bad name for a column since it is a reserved word.
As only Grade table contains all available IDs you should use grade.ID in your SELECT instead of telephony.GradeID. Try to execute this query:
SELECT grade.ID, COUNT(telephony.ID) FROM grade
LEFT JOIN telephony ON telephony.GradeID = grade.ID
GROUP BY grade.ID
ORDER BY 1;