Forum Moderators: open

Message Too Old, No Replies

Need a SQL Query for an Access database

         

kevinj

5:15 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



I have a table named Attendees that includes a field named position and a field named level. In the position field, there are letters that represent the attendees position. In the level field there are numbers that represent the grade level.

Examples of each:

Level Field

1,2,3

Position Field

B,O,P

I need a SQL query that will allow me to count the number of attendees that checked 1 for level AND B for position.

Any help would be greatly appreciated.

aspdaddy

5:22 pm on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should do it:
select count(*)
from attendees
where(level like '%1%' and position like '%B%')

kevinj

5:38 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



It gave me a total of zero. When I removed the %s it gave me a count of those that had an exact Level of 1 and Position of B. I need a count of any record that contains a 1 and a B. Doesn't look like the %s work. Any other thoughts.

Thanks for your help!

txbakers

5:45 pm on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the level and integer field or a text field?

the code above will work if the level is a text field.

If it's an integer it would look like this:

select count(*)
from attendees
where(level = 1 and position like '%B%')

aspdaddy

5:47 pm on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I thought you had raw text in one field like "1,2,3" :)

kevinj

5:53 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



both the Level and Position fields are text fields not integers. The data is raw text, just like,

1,2,3

or

B,A,P

aspdaddy

6:06 pm on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Strange... well seen as its access this should work :)

select count(*)
from attendees
where instr(level, '1') and instr(position, 'B')

kevinj

6:16 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



That works perfect. One last question. What would I need to do to get a count of the people that entered a B for position but left the level field blank? I tried this:

select count(*)
from attendees
where instr(level, '') and instr(position, 'J')

but the number it came up with was definitely not correct

kevinj

6:17 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



I meant this:

select count(*)
from attendees
where instr(level, '') and instr(position, 'B')

kevinj

6:36 pm on Mar 31, 2003 (gmt 0)

10+ Year Member



Never mind. I figured it out:

select count(*)
from attendees
where (Isnull(level) and instr(position, 'B'))

Thanks ASPdaddy and TX for the help!

Kevin

txbakers

6:50 pm on Mar 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's funny though as to why the %1% didn't work.

If the field really is "1,2,3", then %1% should return a true for that data row.

But, I'm glad it's working for you.