Query for finding similar interests together - mysql

Name Place visited
Ash New york
Bob New york
Ash Chicago
Bob Chicago
Carl Chicago
Carl Detroit
Dan Detroit
Above is the sample table. The output should be two names who visited place together. I.e. the output should be Ash and Bob since the places visited by Ash also visited by Bob.
Output:
Name1 Name2
Ash Bob
What is a query for this using MySQL or even relational algebra?

The simplest method is to use group_concat(). Assuming no duplicates,
select places, group_concat(names) as names
from (select name, group_concat(place order by place) as places
from t
group by name
) t
group by places
having count(*) > 1;
This will return all the names with exactly the same places on a single row. The names will be in a comma-delimited list.

Related

Fetch fixed set of duplicate records from table

I want to fetch records from a table that contains duplicate records. I want the output to be like only two duplicate records from each set of duplicate records in overall record output set.
example-
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Monesy
Russia
Bhumi
India
Peter
England
Bruice
England
Radhe
India
Output should have only two duplicate set of records from all duplicate of similar type as we can see in output below the country is repeating only two times and it took only first two counters of duplicate records in final record set -
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Peter
England
You can number the lines by the window and select only the first N.
Sorting should be chosen according to the business logic of the query.
For example:
;WITH numbered_name AS
(
SELECT *
, ROW_NUMBER() OVER (PARTITION BY t.Country ORDER BY t.Name) rn
FROM table t
)
SELECT Name
, Country
FROM numbered_name
WHERE rn <= 2

How to get only one value from data base colum that has multiple same values? [duplicate]

This question already has an answer here:
How to output table results by using cfoutput group by date?
(1 answer)
Closed 7 years ago.
I'm working on outputting values from data base in the table. My table has 5 columns: Date, FirstName, LastName, City, State. Here is example of my data base table:
DateMeeting FirstName LastName City State
2015-12-11 Mike Johns Dallas TX
2015-12-11 John Cook Dallas TX
2015-12-11 Nick Roberts Dallas TX
2015-12-11 Oliver Ryan New York NY
2015-12-11 Michael Best New York NY
2015-12-11 David Holmes New York NY
So I want to have output table that will display just one date for multiple records. I tried to use DISTINCT on the date and that works fine but if I include my WHERE clause for City and State my query breaks. Also I tried to use GROUP BY but same problem, I can get Date values only once as long as I do not include other columns. In this case I need all columns but my Date value only once. Here is my query that I use:
Select Distinct(DateMeeting),FirstName, LastName, City, State
From Customers
Where City = 'Dallas'
and State = 'TX'
This query does not work with all columns that I have in my select, only if I run DISTINCT(DateMeeting). I would like to output my values in the table to look like this:
Date First Name Last Name City State
Mike Johns Dallas TX
John Cook Dallas TX
Nick Roberts Dallas TX
2015-12-11 Oliver Ryan New York NY
Michael Best New York NY
David Holmes New York NY
If anyone knows how this can be done please let me know. Thank you.
Each layer in the technology stack has its strengths and weaknesses.
As for mysql, do not turn it into a report engine as described with blank date columns except for one per date somewhere in the middle of a date chunk as shown. Subsequent dates as ordered will get muddled and confused.
True, one could use slightly interesting mysql variables and dump it just on the first row of a chunk. But for what.
Play to mysql's strengths, return all the data. And have the front-end (coldfusion or whatever), deal with the reporting features you desire for the output.

SQL SELECT FROM WHERE FirstName > "Maria"

I have the following table:
First Name
Bryce
Marcellin
Caroline
Kerry
Roberto
Mary
Carol
Warren
Bonnie
Terry
Louis
Michelle
Bobby
Tony
Vic
Frank
Roberto
Jose
Doug
Brian
William
Aiden
Davis
What exactly does SELECT FirstName FROM Members WHERE FirstName > "Maria"; search for ? in particular, the WHERE statement.
It returns the names:
Roberto, Mary, Warren, Terry, Michelle, Tony, Vic, Roberto and William
I thought it was searching for FirstName strings that are longer than 5 characters but this is not the case since Tony and Vic are also returned.
It searches for terms that are in alphabetical order AFTER "Maria."
For example, with "Jack, James, Jim" if you searched for SELECT name FROM table WHERE first_name >= 'James' you would receive the results of 'James' and 'Jim', since alphabetically those two are after one another. The reason Vic, Robert, William, etc are returned is because they are alphabetically after the value of "Maria"
Maria's string length is 5 characters, so use length function to find length of individual names and check it to be greater than 5 as below:
SELECT FirstName
FROM Members
WHERE length(FirstName) > 5;
Or if your name is going to be dynamic, you could use like:
SELECT FirstName
FROM Members
WHERE length(FirstName) > length("Maria");
If you need all names that comes after Maria and length greater than 5 then use:
SELECT FirstName
FROM Members
WHERE FirstName > 'Maria';
AND length(FirstName) > length('Maria');

mysql grouping distinct query

I have the following data in my database table, since I'm fairly new to MYSQL i'm having problems in querying it to give me the following output
City Subject
london english
toronto math
london math
london math
toronto english
toronto english
There can only be two subjects, english or math. Im trying to output the data this way, first the query should pick all the distinct items in the city column. Then tell me the count of each subject in that city.
output
city English Math
london 1 2
toronto 2 1
I tried grouping, but since I don't know mysql that well, I realized it just groups the subjects together and eats the cities while grouping.
try this:
SELECT city,
SUM(IF(subject='english',1,0)) AS English,
SUM(IF(subject='math',1,0)) AS Math
FROM foo
GROUP BY city;

Searching for data that can be in two different column (query/design)

Sorry if the title is not clear. I am a bit confused about how to plan my database schema as given my database design skill level the requirement falls under kind of advanced :) I could really use some help here. Anyway, here it goes ...
I need to track match details for teams. For the sake of simplicity, lets say I need to track the match date, result and the teams that played the match. Now, how do I design my tables so I can make sure all relevant data is returned without having to keep multiple records of the same match. I am not sure if I am explaining clearly, so here's an example below.
match_id team1 team2 result
________ ________ ________ ________
1 Arsenal Chelsea 5-3
2 Manchester Utd Arsenal 1-0
3 Liverpool Newcastle 2-0
4 Arsenal Everton 1-0
From this data, if I search for match_ids for matches played by Arsenal, I should get the below results,
1,2,4.
Now, in the basic designs which I know of, I would normally search for matched in team name for the team name supplied and return the result. But here the team name can be in two different columns and both can be relevant. So, is it something I need to decide on the design level or something that can be done with some sort of query.
(Note: Storing teams as home/away is not an option for my requirement).
You can just query both columns, it's not a problem:
select match_id
from matches
where team1 = 'Arsenal' or team2 = 'Arsenal';
(You could also normalize this schema by placing teams in their separate table and leaving only their ids in the matches table, but that doesn't change much, you still have to query both columns. Read about database normalization, any SQL book covers this).
If there are always two teams per match, then I think you did a good job here, and when querying for a particular team, you'll want to search for one column OR the other (SELECT match_id FROM matches WHERE team1 = "?" OR team2 = "?").
One note though: I would definitely split up the score into two columns:
match_id team1 team2 score1 score2
________ ______________ _________ ______ ______
1 Arsenal Chelsea 5 3
2 Manchester Utd Arsenal 1 0
3 Liverpool Newcastle 2 0
4 Arsenal Everton 1 0
This way you'll be able to query on scores later on, if you need it. (e.g. Big wins = SELECT match_id FROM matches WHERE ABS(score1 - score2) > 3;)
The other option you have should be more scalable if there exists a possibility of having more than two teams per match. If this is the case, then you'd likely want to remove the uniqueness constraint on match_id and cut out the team/score columns from 2 to 1:
match_id team score
________ ________ ____
1 Arsenal 5
1 Chelsea 3
2 Manchester Utd 1
2 Arsenal 0
3 Liverpool 2
3 Newcastle 0
4 Arsenal 1
4 Everton 1
And of course, you're definitely going to want to take Sergio's advice in putting all this stuff into separate tables. "Teams" are likely going to have different attributes (hometown, coach name, etc.), and you're not going to want to duplicate that data.
This will give you the results you want but there may be a better design too.
Select *
from table
where (team1 = 'ARSENAL' or Team2 = 'ARSENAL')
You may want to Separate out scores such as Team1Score Team2Score otherwise you can't easily do math with them.
for star I would not store the time name, I think its better if you store the times in other table and linq then thru an id.
And then you could create a table with columns id, match id and team id, and just search for the team id in that table!
you can used this query and its no problem for your program:
select YOUR_ID_FIELDS
from YOUR_TABLE_NAME
where YOUR_FIELD_NAME(team1) = 'Arsenal' or YOUR_FIELD_NAME(team2) = 'Arsenal';
and for exmale (Chelsea)
select YOUR_ID_FIELDS
from YOUR_TABLE_NAME
where YOUR_FIELD_NAME(team1) = 'Chelsea' or YOUR_FIELD_NAME(team2) = 'Chelsea';