Find duplication in multi tables - sql-server

We have 4 tables
Student, School, Location and StudentSchool
Students can can have same name but they are different persons
each student can be in one school only and each school located in one location
We found out that same student is somehow located in 2 different school
In this example "Adam Mike" with the Id '1' is located in 2 different schools in different locations.
How can i find a list of all students who are located in 2 different school?

You only need to search one table
select student_id
from school_student
group by student_id
having count(*) > 1

In the result, group students by name (group by in SQL), producing their count in the group (count(*) in SQL), then filter only those who have count > 1 (having count(*) > 1 in SQL).
I don't know how to express it in the query building tool, but it must support it.
Note that such grouping will lose IDs and school names; you will have to query for them again using the names.

Related

Simple database design - some columns have multiple values

Caveat: very new to database design/modeling, so bear with me :)
I'm trying to design a simple database that stores information about images in an archive. Along with file_name (which is one distinct string), I have fields like genre and starring where each field might contains multiple strings (if an image is associated with multiple genres, and/or if an image has multiple actors in it).
Right now the database is just a single table keyed on file_name, and the fields like starring and genre just have multiple comma-separated values stored. I can query it fine by using wildcards and like and in operators, but I'm wondering if there's a more elegant way to break out the data such that it is easier to use/query. For instance, I'd like to be able to find how many unique actors are represented in the archive, but I don't think that's possible with the current model.
I realize this is a pretty elementary question about data modeling, but any guidance anyone can provide or reading you can direct me to would be greatly appreciated!
Thanks!
You need to create extra tables in order to stick with the normalization. In your situation you need 4 extra tables to represent these n->m relations(2 extra would be enough if the relations were 1->n).
Tables:
image(id, file_name)
genre(id, name)
image_genres(image_id, genre_id)
stars(id, name, ...)
image_stars(image_id, star_id)
And some data in tables:
image table
id
file_name
1
/users/home/song/empire.png
2
/users/home/song/promiscuous.png
genre table
id
name
1
pop
2
blues
3
rock
image_genres table
image_id
genre_id
1
2
1
3
2
1
stars table
id
name
1
Jay-Z
2
Alicia Keys
3
Nelly Furtado
4
Timbaland
image_stars table
image_id
star_id
1
1
1
2
2
3
2
4
For unique actor count in database you can simply run the sql query below
SELECT COUNT(name) FROM stars

Counting common visited countries

This is a simplified post from another question.
Consider this :
How many visited countries in common does John and Mary have? Same question for John and alfred ? Same question for Alfred and Mary ?
Here is a google sheet to play : https://docs.google.com/spreadsheets/d/1jWAXVGt2_E3fYo8WZSBP1Fp-vg3gYPKlG2ZxC-4SE34/edit?usp=sharing
Try this:
=ArrayFormula(sum(countifs($A$2:$A$9,E$1,$B$2:$B$9,unique($B$2:$B$9))*countifs($A$2:$A$9,$D2,$B$2:$B$9,unique($B$2:$B$9))))
As far as I can see there are four correct answers to this question depending how you pose the question, rather like in SQL:
(1) for every instance of person 1 with a country, how many instances of person 2 are there with the same country including duplicates (like a cross join)
(2) for every unique combination of person 1 with a country, how many instances of person 2 with the same country are there (like a left join)
(3) for every unique combination of person 2 with a country, how many instances of person 1 with the same country are there (like a right join)
(4) for each unique combination of person 1 with a country, is there at least one instance of person 2 with the same country (like an inner join)
I have gone for option (1).
The other three formulas should be
=ArrayFormula(sum((countifs($A$2:$A$9,E$1,$B$2:$B$9,unique($B$2:$B$9))>0)*countifs($A$2:$A$9,$D2,$B$2:$B$9,unique($B$2:$B$9))))
=ArrayFormula(sum(countifs($A$2:$A$9,E$1,$B$2:$B$9,unique($B$2:$B$9))*(countifs($A$2:$A$9,$D2,$B$2:$B$9,unique($B$2:$B$9))>0)))
=ArrayFormula(sum((countifs($A$2:$A$9,E$1,$B$2:$B$9,unique($B$2:$B$9))>0)*(countifs($A$2:$A$9,$D2,$B$2:$B$9,unique($B$2:$B$9))>0)))

SalesForce SOQL highest number from size column

i'm new to SOQL and SF, so bear with me :)
I have Sales_Manager__c object tied to Rent__c via Master-Detail relationship. What i need is to get Manager with highest number of Rent deals for a current year. I figured out that the Rents__r.size column stores that info. The question is how can i gain access to the size column and retrieve highest number out of it?
Here is the picture of query i have
My SOQL code
SELECT (SELECT Id FROM Rents__r WHERE StartDate__c=THIS_YEAR) FROM Sales_Manager__c
Try other way around, by starting the query from Rents and then going "up". This will let you sort by the count and stop after say top 5 men (your way would give you list of all managers, some with rents, some without and then what, you'd have to manually go through the list and find the max).
SELECT Manager__c, Manager__r.Name, COUNT(Id)
FROM Rent__c
WHERE StartDate__c=THIS_YEAR
GROUP BY Manager__c, Manager__r.Name
ORDER BY COUNT(Id) DESC
LIMIT 10
(I'm not sure if Manager__c is the right name for your field, experiment a bit)

Crystal report - Group Towns by Countries

I have two tables
With this query i got the results
select countries.id, countries.name, Towns.id,Towns.TownName
from
Countries
left outer join Towns on countries.id = towns.countryID
Now i want to group results by country to get something like this on my CrystalReport. Is it even possible to make it ?
In the example you have provided, doing a GROUP BY in Sql Server would not be desirable. A Sql GROUP BY is generally used to project single scalar values from a list in each group, e.g.
SELECT Countries.Name, Count(Towns.Id) AS NumberOfTowns, Sum(Towns.Population) AS TotalPop
FROM Countries
LEFT OUTER JOIN Towns
ON Countries.Id = Towns.CountryID
GROUP BY Countries.Name;
Would give results like
Name NumberOfTowns TotalPop
Bosnia 2 123456
England 2 98765
by applying the Aggregates COUNT and SUM to all rows (Towns) in each country.
This isn't going to be useful for your report, as you need to show a list of all towns per country.
What you want instead is to use your current query as-is, and then to apply a Crystal Group Header on Countries.Name. Then, in Crystal, remove the Countries.Name field from the Details section (since you don't want the country repeated). You'll possibly also want some ordering done in the groups and data - again, I would suggest you do this in Crystal (although an ORDER BY Countries.Name, Towns.TownName would also work).
You'll then have a report which resembles your requirement.

How to write a query to see who called who the most in an Access database of phone calls?

I have a phone bill in Excel that shows all calls made to and from my phone and I imported it into a table in Access 2007. I want to learn to use Access to do a simple query to determine who I talk to the most.
Say we have Column A (caller) and Column B (person being called), and that my number will always be in either column. How do I make a query in Access to determine which phone number I talk the most with? I've got the Table with the Excel data in it, but I need some step-by-step handholding to learn how to do the query.
In simple english, I want to query all phone calls that contain my number in either column A or column B. Then, I want to count each unique pair (mynumber + othernumber or othernumber + mynumber should be counted under the same pair). Then, I want to count/summarize each unique pair to see which pair yields the highest count.
E.g. Go to Create ribbon, click Query Wizard, etc...
Thanks!
Lets say you have the following table :-
Column A : Column B
---------:----------
Fred : 1
Bill : 2
Fred : 1
You could do a query for example :-
SELECT A, B, Count(B) AS CountOfB
FROM Table1
GROUP BY A, B
ORDER BY Count(B) DESC
This would give you :-
Column A : Column B : CountOfB
---------:----------:----------
Fred : 1 : 2
Bill : 2 : 1
The first row would list the most common occurrences of column B and the count would list the number of times that row has been seen.

Resources