views:

36

answers:

3

the question: there are two tables - DAYS(name), ACTIONS(day_name,name)

records for DAYS are:

Sunday, Monday, Wednesday

records for ACTIONS are:

{eat,sunday},
{sleep,sunday},
{write,sunday},
{drink,sunday}

{eat,wednesday},
{sleep,wednesday},
{write,wednesday},

{eat,monday},
{sleep,monday},

i want to search in ACTIONS for the days that have the highest number of curtain actions, and return the results ordered from the higher to the lower. Meaning: if i search for "eat","sleep", "write", and "drink" - the results will be: sunday, wednesday, monday.

+2  A: 
SELECT d.name
    FROM ACTIONS a JOIN DAYS d ON a.day_name=d.name
        WHERE a.name IN ('eat', 'sleep', 'write', 'drink')  -- Your search
            GROUP BY a.name
                ORDER BY COUNT(*) DESC;
Adam Matan
+2  A: 

Are you looking for this...

SELECT day_name, number FROM
(
  SELECT day_name, COUNT(*) 
  FROM ACTIONS 
  WHERE name in ('eat', 'sleep', 'write') 
  GROUP BY day_name
) AS Test
ORDER BY number DESC
Yves M.
+1  A: 
select 
    day_name, count(day_name) qty
from
    actions
where 
    name in ('eat', 'sleep', 'write', 'drink')
group by day_name
order by qty desc
sheeks06