Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subquery count

Status
Not open for further replies.

snowboardr

Programmer
Feb 22, 2002
1,401
PH
I have this sql statement where its selecting a bunch of folders from the folder table (userfolders) and then its counting how many messages are in that folder in table (privatemessages) well I need to take this sql statement one step further and grab a column from the privatemessage folder also called pstatus, however you can't use more then 1 column in this subquery below...

thanks, Jason

Code:
SELECT 
f.fid, f.fuid, f.fname, 
( SELECT COUNT(ptoid) 
FROM privatemessage 
WHERE ptoid=2 
AND 
pfolder=f.fid ) 
as intMailCount 
FROM userfolders 
as f 
WHERE fuid=2 
ORDER by fname ASC
 
Code:
select f.fid
     , f.fuid
     , f.fname
     , PMcounts.pstatus
     , PMcounts.intMailCount 
  from userfolders as f 
inner
  join ( select pfolder
              , pstatus
              , count(ptoid) 
                  as intMailCount
           from privatemessage 
          where ptoid = 2 
         group
             by pfolder
              , pstatus
        )  as PMcounts
   on PMcounts.pfolder = f.fid 
 where fuid = 2 
order 
    by fname asc

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top