?
Solved

Grouping a Subselect

Posted on 2003-03-03
3
Medium Priority
?
707 Views
Last Modified: 2010-08-05
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
0
Comment
Question by:knobbo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Expert Comment

by:malekam
ID: 8059367
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
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 520 total points
ID: 8059624

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
 

Author Comment

by:knobbo
ID: 8063285
Thanks lowfatspread, your first solution is exactly what i wanted.
I'll try the other one as well, might come in handy later :)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question