error on count query

i got a location(text) inside the variable xlocation
now, i want to count how many records have this location inside from or loc and where end is empty...

i got an error here: can you correct me please
mySQL8="SELECT end, from, to count(*) as StatusTotal FROM demo WHERE end is null and (from='" & xlocation &"' or to='" & xlocation &"') group by fromloc, toloc"

Open in new window

sasha85Asked:
Who is Participating?
 
mbizupConnect With a Mentor Commented:
Assuming your actual fieldnames are from and to, the group By clause should also use the same names

mySQL8="SELECT from, to, count(*) as StatusTotal FROM demo WHERE end is null and (from='" & xlocation & "' or to='" & xlocation &"') group by from, to"
 

0
 
mbizupConnect With a Mentor Commented:
Also use brackets to differentiate the From field from the From keyword:

="SELECT [from], [to], count(*) as StatusTotal FROM demo WHERE [end] is null and ([from]='" & xlocation & "' or [to]='" & xlocation &"') group by [from], [to]"
0
 
Doc_McAlisterConnect With a Mentor Commented:
Are "from" and "to" actual column names? Because when you use "group by" you can only have columns in your select that are either grouped by, or computed from the grouping.  So if you group by "fromloc" and "toloc" then you can Select "fromloc" and "toloc" but not "from" and "to".

I am assuming that the columns are fromloc and toloc, not from and to.  I am also assuming that you want to match "xlocation", not " xlocation ".

Set @xlocation = 'Colorado'

Select fromloc, toloc, count(*) As 'Status Total'
From demo
Where end is null
   And ( fromloc like '%@xlocation%' Or toloc like '%@xlocation%' )
Group By fromloc, toloc

You can replace the "like"s with =  and remove the %'s if you don't need a pattern match.
0
 
Doc_McAlisterConnect With a Mentor Commented:
You can not select the "end" column without putting it in the group by clause.  But you have no need to as you are only getting the Nulls in the where clause.
0
All Courses

From novice to tech pro — start learning today.