Sub Query

Posts   
 
    
EdFrancell
User
Posts: 3
Joined: 20-Dec-2005
# Posted on: 20-Dec-2005 03:19:13   

Dear Friends,

My Name is Eduardo and this is my first post.
I want to make this query, I think It's possible. How I do proceed?


select 
  [dbo].[TestTable].Id,
  [dbo].[TestTable].Name
  (
    select 
      Count(Sub.Id)
    from 
      [dbo].[TestTable] As Sub
    where 
      [dbo].[TestTable].Id = Sub.Id
  ) As 'Count'
from 
  [dbo].[TestTable]

Thanks !

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Dec-2005 06:56:18   

Your query is missing a comma after the second column name

select 
[dbo].[TestTable].Id,
[dbo].[TestTable].Name,
(
select 
Count(Sub.Id)
from 
[dbo].[TestTable] As Sub
where 
[dbo].[TestTable].Id = Sub.Id
) As 'Count'
from 
[dbo].[TestTable]

I assume that TestTable.Id is the PK of the table, in this case the inner select will return a count of one for every row!!

is that what you want?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-Dec-2005 09:02:07   

those kind of queries should be re-written with a join. Those are also more efficient. Subqueries in the selectlist aren't supported at the moment as well, so the join option is the only way to achieve this at the moment.

Frans Bouma | Lead developer LLBLGen Pro
EdFrancell
User
Posts: 3
Joined: 20-Dec-2005
# Posted on: 20-Dec-2005 13:10:17   

It was a simple example...

Could you help me to create something like that: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=3401&HighLight=1

Obs.: I'm working with SelfService.

Thanks!

Otis wrote:

those kind of queries should be re-written with a join. Those are also more efficient. Subqueries in the selectlist aren't supported at the moment as well, so the join option is the only way to achieve this at the moment.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Dec-2005 14:48:40   

Could you help me to create something like that: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=3401&HighLight=1

This thread already has a code example of how to create such a query.

Also please refer to the LLBLGen Pro documentation "Using the generated code - SelfServicing - The predicate system" and check the FieldCompareSetPredicate

Good Luck