Subquery?

Posts   
 
    
llblPowa
User
Posts: 41
Joined: 19-Nov-2006
# Posted on: 21-Mar-2007 13:31:10   

Hello All,

Is it possible to do this kind of query :


Select name, (select count(*) from cars where UserID=UserID) as 'NumberOfCars'
from User

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 21-Mar-2007 13:34:09   

See the documentation -> Generated code - Field expressions and aggregates -> Scalar query expressions

JCroain
User
Posts: 10
Joined: 08-Dec-2006
# Posted on: 21-Mar-2007 15:48:13   

Hi, I'm encountred problem to do this statement. Can you help me.


Select count(*) From t_calendrier
     Where cal_dt >= 
          case 
           when @StartDate <= ven_dt 
               then abo_dt
           else @StartDate
          end


And how make this statement too.


select date from
(
    select ven_dt date
    union
    select @EndDate date
)

Thanks in advance

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Mar-2007 15:57:20   

1- Can we convert the above statement to the following:


Select count(*) From t_calendrier
Where (cal_dt >= abo_dt     AND ven_dt >= @StartDate)
      OR (cal_dt >= @StartDate AND ven_dt <= @StartDate)

2- Does this query run? I thought UNIONS are done between tables only.

JCroain
User
Posts: 10
Joined: 08-Dec-2006
# Posted on: 22-Mar-2007 16:08:25   

Hello,

Thanks for you help : it does work :-)

Regarding my second issue I find a walkaround by refactoring the query (I am using a case now).

And yes, this query does work. We can union a table and other data if the resulting columns name (and format) are the same :-)

Cheers,