I have rating system which people can rate on ideas. The user can get a summary from the SQLite database of ideas he hasn't voted for yet. My problem is that the database still gets the ideas he already voted for. My Table where the query has to operate looks like this:
ID (pk), ratingScore, userID, ideaID
1 3 1 1
2 4 2 1
3 5 2 2
Ratings score is the score the user has given the idea, userID is the ID of the user who voted and ideaID is the ID of the idea.
How can I get the ideas from user 1 where he hasn't voted for without getting the other ideas, so the result of the query on this table would have been only 'ideaID 2'.
SELECT Ratings.ideaID
FROM Ratings
WHERE (NOT(Ratings.userID)=1)
GROUP BY ideaID;
This is what I tried, but it didn't work, I also tried DISTINCT but that doesn't work either.
Assuming that you have an Ideas table, you could use a correlated subquery like this to find ideas for which no rating for that user exists:
SELECT ID
FROM Ideas
WHERE NOT EXISTS (SELECT 1
FROM Ratings
WHERE userID = 1
AND ideaID = Ideas.ID)
(Reading the ideas from the Ratings table would fail if there is an idea that has not been rated by any user.)
You need to get a list of all ideas and left join it to your table, like this:
SELECT ideas.ideaID
FROM (SELECT DISTINCT ideaID FROM Ratings) ideas LEFT JOIN
Ratings r ON r.ideaID = ideas.ideaID AND r.userID=1
WHERE r.ideaID IS NULL;
The example above will give you a list of unvoted ideas for a single user.
Related
I am using SQL Server. I have spent a lot of time on this and cannot figure it out.
I created a database for this, which shows which athlete another athlete has voted to win:
Here is my athlete table:
Here is my team table:
I have a separate table for team, and athlete. (Athlete includes votesForId)
I need a query that is specifically for this self-join table.
I am trying to figure out a query to show which athletes did not receive any votes by the other athletes. I have tried quite a few things and to be honest at this point I have no idea which way this is supposed to go. Thank you in advance!
EDIT:
If anyone needs this answer in the future, this was my completed answer I figured out (I added concat for my own purposes):
SELECT CONCAT(voter.athleteFirstName, ' ', voter.athleteLastName) AS [Did Not Recieve Any Votes]
FROM athlete AS voter LEFT JOIN athlete AS recipent ON voter.athleteID = recipent.votesforId
WHERE recipent.athleteId IS NULL
ORDER BY voter.athleteFirstName
Presumably you have a table of athletes and a separate one for votes. Then you would use:
select a.*
from athletes a
where not exists (select 1
from votes v
where v.votesForId = a.athleteId
);
Of course, this could be one table, but it would make more sense to me if there were two.
You want to get athletes that do not have their id in the votesForId column.
SELECT *
FROM athlete
WHERE athleteId NOT IN (SELECT DISTINCT votesForId FROM athlete)
If anyone needs this answer in the future, this was my completed answer I figured out (I added concat for my own purposes):
SELECT CONCAT(voter.athleteFirstName, ' ', voter.athleteLastName) AS [Did Not
Recieve Any Votes]
FROM athlete AS voter LEFT JOIN athlete AS recipent ON voter.athleteID =
recipent.votesforId
WHERE recipent.athleteId IS NULL
ORDER BY voter.athleteFirstName
To be blunt I don't know SQL however I don't want the answer, I want to work it out myself.
Here's the question:
Write a SQL query to calculate the number of goals for each team.
players
id name team_id goals
1 Joel 1 3
2 Ed 2 1
3 Simon 2 4
teams
id name
1 New Zealand
2 London
What I'm asking for is an arrow to information that will allow me to solve the question.
I've tried looking myself but I don't even know the correct terminology to ask the question, googling 'write sql to add fields for each row' just seems to return about adding columns or inserting.
You need to first try to JOIN your tables(id in Teams will be linked to TeamId in Players.) based on the foreign key columns.
Then you need to do the GROUP BY and use the aggregate function SUM to get the goals for each team.
So your query will be like:
select t.name, sum(p.goals) as cnt,
from players p inner join teams t on p.teamid = t.id
group by t.name
First you have to group players by teams : use t1.id=t2.id to join values in the tables, and then group theme by "BROUP BY" t.name.
Then : user "SUM(value)" function who sum values .
select teams.name,sum(players.goals) from players,team where player.team_id=teams.id group by teams.name;
what i want to achieve is this
i got 2 table table 1 = users table 2 = rooms
what i try to do is to get a list of all room but at same time get how many user that are in that specific room.
table rooms contain
id | name | description | limit
table users contain
roomid | username | password
at the end i want to display something like this
room 1 | description | numberofuser / limit
Thanks for any help
currently what i am using to do this is i repeat a query inside a while but if i have 100 rooms then it will make 100 sql query everytime i display the list
I replicated your database and this query works.
SELECT room.id, room.name,
(SELECT COUNT(*) FROM user WHERE room.id=user.roomid)
AS "Occupancy"
FROM room;
Note: My standard is to use singular names for tables (user rather than users) and I called my key to room in the user table"roomid", but you can use yours of course.
Here is the result:
id Name Occupancy
1 PentHouse 4
2 basement 5
3 MeetingRoom 0
You can add limit to the field list. I didn't replicate that.
First get the count using GROUP BY and then perform a JOIN
select r.name,
r.description,
xx.numberofuser_perRoom
from rooms r
join (select roomid, count(*) as numberofuser_perRoom
from users
group by roomid) xx on r.id = xx.roomid;
You didnt specify which DB is that for, nor whether you want to see rows for empty rooms (i assumed yes), so it could look something like this:
SELECT r.name, r.description, COUNT(u.username) * 100. / r.limit
FROM rooms r
LEFT JOIN users u ON u.roomid = r.id
GROUP BY r.name, r.description, r.limit
I use Oracle DB .I have for example 3 tables:
User (id_user,username)
Interest (id_interest,name)
UserInterest(id_user_interest,id_user,id_interest)
I'd like to get all users, together with name of interest, which has the highest id_user_interest in UserInterest table. What is the best way to get it in sql query?
Thanks for the help
This should work to get the user with the highest user interest hopefully...
I do not have access to a compiler so this is all a guess.
SELECT TOP 1 U.username, I.name FROM user U
JOIN user_interest UI ON U.id_user=UI.id_user
JOIN interest I ON UI.id_interest=I.id_interest
GROUP BY U.username
ORDER BY UI.id_user_interest ASC
The TOP attribute grabs the first x amount of results and the ORDER BY blank ASC attribute orders the results from highest to lowest.
Hopefully this works for you, and GL programming. :)
Let me explain what I mean with that question:
Lets say I have to tables like these:
customers
id customer location
1 Adam UK
2 Pete US
values
id value
1 10
1 7
2 3
2 41
Let's ignore here for a moment that that (and the following query) wouldn't make a lot of sense. It's meant as a simplified example.
Now, if I run this query
SELECT id, customer, value FROM customers INNER JOIN values GROUP BY id
I should get this result (distinct by id)
id customer value
1 Adam 10
2 Pete 3
What I would like to be able to do is get that to use it in a search result list, but for actual displaying of the results I'd like to do something like this:
Customer: Adam
Values: 10, 7
So, basically, while I need to have a result set that's distinct for the ID, I'd still like to somehow save the rows dropped by the GROUP BY to show the values list like above. What is the best way to do this?
Look at http://mysql.com/group_concat - which only will work in MySql.
Better link: http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_group-concat
Technically, the following is not valid SQL even though MySQL allows it:
Select customers.id, customers.customer, values.value
From customers
Inner Join values
On values.id = customers.id
Group By customers.id
The SQL spec requires that every column in the Select clause be referenced in the Group By or in an aggregate function. However, given what you said later in your post, what I think you want is GROUP_CONCAT as first mentioned by Erik (+1) which is a function specific to MySQL:
Select customers.customer, Group_Concat(values.value)
From customers
Inner Join values
On values.id = customers.id
Group By customers.customer