There are two tables:
Table of peoples (m_id, m_name);
Table of links (m_id, f_id), where both fields link to m_id from first table
I need an Oracle database query that prints the word "Possible" if everyone is linked to everyone by not more than:
through 3 friends
through N friends
otherwise prints "Impossible"
Help me with this task if it's possible, or at least show me where to look for the answer, I mean what I have to read before, and what's necessary for solving this task.
I am not sure i got you question correct but i guess you require something like this.
select p.m_id,count(l.f_id),'Possible' col
from people p,
links l
where p.m_id = l.m_id
group by p.m_id
having count(l.f_id) >= 3
union
select p.m_id,count(l.f_id),'Impossible' col
from people p,
links l
where p.m_id = l.m_id
group by p.m_id
having count(l.f_id) < 3
Related
My query here has a sub-query in it but it returns no output, but in reality it has to give some output because I manually checked and output exists.I have posted the query below.
select mac.mac_id,mac.mac1,mac.mac_type,record.soc_id
from mso_charter.mac
join record on mac.record_id = record.record_id
where mac.mac_type='ethB' and record.soc_id IN (select soc from d);
Sample data is below
mac_id mac1 mac_type record_id--- for table mac
1 6142 ethA 1
2 6412 ethB 1
3 2313 ethC 1
record_id soc_id ---- for table record
1 Qu132
1 as432
1 342aq
soc --- for table d
a12w2
23we
qw12
mso_charter is the schema name mac,d and record is the table name.
Note that your subquery is actually still a join and can be written that way:
select mac.mac_id,mac.mac1,mac.mac_type,record.soc_id
from mso_charter.mac
join record using(record_id)
join d on record.soc_id = d.soc
where mac.mac_type='ethB';
As per the comment we still need a data set to reproduce and help.
Should be select soc_id from d instead of select select soc from d
According to your sample data, d has a column soc_id. That should be used for the comparison:
select m.mac_id, m.mac1, m.mac_type, r.soc_id
from mso_charter.mac m join
record r
on m.record_id = r.record_id
where m.mac_type = 'ethB' and
r.soc_id in (select d.soc_id from d);
It is possible that ids look the same but are not, because of international characters, hidden characters, spaces in the wrong place, and so on.
If this doesn't work then try the following:
Remove the soc_id condition and see if any rows match the first condition and join.
If that still returns nothing, remove the entire where clause to see if anything matches the join.
None of your record.soc_id match any of your d.soc_id. So you get no row.
Also, you write select soc from d. soc, not soc_id. Typo or error?
So, thanks to all who tried helping me in this situation. I actually had did a very silly mistake.The reason I am posting the right answer is probably because if someone else in future get stuck in such a issue or something similar it would be helpful to them.
select m.mac_id,m.mac,m.mac_type,r.soc_id
from mso_charter.mac m
join mso_charter.record r on m.record_id = r.record_id
where m.mac_type = 'ethB' and r.soc_id IN (select d.soc_id from d);
Mistake was I had not mentioned the schema name while performing join and there were multiple tables named record in other schema's, it was just out of frustration we tend to forget small things which costed me few hours to work over.
I have two tables, with structures like this:
roomlist:
-room (unique)
-totalDesks
userlist:
-room (has duplicates)
-deskOwned
I need a SQL statement that will spit out the following:
room
totalDesks
desksUsed (COUNT(DISTINCT userlist.desk))
desksOpen (totalDesks-(COUNT(DISTINCT userlist.desk)))
I have this so far:
SELECT
DISTINCT roomList.room, userlist.room, roomList.totalDesks,
COUNT(DISTINCT userlist.desk) AS desksUsed,
roomlist.totalDesks - COUNT(DISTINCT userlist.desk) AS desksOpen
FROM
roomlist, userlist
WHERE
roomlist.room = userlist.room
The problem is that not all rooms currently have users in them. If I have rooms A, B, C and D, but only records of people in A, B and C, it won't include room D in the result - even though room D has 5 totalDesks, none of which are taken.
How can I get a result that will still give me results on a room from roomlist, even if no records exist for that room in userlist?
Try a LEFT JOIN instead. I assume you need to group by room as well:
SELECT DISTINCT
roomList.room,
userlist.room,
roomList.totalDesks,
COUNT(DISTINCT userlist.desk) AS desksUsed,
roomlist.totalDesks-COUNT(DISTINCT userlist.desk) AS desksOpen
FROM roomlist
LEFT JOIN userlist ON roomlist.room=userlist.room
GROUP BY roomList.room,
userlist.room,
roomList.totalDesks
I would suggest mocking this up in http://sqlfiddle.com/ it will be much easier to help you then
I'm making a walkathon rails app. Each walker is sponsored by people who pledge money per lap up to a maximum amount.
I have a table called sponsorships that has columns walker_id, amount_per_lap, and max_amount. I want to write a SQL query to determine how much money a walker has raised.
There is also a walkers table that has id, name, and laps columns.
I know this isn't valid SQL, but I wanted something like this, but don't know the best way to do it. The walker_id and laps could be provided before executing the query.
SELECT SUM(MIN(Laps * sponsorships.amount_per_lap, sponsorships.max_amount)) FROM sponsorships
where sponsorships.walker_id = 1;
I'm making this in rails, so I was trying to figure out how to do something like this in Arel, but couldn't figure it out.
Any help would be greatly appreciated.
Edit: clarifying the walkers table.
Edit2: Accidentally had max instead of min in the pseudo code
I think the SQL you're looking for is this:
select w.id, w.name, sum(least(w.laps * s.amount_per_lap, s.max_amount))
from walkers w
join sponsorships s on w.id = s.walker_id
group by w.id, w.name
The least function is what applies your "no more than max_amount" condition. Translating that to AR should be a simple matter for you now that you know what to SELECT; I tend to go straight to SQL for anything like this.
I would try something like:
class Sponsorship < ActiveRecord::Base
name
walker_id
amount_per_lap
max_amount
class Walker < ActiveRecord::Base
name
number_of_laps
walker_sum = 0
walker=walker.find(1) # For 1 walker.
walker.sponsorships.each do |sponsor| #
walker_sum+=
min((sponsor.amount_per_lap * walker.number_of_laps), (sponsor.max_amount))
end
I've found a great site to practice sql - http://sqlzoo.net. my sql is very weak that is why i want to improve it by working on the exercises online. But i have this one problem that I cannot solve. can you please give me a hand.
3a. Find the songs that appear on more than 2 albums. Include a count of the number of times each shows up.
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
my answer is incorrect as i ran the query.
select a.song, count(a.song) from track a, track b
where a.song = b.song
a.album != b.album
group by a.song
having count(a.song) > 2
thanks in advance! :D
I realize this answer may be late but for future reference to anyone taking on this tutorial the answer is as such
SELECT track.song, count(album.title)
FROM album INNER JOIN track ON (album.asin = track.album)
GROUP BY track.song
HAVING count(DISTINCT album.title) > 2
Some things that my help you in your quest for this query is that what to group by is usually specified by the word each. As per the tip presented in the previous answers you want to select by distinct albums, SINCE it mentioned in the database description that album titles would be repeated when the two tables are joined
Your original answer is very close, with the GROUP BY and HAVING clause. What is wrong, is just that you don't need to join the track table against itself.
SELECT song, count(*)
FROM track
GROUP BY song
HAVING count(*) > 2
Another answer here uses COUNT(DISTNCT album), which is necessary only if a song can appear on an album more than once.
If they support nested querys, you can:
Select song, count(*)
from(
select a.song
from track a
group by a.song, a.album
having count(*) > 1
)
group by song
or(best way to write it)if they support this syntax:
select a.song, count(distinct a.album)
from track a
group by a.song
having count(distinct a.album) > 1
I have written a psychological testing application, in which the user is presented with a list of words, and s/he has to choose ten words which very much describe himself, then choose words which partially describe himself, and words which do not describe himself. The application itself works fine, but I was interested in exploring the meta-data possibilities: which words have been most frequently chosen in the first category, and which words have never been chosen in the first category. The first query was not a problem, but the second (which words have never been chosen) leaves me stumped.
The table structure is as follows:
table words: id, name
table choices: pid (person id), wid (word id), class (value between 1-6)
Presumably the answer involves a left join between words and choices, but there has to be a modifying statement - where choices.class = 1 - and this is causing me problems. Writing something like
select words.name
from words left join choices
on words.id = choices.wid
where choices.class = 1
and choices.pid = null
causes the database manager to go on a long trip to nowhere. I am using Delphi 7 and Firebird 1.5.
TIA,
No'am
Maybe this is a bit faster:
SELECT w.name
FROM words w
WHERE NOT EXISTS
(SELECT 1
FROM choices c
WHERE c.class = 1 and c.wid = w.id)
Something like that should do the trick:
SELECT name
FROM words
WHERE id NOT IN
(SELECT DISTINCT wid -- DISTINCT is actually redundant
FROM choices
WHERE class == 1)
SELECT words.name
FROM
words
LEFT JOIN choices ON words.id = choices.wid AND choices.class = 1
WHERE choices.pid IS NULL
Make sure you have an index on choices (class, wid).