I currently have a pair of datasets. They are closely related with each other in that they share a common column called product.
So the first table would look like this (provider table)
Provider
Product
John
Apples
John
Bananas
Steve
Apples
Steve
Pears
The second table would look like this (buyer table)
Buyer
Product
Elton
Apples
Elton
Kiwis
Austin
Bananas
Austin
Melons
The relationship that I need is on how the products that every provider has, relates to the products that every buyer also owns. Or in a simpler manner, what are the products a provider could sell to a buyer and which are the ones he is missing, with a focus on what are the products the buyer requires.
For instance, the relationship of the current users in the table would tell me
John -> Elton = Apples(Could Sell), Kiwis (Missing)
John -> Austin = Bananas(Could Sell), Melons (Missing)
Steve -> Elton = Apples(Could Sell), Kiwis (Missing)
Steve -> Austin = Bananas(Missing), Melons (Missing)
The idea is to end un with a table that looks like this, and that contains every provider, along with every relationship to every buyer, ordered first by providers, and then by buyers.
Provider
Buyer
Selling product
Missing Product
John
Elton
Apples
Kiwis
John
Austin
Bananas
Melons
Steve
Elton
Apples
Kiwis
Steve
Austin
----
Bananas, Melons
I have used this type of queries to try to find the products that match but it is not working as intended
from (
select tss.product , buyer_table.buyer
from buyer_table
inner join provider_table tss
on buyer_table.product = tss.product
group by tss.provider , buyer_table.buyer
) t join buyer_table c on t.buyer= c.buyer
order by t.provider desc;
Additionally, I have not being able to find a way of finding the products that do not match and have it grouped by Provider and Buyer.
It is ok to build this use case by parts, for instance find the products that match and dont match first, and then join them together, or do it all in a single query.
What are the SQL queries that I need in order to create this type of functionality?
Related
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)))
I want to show the results of a MySQL query on my website using angularjs. For now, I'm showing them using a simple table with ng-repeat and it works with no problem. But because the data is a lot, I wanted to ask if it is possible to create multiple panels or tables per specific field.
To be more specific, I have 4 fields returned from the query: name, address, occupation, department. Right now I have a table such as:
George Smith Nikis 10 Project Manager Finance
Maria Bexley Lincoln 20 Project Manager Research
Chris Liggs Forks 123 Programmer Computer Science
etc. I want to know if I can create as many panels or tables as the unique values of the "occupation" field are and then show the results per that unique value inside each panel/table. So instead of the above table I would have something like:
Project Manager
George Smith Nikis 10 Finance
Maria Bexley Lincoln 20 Research
Programmer
Chris Liggs Forks 123 Computer Science
I think you need to use groupby filter
Check this fiddle by Darryl Rubarth, it contains the answer you need
http://jsfiddle.net/drubarth/R8YZh/
<div ng-repeat="item in MyList | orderBy:'groupfield' | groupBy:'groupfield'" >
You can use group by filter in ng-repeat with which you need to group
(revised) I have a web app where information will be entered for a user. First and last name as well as 3 Affiliations (primary, secondary, and tertiary) associated with the person. Each affiliation has 3 components (title, department, and university). So for example one record could be for:
User: Bob, Robertson
Affiliation1: Professor, Chemistry, U. Florida
Affiliation2: Director, Amazing Chemistry Institute, U. Florida
Affiliation3: Affiliated Faculty, BioChemistry, Florida Tech.
Also, Title and Department are text input fields but Univ. refers to a specific list of about 3000 university names 'univ_name' which is why it has it's own table. also affiliationOrdinal would be something like (1st, 2nd, 3rd)
Users Affiliation Univ.
======= ============ =========
id_user id_affiliation id_univ
FirstName id_user univ_name
LastName affiliationOrdinal
title
department
id_univ
Thanks Sean for your feedback, I started thinking of this more as a user with multiple addresess type of problem and that has been solved many times over it seems. I picked this one as a reference. Mysql database design for customer multiple addresses and default address. So the above should be a bit closer to workable right?
I will try to be as specific as possible, but I am having trouble conceptualizing the problem. As a hobby I am trying to design a NFL database that takes raw statistics and stores it for future evaluation for fantasy league analysis. One of the primary things I want to see is if certain players/teams perform well against specific teams and which defenses are suspect to either pass/run. The issue I am having is trying to design the schedule/event table. My current model is as follows.
TEAMS
TeamID, Team
SCHEDULE
ScheduleID, TeamID, OpponentID, Season, Week, Home_Away, PointsFor, PointsAgainst
In this scenario I will be duplicating every game, but when I use an event table where I use TeamAway and TeamHome I find my queries impossible to run since I have to query both AwayTeam and HomeTeam to find the event for a specific team.
In general though I cannot get a query to work where I have two relationships from a table back to one table, even in the schedule table my query does not work.
I have also considered dropping the team table and just storing NE, PIT, etc. for the Team and Opponent fields so I do not have to deal with the cross-relationships back to the team table.
How can I design this so I am not running queries for TeamID = OpponentID AND TeamID?
I am doing this in MS Access.
Edit
The issue I am having is when I query two table: Team (TeamID, Team) and Event(TeamHomeID, TeamAwayID), that had relationships built between the TeamID - TeamHomeID, and TeamID - TeamWayID I had issues building the query in ms Access.
The SQL would look something like:
SELECT Teams.ID, Teams.Team, Event.HomeTeam
FROM Teams INNER JOIN (Event INNER JOIN Result ON Event.ID = Result.EventID)
ON (Teams.ID = Result.LosingTeamID) AND (Teams.ID = Result.WinningTeamID)
AND (Teams.Team = Event.AwayTeam) AND (Teams.Team = Event.HomeTeam);
It was looking for teams that had IDs of both the losing team and the winning team (which does not exist).
I think I might have fixed this problem. I didn't realize the Relationships in database design are only default, and that within the Query builder I could change the joins on which a particular query is built. I discovered this by deleting all the AND portions of the SQL statement returned, and was able to return the name of all winnings teams.
This is an interesting concept - and good practice.
First off - it sounds like you need to narrow down exactly what kind of data you want so you know what to store. I mean, hell, what about storing the weather conditions?
I would keep Team, but I would also add City (because Teams could switch cities).
I would keep Games (Schedule) with columns GameID, HomeTeamID, AwayTeamID, ScheduleDate.
I would have another table Results with columns ResultID, GameID, WinningTeamID, LosingTeamID, Draw (Y/N).
Data could look like
TeamID | TeamName | City
------------------------
1 | PATS | NE
------------------------
2 | PACKERS | GB
GameID | HomeTeamID | AwayTeamID | ScheduleDate | Preseason
-----------------------------------------------------------
1 | 1 | 2 | 1/1/2016 | N
ResultID | GameID | WinningTeamID | LosingTeamID | Draw
------------------------------------------------------------
1 | 1 | 1 | 2 | N
Given that, you could pretty easily give any W/L/D for any Scheduled Game and date, you could easily SUM a Teams wins, their wins when they were home, away, during preseason or regular season, their wins against a particular team, etc.
I guess if you wanted to get really technical you could even create a Season table that stores SeasonID, StartDate, EndDate. This would just make sure you were 100% able to tell what games were played in which season (between what dates) and from there you could infer weather statistics, whether or not a player was out during that time frame, etc.
Does someone know how I can put more than one value in one field to make a relation between two different records?
Google translation (from German):
Using multivalued fields
In most systems, DBMS (database management systems), including earlier
versions of Microsoft Access, you can only store a single value in a
field. In Microsoft Office Access 2007, you can also create fields
that contain multiple values, such as a list of categories to which
you have assigned a condition. Multivalued fields are used in specific
situations, such as when you use Office Access 2007 to work saved in a
Windows SharePoint Services 3.0 list that contains a field of one of
the field types with multiple values that are available in Windows
data SharePoint Services.
This topic describes how to create and use multivalued fields in
Office Access 2007 and Windows SharePoint Services, how to create
multivalued fields and used, and how to use multivalued fields in a
query.
You can use multi-value Lookup fields in JOIN conditions (via their .Value property), but be aware that if there is such a field on both sides of the join then it will produce a match when any item matches on the joined fields, not when all items match. This may or may not be desirable, depending on the situation.
Case 1: Students with allergies
A school administrator needs to keep track of students with allergies and provide them with a list of meals that they should avoid when eating at the school cafeteria.
[Students]
ID Student Allergies
-- ------- ---------
1 Alice Eggs, Soy
2 Bradley Peanuts
3 Carol
4 Dennis Soy
[Meals]
ID Meal Allergens
-- ------------- ---------
1 Thai stir-fry Peanuts
2 Tofu omlette Eggs, Soy
3 Waffles Eggs
The query
SELECT Students.Student, Students.Allergies, Meals.Meal, Meals.Allergens
FROM Students INNER JOIN Meals ON Students.Allergies.Value = Meals.Allergens.Value;
returns
Student Allergies Meal Allergens
------- --------- ------------- ---------
Alice Eggs, Soy Tofu omlette Eggs, Soy
Alice Eggs, Soy Waffles Eggs
Bradley Peanuts Thai stir-fry Peanuts
Dennis Soy Tofu omlette Eggs, Soy
This is appropriate, since Alice should avoid meals that contain any ingredients to which she is allergic.
Case 2: Hotel requirements
[Travellers]
ID Traveller Requirements
-- --------- -------------------------
1 Gord free WiFi, in-room coffee
[Hotels]
ID Hotel Amenities
-- ------------ ----------------------------
1 Budget Motel free WiFi, in-room coffee
2 Fancy Hotel in-room coffee, room service
The query
SELECT Travellers.Traveller, Travellers.Requirements, Hotels.Hotel, Hotels.Amenities
FROM Hotels INNER JOIN Travellers ON Hotels.Amenities.Value = Travellers.Requirements.Value;
returns
Traveller Requirements Hotel Amenities
--------- ------------------------- ------------ ----------------------------
Gord free WiFi, in-room coffee Budget Motel free WiFi, in-room coffee
Gord free WiFi, in-room coffee Fancy Hotel in-room coffee, room service
The query returns both properties because they both offer in-room coffee. However, Fancy Hotel does not offer free WiFi, so I would prefer not to stay there. In this case the default join behaviour is not desirable (to me).