Full Version: Select Count To Count Everything Except...
UtterAccess Discussion Forums > Microsoft® Access > Access Queries
Bongazi
Hi UA again,

I am struggling to make a (SELECT Count([Trucks]) FROM [tblTrucks]). What I need to do is count every truck in tbltrucks except
trucks containing the number 769... (this is in a query)

what I have is this, but it counts all trucks...
CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks])


and then I have this, but this only counts the trucks with 777 and there are different trucks in there that need to counted as well:
CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] like  "*777*")



what i need is a statement that counts everything except 769 trucks :-)


Thanking you in advance for your help,

Bongazi


cpetermann
Maybe try:

CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] <>like  "*777*")


HTH
Bongazi
Hi HTH,

I tried your statement but I get a "Syntax Error" trying to execute the query... (attached image)

Thank you for your time.

Bongazi
cpetermann
Looks like I might have left a space in the expression. blush.gif

How does this work for you?
CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] <>like "*777*")


If this doesn't work, you might want to try String Manipulation Functions
Bongazi
I am sorry - but I am getting the same error...
I will take a look at the string manipulation - maybe there is a way out of this :-)


Thank you again for your time

Bongazi
cpetermann
thumbup.gif

Just wondering, are trucks with 769 always excluded from Available Trucks?

If not, you might want to consider creating a query that parses the identifying number from the string of all trucks
And use the result to filter the available trucks from a form that would use this parsed string in an unbound combobox.
the RecordSource of the form could be a query that Combines the Parsed qry results with the tblTrucks
and with the Parsed Field criteria being: <>Forms!NameOfYourForm.NameOfUnboundCBO.



just an idea smile.gif
Bongazi
Hi, thank you for the idea - but this would just complicate things. The trucks "769" are always excluded from "Available Trucks" and
should be ignored from the count...

Kind regards,
Bongazi

cpetermann
thumbup.gif

Please let us know how parsing the truck number works for you smile.gif
Bongazi
lol - I am still trying to wrap my head around that.... :-)


regards
cpetermann
QryFindTruckNumber (or whatever you want to name it wink.gif


LftParsTruck:Left([trucks],NumberOfCharacters
Parses the number of characters from the Left

Then take that result:
RtParsTruck:Right([LftParsTruck],NumberOfCharacter from the right

LftParsTruck & RtParsTruck are just aliases I came up with--Use your own understandable aliases.

Available Trucks: (SELECT Count([Trucks]) FROM [QryFindTruckNumber] where [RtParsTruck] <>769)


Does this help?
Bongazi
Hi again,

i did not get far with the string manipulation - but this seems to work:

CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks])-(SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] like "*769*")


it gives me the correct count - so I guess this will work?

regards,
Bongazi
cpetermann
As my good friend Jack always says, "There's more than one way to skin the Access cat!" laugh.gif

If you only need the Count of available trucks, that will do the trick!
If you need a list of the available trucks, then try Parsing. smile.gif
Bongazi
LOL ohyeah.gif

I only need to count the available trucks excluding the "769"....
If this will do the trick - this is what I will use.

Thank you for your time and help !
thanks.gif

Regards,
Bongazi
cpetermann
yw.gif
argeedblu
Hmmm.

What about
CODE
(SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] not like  "*777*")


Glenn
Bongazi
Hi Glenn,

thank you for that - I tried your code and it works perfectly thumbup.gif
I will use your code - simpler and most probably faster as well...

Thank you ! thanks.gif

Regards,
Bongazi
cpetermann
Glenn,

Learn something from you everytime smile.gif

Why would Not Like work and <>Like.. cause syntax error?

Duh! Text not number blush.gif

think it's time for my nap sleeping.gif
Bongazi
changed it just a bit to:

CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] not like  "*769*")


to count trucks that don't have the 769 identifier...

regards,
Bongazi

argeedblu
@Boganzi,

Yes, of course, you would specify whatever value you want to exclude. Also don't forget about wildcards when you are specifying criteria:

"*76?2*" would, for example specify any text value that include 76 and any text value that has 76 followed by any character followed by 2.

@Cythia,

Have a good nap. I will stand watch while you are off-duty. tongue.gif

Glenn
Bongazi
Hi Glenn,

thank you for that - I use *769* that seems to work perfectly :-)

Thank you and regards
Bongazi

PS: could I add more trucks to the exclusion list if I needed to with AND statement or so?
like (i don know the correct syntax):

CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] not like  "*769*" and where [trucks] not like  "*123*" )


regards
Bongazi
argeedblu
Hi Bongazi,

CODE
WHERE (((tblTrucks.Truck) Not Like "*769*" And (tblTrucks.Truck) Not Like "*123*"));


The query designer puts in extra parentheses so this should work also:

CODE
WHERE ((tblTrucks.Truck) Not Like "*769*" And (tblTrucks.Truck) Not Like "*123*");


and so will this:

CODE
WHERE (tblTrucks.Truck Not Like "*769*" And tblTrucks.Truck Not Like "*123*");


However you do have to have the outer ones:

Where (multiple criteria).

Glenn
niesz
>>However you do have to have the outer ones<<

I don't believe that is true. You do not need any parens at all (unless your criteria requires it for precedence)
argeedblu
Hi Walter,

I may be wrong but I think I recall having some queries that I constructed in VBA breakdown when there were multiple WHERE criteria not enclosed in at least one set of parentheses. However, I just did an empirical test and a query with multiple query using an OR did work just fine. Interesting dontknow.gif that the QBE puts all those parentheses in.

Glenn
Bongazi
after a bit of tinkering - this would be the correct statement - at least it is producing the correct results:

CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] not like  "*769*" AND [trucks] not like "*123*")



regards,

Bongazi
niesz
The QBE does it "just in case" to force order of operations, but none are ever required that I know of.
argeedblu
Bongazi,

Try it without the outer parentheses and it should work equally well.

Glenn
Bongazi
QUOTE (argeedblu @ May 10 2012, 08:21 AM) *
Bongazi,

Try it without the outer parentheses and it should work equally well.

Glenn


Hi Glenn,

when I do, I get the following error (attached image)
this is the code:
CODE
Available Trucks: SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] not like  "*769*" AND [trucks] not like "*123*"



Regards,
Bongazi
niesz
You still need parens around the whole subquery, but you do not need a set around the criteria.
PaulBrand
Yeah, but you don't need to repeat WHERE: And [trucks] Not Like "*123*"

how late was I????
argeedblu
Bongazi,

Interesting. iconfused.gif This reminds me of what I have experienced when constructing queries in VBA. Perhaps Walter will comment. Before my last post I tried a similar query in the QBE without the outer parentheses and it did not error out.

My pragmatic side says then, go with what works and hope that a reasonable explanation surfaces while the problem is still fresh in your mind.

Glenn
Bongazi
QUOTE (PaulBrand @ May 10 2012, 08:36 AM) *
Yeah, but you don't need to repeat WHERE: And [trucks] Not Like "*123*"

how late was I????


Hi Paul,
if I remove the second WHERE ... if I understand you correctly i get an error as well (attached image)
CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] not like  "*769*" AND not like "*123*")


Regards,
Bongazi
Bongazi
QUOTE (argeedblu @ May 10 2012, 08:36 AM) *
Bongazi,

Interesting. iconfused.gif This reminds me of what I have experienced when constructing queries in VBA. Perhaps Walter will comment. Before my last post I tried a similar query in the QBE without the outer parentheses and it did not error out.

My pragmatic side says then, go with what works and hope that a reasonable explanation surfaces while the problem is still fresh in your mind.

Glenn


Hi Glenn,
I will stick to the code that currently produces the right count:
CODE
Available Trucks: (SELECT Count([Trucks]) FROM [tblTrucks] where [trucks] not like  "*769*" AND [trucks] not like "*123*")


thank you all for your help - this is really appreciated!!

thumbup.gif

Regards,
Bongazi
niesz
There's two issues here ...

1) Do you need parens around criteria in a query? No
2) Do you need parens around subqueries? Yes

Glenn asked you to remove the parens around the criteria of the subquery, but you removed them from around the entire subquery. (Your criteria didn't have any around them to remove)

Your current SQL is correct.
Bongazi
Hi niesz,

thank you for that - I will keep that in mind.

Kind regards,
Bongazi
niesz
thumbup.gif
argeedblu
@Bongazi,

You are welcome. I think I can speak for everyone who has contributed to this thread when I say we are always happy to help.

@Walter, What you said. thumbup.gif

Glenn
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.