1

what I'm trying to do:

from these tables

---------------------------------
name      date      state
---------------------------------
ali      jan 12    started

ali      jan 12    drop out

masa     jan 12    registered

masa     jan 12    started

sami     jan 12    started

I want the results to be

---------------------------------
name      date      state
---------------------------------
masa     jan 12     started

sami     jan 12     started

So basically what i want is to have all the started users without the ones who dropped out so the filtering should be based on the state thanks

4
  • 1
    well on what category would you want to filter out data Commented Feb 6, 2011 at 8:39
  • You should show what you have tried already to show what you are having trouble with Commented Feb 6, 2011 at 8:45
  • i already tried something like : select * from log where log.state = started and log.state !=drop out but it didn't work the way i want it because it would show "ali" as started even though he is not anymore (drop out) so what i want is something to show people who started and still starting (didn't drop out) and oh the drop out date is always after the start date but i omit the day for simplicy as i can simply "group by" and add "having" the date between the 2 dates Commented Feb 6, 2011 at 16:20
  • thanks experminetX for the edit it's so much clearer now :) Commented Feb 6, 2011 at 16:29

4 Answers 4

3

Those two rows in your result example are not the only started people on that date.

SELECT * FROM table WHERE state = 'started';

Would return 3 rows:

---------------------------------
name      date      state
---------------------------------

ali      jan 12     started

masa     jan 12     started

sami     jan 12     started

To get the two rows in your example you need:

SELECT * FROM table WHERE name IN ('sami', 'mesa');

Update, added this example

You could limit if you only wanted two rows:

SELECT * FROM table WHERE state = 'started' LIMIT 2;
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your reply but i want my results filtered based on the state not the names
Sorry your question was unclear, this is what you need: SELECT * FROM table WHERE state != 'drop out' GROUP BY name; This will only show one row per name, and no one who has dropped out.
No it wouldn't! GROUP will only show people (once) who have not dropped out. Try it!
yes but what if someone registered and didn't start, the query only exclude the drop out am i right?
2

Perhaps something along the following lines is what you need...

SELECT NAME,
       DATE,
       STATE
   FROM MYTAB
   WHERE STATE = 'Started' AND
         NOT EXISTS (SELECT *
                         FROM MYTAB MYTAB2
                         WHERE MYTAB2.NAME  = MYTAB.NAME AND
                               MYTAB2.STATE = 'Drop Out');

That should get everyone who started and didn't drop out.

8 Comments

thank you i think this would solve it although i need to try it first, will it work in MS Access ? sorry for my laziness
@Ali, I don't see any reason why it wouldn't.
it worked but it's taking forever to show the results...:( why is that?
@Ali, if you are using MySQL, you could try EXPLAIN ..., and post the results; otherwise I'd guess you need an index on the NAME and STATE columns, but without the explain results, that is a guess. How large is your table?
SELECT DISTINCTROW Basic.ApplicationNumber FROM Service INNER JOIN (Basic INNER JOIN Log ON Basic.[b_id] = Log.[b_fid]) ON Service.[service_id] = Log.[s_fid] WHERE (((Format$([ServiceDate],'mmmm yyyy'))="January 2011") AND ((Service.ServiceName)="english") AND ((Log.state)=1) AND ((Exists (
|
1
SELECT name, date, state FROM table GROUP BY name HAVING state = 'started';

I believe this would eliminate people who dropped out eventually because the GROUP BY would put the people with the same name into the same group, and then eliminate them once they drop out with the HAVING statement.

2 Comments

thanks for your reply that would show Ali who started but then droped out while what i needed only people who started and didn't drop out
SELECT Basic.ApplicationNumber,log.state FROM Service INNER JOIN (Basic INNER JOIN Log ON Basic.b_id = Log.b_fid) ON Service.service_id = Log.s_fid WHERE (Service.ServiceName="english" AND Format$(log.ServiceDate,'mmmm yyyy')="January 2011") GROUP BY Basic.ApplicationNumber,log.state having log.state=1 it didn't work it shows all the started and it does not exclude the drop outs
0
  select * from table where state != 'dropped out'

2 Comments

-1 that would include the registered row too. (should your sql say drop out)
exactly :) i don't want to show the registered i only need the started who didn't drop out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.