Something very simple, but I cannot get it working for me :)
select x.name, count(x.name) from <table_name> x where ...<complex and long>...
It throws an error, that the x.name isn't used with group by.
select x.name from <table_name> x where ...<complex and long>...
works fine and returns e.g. 6 names
select count(x.name) from <table_name> x where ...<complex and long>...
works also fine and returns e.g. the number 6
But the combination is not working, when I tried to add the group by:
select x.name, count(x.name) from <table_name> x where ...<complex and long>...
group by x.name
it worked, but all counts were 1 and not 6.
The thing is, I could firstly get the count into variable, then write the long sql statement just to get the names, but I don't want to write the long complex select statement twice. There has to be some way to do the combination in one select: get all names and by the way tell me how many they were.
Thanks
P.S.
name bla1 bla2
a ... ...
a foo ...
b foo ...
c ... ...
d foo ...
d foo ...
b foo ...
c foo ...
The result of x.name where bla1 = foo would be:
a
b
d
d
b
c
The result of count(x.name) where bla1 = foo would be:
6
My desired result of:
...variable definitions
select @foundName = x.name, @numOfAllFoundNames = count(x.name)
from <table_name> x where ...<complex and long>...
should be: @foundName = a (just one of the names, no matter which one) @numOfAllFoundNames = 6