I am stuck at the moment. I am creating a sports database in MS Access for our sports club and have the following question...
So what I have:
Tbl_Player (Player_ID, Firstname, Lastname, position, Min_Played, Goal, Assist, Yellow, Red)
Tbl_Player_Match (FK Tbl_Player_Player_ID, Min_Played, Goal, Assist, Yellow, Red).
What I need and strugling on is the following. When I add a match and data is being saved in tbl_Player_Match, I would need to have this saved in the "Summary" table which is Tbl_Player.
I cannot find the right syntax for this, should I create another temp table with the stats? Should I simply add an SQL code here?
Thanks a lot for your help.
Bob
I'd strongly suggest just using a query to pull the totals at run time:
SELECT ID
, Naam
, Achtenaam
, SUM(Goals) as Total
FROM Speler LEFT JOIN SpelerStats ON Speler.ID = SpelerStats.Speler
GROUP BY ID
, Naam
, Achtenaam
If you must add it to the table you'll have to use a domain aggregate function:
UPDATE Speler
SET Total = DSUM("Goals","SpelerStats","SpelerStats.Speler = " & Speler.ID)
I would tell you that you shouldn't store a summed value in tbl_Player. There comes only the characteristics of a Player for instance Name, Position and so on.
For the Output you create a query that would go something like this:
Select p.Player_ID, p.Firsname, p.Lastname, p.position, SUM(pm.Min_Played) as Sum_Min_Played
//, Count(pm.Tbl_Player_Match_ID) as Matches_Played
From
Tbl_Player p inner join Tbl_Player_Match pm on p.Player_ID = pm.Tbl_Player_Player_ID
group by p.Player_ID, p.Firsname, p.Lastname, p.position
The Matches_Played is just an addition, if you have sth. like an ID for the Matches Table.
Tell me if it helped you.
Related
Table 1: countries
Columns: code, country_name, full_name, continent_code.
countries table
Table 2: user_entries
Columns: active, recovered, death, createdAt
user_entries table
join tables to get countrywise data
query
select
country_name,
sum(active + recovered + death) as total,
sum(active) as active,
sum(recovered) as recovered,
sum(death) as death
from
user_entry
JOIN countries on user_entry.country_id = countries.code
group by
country_name;
output
join output
Goal
Write a query to get entries which were added in past 24 hours in a separate column, on the basis of user_entries.createdAt.
country_name
active
new_active
recovered
new_recovered
death
new_death
link to worldometers site
I am trying to achieve something similar to this.
So far I have tried using CASE but that didn't work. I also tried writing a subquery for this but was not able to figure out where to even get started.
Any help or guidance would be really appreciated.
Thanks.
I have two tables in a SQL Server Database:
Table: People
Columns: ID, FirstName, LastName
Table: StandardNames
Columns: Nickname, StandardName
Sample Nicknames would be Rick, Rich, Richie when StandardName is Richard.
I would like to find duplicate contacts in my People table but replace any of the nicknames with the standard name. IE: sometimes I have Rich Smith other times it is Richard Smith in the People table. Is this possible? I realize it might be multiple joins to the same table but can't figure out how to start.
Firstly, you need to determine how many duplicates you have in your People table...
SELECT p.FirstName, COUNT(*)
FROM People AS p
INNER JOIN StandardNames AS sn
ON CHARINDEX(sn.Nickname, p.FirstName) > 0 OR
CHARINDEX(sn.Nickname, p.LastName) > 0
GROUP BY p.FirstName
HAVING COUNT(*) > 1
That's just to get an idea of what data you're trying to find in relation to the Nicknames that may possibly exist inside (as a wildcard word search) the Firstname and Lastname columns.
If you are happy with the items found then expand on the query to update the values.
Let's say you wanted to change the Firstname to be the Standardname...
UPDATE p2
SET p2.FirstName = p2.Standardname
FROM
(SELECT p.ID, sn.StandardName
FROM People AS p
INNER JOIN StandardNames AS sn
ON CHARINDEX(sn.Nickname, p.FirstName) > 0 OR
CHARINDEX(sn.Nickname, p.LastName) > 0) AS a
INNER JOIN People AS p2 ON p2.ID = a.ID
So this will obviously find all the People IDs that have a match based on the query above, and it will update the People table by replacing the FirstName with the StandardName.
However, there are issues with this due to the limitation of your question.
the StandardNames table should have its own ID field. All tables should have an ID column as its primary table. That's just my view.
this is only going to work for data it matches using the CHARINDEX() function. What you really need is something to find based on a "sound" or similarity to the nicknames. Check out the SOUNDEX() function and apply your logic from there.
And this is assuming your IDs above are unique!
Good luck
You could standardize the names by joining, and count the number of occurrences. Extracting the ID is a bit fiddly, but also quite possible. I'd suggest the following - use a case expression to find the contact with the standard name, and if you don't have one, just take the id of the first duplicate:
SELECT COALESCE(MIN(CASE FirstName WHEN StandardName THEN id END), MIN(id)),
StandardName,
LastName,
COUNT(*)
FROM People p
LEFT JOIN StandardNames s ON FirstName = Nickname AND
GROUP BY StandardName, LastName
I have following table layout (all in one table SQL):
user_id, user_id_parent, fname, lname, shopname
I want to be able to print out fname and lastname and show what shop name the user is related to via (user_id_parent(is refering to user_id, you could say that user_id is a kind of shopname id) ). If it is a normal useracount in this case, the shopname is empty.
I would guess I should use som kind of Join, but i don't know how to use it when it's in the same table...
Result something like:
John Doe, Relating to: Shop #1
I did this working SQL and guess what, Tada! :-)
Posting this, so it may help other guys trying to do the same thing.
SELECT e.`user_type` AS usertype, e.`fname` AS employee, m.`shopname` AS associated_to
FROM rw_users e
INNER JOIN rw_users m ON e.`user_id_parent` = m.`user_id`
I am trying to use a second SELECT to get some ID, then use that ID in a second SELECT and I have no idea how.
SELECT Employee.Name
FROM Emplyee, Employment
WHERE x = Employment.DistributionID
(SELECT Distribution.DistributionID FROM Distribution
WHERE Distribution.Location = 'California') AS x
This post got long, but here is a short "tip"
While the syntax of my select is bad, the logic is not. I need that "x" somehow. Thus the second select is the most important. Then I have to use that "x" within the first select. I just don't know how
/Tip
This is the only thing I could imagine, I'm very new at Sql, I think I need a book before practicing, but now that I've started I'd like to finish my small program.
EDIT:
Ok I looked up joins, still don't get it
SELECT Employee.Name
FROM Emplyee, Employment
WHERE x = Employment.DistributionID
LEFT JOIN Distribution ON
(SELECT Distribution.DistributionID FROM Distribution
WHERE Distribution.Location = 'California') AS x
Get error msg at AS and Left
I use name to find ID from upper red, I use the ID I find FROM upper red in lower table. Then I match the ID I find with Green. I use Green ID to find corresponding Name
I have California as output data from C#. I want to use California to find the DistributionID. I use the DistributionID to find the EmployeeID. I use EmployeeID to find Name
My logic:
Parameter: Distribution.Name (from C#)
Find DistributionID that has Distribution.Name
Look in Employment WHERE given DistributionID
reveals Employees that I am looking for (BY ID)
Use that ID to find Name
return Name
Tables:
NOTE: In this example picture the Employee repeats because of the select, they are in fact singular
In "Locatie" (middle table) is Location, I get location (again) from C#, I use California as an example. I need to find the ID first and foremost!
Sory they are not in english, but here are the create tables:
Try this:
SELECT angajati.Nume
FROM angajati
JOIN angajari ON angajati.AngajatID = angajari.AngajatID
JOIN distribuire ON angajari.distribuireid = distribuire.distribuireid
WHERE distribuire.locatie = 'california'
As you have a table mapping employees to their distribution locations, you just need to join that one in the middle to create the mapping. You can use variables if you like for the WHERE clause so that you can call this as a stored procedure or whatever you need from the output of your C# code.
Try this solution:
DECLARE #pLocatie VARCHAR(40)='Alba'; -- p=parameter
SELECT a.AngajatID, a.Nume
FROM Angajati a
JOIN Angajari j ON a.AngajatID=j.AngajatID
JOIN Distribuire d ON j.DistribuireID=d.DistribuireID
WHERE d.Locatie=#pLocatie
You should add an unique key on Angajari table (Employment) thus:
ALTER TABLE Angajari
ADD CONSTRAINT IUN_Angajari_AngajatID_DistribuireID UNIQUE (AngajatUD, DistribuireID);
This will prevent duplicated (AngajatID, DistribuireID).
I don't know how you are connecting Emplyee(sic?) and Employment, but you want to use a join to connect two tables and in the join specify how the tables are related. Joins usually look best when they have aliases so you don't have to repeat the entire table name. The following query will get you all the information from both Employment and Distribution tables where the distribution location is equal to california. You can join employee to employment to get name as well.
SELECT *
FROM Employment e
JOIN Distribution d on d.DistributionID = e.DistributionID
WHERE d.Location = 'California'
This will return the contents of both tables. To select particular records use the alias.[Col_Name] separated by a comma in the select statement, like d.DistributionID to return the DistributionID from the Distribution Table
Need some help with an homework assignment on SQL
Problem
Find out who (first name and last name) has played the most games in the chess tournament with an ID = 41
Background information
I got a table called Games, which contains information...
game ID
tournament ID
start_time
end_time
white_pieces_player_id
black_pieces_player_id
white_result
black_result
...about all the separate chess games that have taken place in three different tournaments ....
(tournaments having ID's of 41,42 and 47)
...and the first and last names of the players are stored in a table called People....
person ID (same ID which comes up in the table 'Games' as white_pieces_player_id and
black_pieces_player_id)
first_name
last_name
...how to make a SELECT statement in SQL that would give me the answer?
sounds like you need to limit by tournamentID in your where clause, join with the people table on white_pieces_player_id and black_pieces_player_id, and use the max function on the count of white_result = win union black_result = win.
interesting problem.
what do you have so far?
hmm... responding to your comment
SELECT isik.eesnimi
FROM partii JOIN isik ON partii.valge=isik.id
WHERE turniir='41'
group by isik.eesnimi
having count(*)>4
consider using the max() function instead of the having count(*)> number
you can add the last name to the select clause if you also add it to the group by clause
sry, I only speak American. What language is this code in?
I would aggregate a join to that table to a derived table like this:
SELECT a.last_name, a.first_name, CNT(b.gamecount) totalcount
FROM players a
JOIN (select cnt(*) gamecount, a.playerid
FROM games
WHERE a.tournamentid = 47
AND (white_player_id = a.playerid OR black_player_id = a.playerid)
GROUP BY playerid
) b
ON b.playerid = a.playerid
GROUP BY last_name, first_name
ORDER BY totalcount
something like this so that you are getting both counts for their black/white play and then joining and aggregating on that.
Then, if you only want the top one, just select the TOP 1