Grouping a Subselect

I've got a problem with the following query:

SELECT ( DatePart(HH, fld_date) ) AS myHour,
               COUNT(fld_ID) AS numPageviews,
               COUNT(DISTINCT fld_visitor_ID) AS numSessions,
               COUNT(DISTINCT fld_page_ID) AS numPages,
               COUNT(DISTINCT fld_statistic_fuseaction_ID) AS numFuseactions,
               SUM(fld_authenticated) AS numAuth
     FROM     tbl_statistic_data main
     WHERE     fld_date >= #CreateODBCDateTime(startDate)#
     AND      fld_date <= #CreateODBCDateTime(endDate)#
     AND      fld_site_ID = #session.sid#
     GROUP BY DatePart(HH, fld_date)
     ORDER BY DatePart(HH, fld_date)

The query returns statistical data grouped by hour.

The problem is with COUNT(DISTINCT fld_statistic_fuseaction_ID) AS numFuseactions.
I want it to count only those records where fld_page_ID is NULL. I believe this could be achieved with a subselect, but I don't know how to "attach" the subselect to my main query groups so it doesn't return the count for the entire timespan every time, but for every single hour.

Is there a way to achieve this or do I need to write multiple queries?

Thanks, Martin
knobboAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

malekamCommented:
Sure,
Just change

COUNT(DISTINCT fld_statistic_fuseaction_ID as numFueseactions

to:

SUM(CASE fld_statistic_fuseaction_ID
       WHEN NULL THEN 1
       ELSE 0
    END) AS numFueseactions

This counts all of the null id's.  You don't need the distinct since, if the id is null, it will not be distinct.
0
LowfatspreadCommented:

change
COUNT(DISTINCT fld_statistic_fuseaction_ID) AS numFuseactions

to Count(Distinct Case when fld_page_ID is NULL then fld_statistic_fuseaction_ID else Null end) AS numFuseactions


or is this more like what you're after

SELECT ( DatePart(HH, fld_date) ) AS myHour,
              COUNT(fld_ID) AS numPageviews,
              COUNT(DISTINCT fld_visitor_ID) AS numSessions,
              COUNT(DISTINCT fld_page_ID) AS numPages,
              COUNT(DISTINCT sub.subfuse) AS  numFuseactions,
              SUM(fld_authenticated) AS numAuth
    FROM     tbl_statistic_data main
     left outer Join
       (Select Datepart(hh,fld_date) as SubHr,
               Count(distinct
                 fld_statistic_fuseaction_id)  as SubFuse
          from tbl_statistic_data
    WHERE     fld_date >= #CreateODBCDateTime(startDate)#
    AND      fld_date <= #CreateODBCDateTime(endDate)#
    AND      fld_site_ID = #session.sid#
    and page_id is null
    GROUP BY DatePart(HH, fld_date)
        ) as Sub
    on Datepart(hh, main.fld_date) = sub.subhr
    WHERE     fld_date >= #CreateODBCDateTime(startDate)#
    AND      fld_date <= #CreateODBCDateTime(endDate)#
    AND      fld_site_ID = #session.sid#
    GROUP BY DatePart(HH, fld_date)
    ORDER BY DatePart(HH, fld_date)



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
knobboAuthor Commented:
Thanks lowfatspread, your first solution is exactly what i wanted.
I'll try the other one as well, might come in handy later :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.